@Override public void evaluate() throws Throwable { base.evaluate(); List<LogItem> logs = ShadowLog.getLogs(); for (LogItem log : logs) { LogItem throwLessLogItem = new LogItem(log.type, log.tag, log.msg, null);
/** Clear all accumulated logs. */ public static void clear() { reset(); }
@Test public void clear() { assertThat(ShadowLog.getLogsForTag("tag1")).isEmpty(); Log.d("tag1", "1"); assertThat(ShadowLog.getLogsForTag("tag1")).isNotEmpty(); ShadowLog.clear(); assertThat(ShadowLog.getLogsForTag("tag1")).isEmpty(); assertThat(ShadowLog.getLogs()).isEmpty(); } }
@Test public void shouldLogAccordingToTag() throws Exception { ShadowLog.reset(); Log.d("tag1", "1"); Log.i("tag2", "2"); Log.e("tag3", "3"); Log.w("tag1", "4"); Log.i("tag1", "5"); Log.d("tag2", "6"); List<LogItem> allItems = ShadowLog.getLogs(); assertThat(allItems.size()).isEqualTo(6); int i = 1; for (LogItem item : allItems) { assertThat(item.msg).isEqualTo(Integer.toString(i)); i++; } assertUniformLogsForTag("tag1", 3); assertUniformLogsForTag("tag2", 2); assertUniformLogsForTag("tag3", 1); }
@Implementation protected static int println_native(int bufID, int priority, String tag, String msg) { addLog(priority, tag, msg, null); int tagLength = tag == null ? 0 : tag.length(); int msgLength = msg == null ? 0 : msg.length(); return extraLogLength + tagLength + msgLength; }
/** Parses an AndroidManifest.xml file using the framework PackageParser. */ public static Package callParsePackage(Path apkFile) { PackageParser packageParser = new PackageParser(); int flags = PackageParser.PARSE_IGNORE_PROCESSES; try { Package thePackage; if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.LOLLIPOP) { thePackage = packageParser.parsePackage(apkFile.toFile(), flags); } else { // JB -> KK thePackage = reflector(_PackageParser_.class, packageParser) .parsePackage( apkFile.toFile(), Fs.externalize(apkFile), new DisplayMetrics(), flags); } if (thePackage == null) { List<LogItem> logItems = ShadowLog.getLogsForTag("PackageParser"); if (logItems.isEmpty()) { throw new RuntimeException( "Failed to parse package " + apkFile); } else { LogItem logItem = logItems.get(0); throw new RuntimeException( "Failed to parse package " + apkFile + ": " + logItem.msg, logItem.throwable); } } return thePackage; } catch (Exception e) { throw new RuntimeException(e); } }
ShadowLog.setupLogging(); loggingInitialized = true;
@Implementation protected static int i(String tag, String msg) { return i(tag, msg, null); }
@Implementation protected static int d(String tag, String msg) { return d(tag, msg, null); }
@Implementation protected static int e(String tag, String msg) { return e(tag, msg, null); }
@Implementation protected static int v(String tag, String msg) { return v(tag, msg, null); }
private static int addLog(int level, String tag, String msg, Throwable throwable) { if (stream != null) { logToStream(stream, level, tag, msg, throwable); } LogItem item = new LogItem(level, tag, msg, throwable); Queue<LogItem> itemList; synchronized (logsByTag) { if (!logsByTag.containsKey(tag)) { itemList = new ConcurrentLinkedQueue<>(); logsByTag.put(tag, itemList); } else { itemList = logsByTag.get(tag); } } itemList.add(item); logs.add(item); return 0; }
@Implementation protected static int e(String tag, String msg, Throwable throwable) { return addLog(Log.ERROR, tag, msg, throwable); }
private void assertUniformLogsForTag(String tag, int count) { List<LogItem> tag1Items = ShadowLog.getLogsForTag(tag); assertThat(tag1Items.size()).isEqualTo(count); int last = -1; for (LogItem item : tag1Items) { assertThat(item.tag).isEqualTo(tag); int current = Integer.parseInt(item.msg); assertThat(current > last).isTrue(); last = current; } }
@Override public void setupLogging() { ShadowLog.setupLogging(); }
@Implementation protected static int i(String tag, String msg) { return i(tag, msg, null); }
@Implementation protected static int d(String tag, String msg) { return d(tag, msg, null); }
@Implementation public static void e(String tag, String msg) { e(tag, msg, null); }
@Implementation public static void v(String tag, String msg) { v(tag, msg, null); }