Search in sources :

Example 1 with LogItem

use of org.robolectric.shadows.ShadowLog.LogItem in project timber by JakeWharton.

the class TimberTest method assertExceptionLogged.

private static void assertExceptionLogged(int logType, String message, String exceptionClassname, String tag, int index) {
    List<LogItem> logs = ShadowLog.getLogs();
    assertThat(logs).hasSize(index + 1);
    LogItem log = logs.get(index);
    assertThat(log.type).isEqualTo(logType);
    assertThat(log.tag).isEqualTo(tag != null ? tag : "TimberTest");
    if (message != null) {
        assertThat(log.msg).startsWith(message);
    }
    assertThat(log.msg).contains(exceptionClassname);
    // We use a low-level primitive that Robolectric doesn't populate.
    assertThat(log.throwable).isNull();
}
Also used : LogItem(org.robolectric.shadows.ShadowLog.LogItem)

Example 2 with LogItem

use of org.robolectric.shadows.ShadowLog.LogItem in project WordPress-Android by wordpress-mobile.

the class JsCallbackReceiverTest method assertLogged.

private void assertLogged(int type, String tag, String msg, Throwable throwable) {
    LogItem lastLog = ShadowLog.getLogs().get(0);
    assertEquals(type, lastLog.type);
    assertEquals(msg, lastLog.msg);
    assertEquals(tag, lastLog.tag);
    assertEquals(throwable, lastLog.throwable);
}
Also used : LogItem(org.robolectric.shadows.ShadowLog.LogItem)

Example 3 with LogItem

use of org.robolectric.shadows.ShadowLog.LogItem in project robolectric by robolectric.

the class ExpectedLogMessagesRule method apply.

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            base.evaluate();
            List<LogItem> logs = ShadowLog.getLogs();
            Map<ExpectedLogItem, Boolean> expectedLogItemMap = new HashMap<>();
            for (ExpectedLogItem item : expectedLogs) {
                expectedLogItemMap.put(item, false);
            }
            for (LogItem log : logs) {
                LogItem logItem = new LogItem(log.type, log.tag, log.msg, log.throwable);
                if (updateExpected(logItem, expectedLogItemMap)) {
                    observedLogs.add(logItem);
                    continue;
                }
                if (log.type >= Log.ERROR) {
                    if (UNPREVENTABLE_TAGS.contains(log.tag)) {
                        continue;
                    }
                    if (expectedTags.contains(log.tag)) {
                        observedTags.add(log.tag);
                        continue;
                    }
                    unexpectedErrorLogs.add(log);
                }
            }
            if (!unexpectedErrorLogs.isEmpty() || expectedLogItemMap.containsValue(false)) {
                Set<ExpectedLogItem> unobservedLogs = new HashSet<>();
                for (Map.Entry<ExpectedLogItem, Boolean> entry : expectedLogItemMap.entrySet()) {
                    if (!entry.getValue()) {
                        unobservedLogs.add(entry.getKey());
                    }
                }
                throw new AssertionError("Expected and observed logs did not match." + "\nExpected:                   " + expectedLogs + "\nExpected, and observed:     " + observedLogs + "\nExpected, but not observed: " + unobservedLogs + "\nObserved, but not expected: " + unexpectedErrorLogs);
            }
            if (!expectedTags.equals(observedTags) && !shouldIgnoreMissingLoggedTags) {
                throw new AssertionError("Expected and observed tags did not match. " + "Expected tags should not be used to suppress errors, only expect them." + "\nExpected:                   " + expectedTags + "\nExpected, and observed:     " + observedTags + "\nExpected, but not observed: " + Sets.difference(expectedTags, observedTags));
            }
        }
    };
}
Also used : HashMap(java.util.HashMap) Statement(org.junit.runners.model.Statement) LogItem(org.robolectric.shadows.ShadowLog.LogItem) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with LogItem

use of org.robolectric.shadows.ShadowLog.LogItem in project robolectric by robolectric.

the class ShadowLogTest method identicalLogItemInstancesAreEqual.

@Test
public void identicalLogItemInstancesAreEqual() {
    LogItem item1 = new LogItem(Log.VERBOSE, "Foo", "Bar", null);
    LogItem item2 = new LogItem(Log.VERBOSE, "Foo", "Bar", null);
    assertThat(item1).isEqualTo(item2);
    assertThat(item2).isEqualTo(item1);
}
Also used : LogItem(org.robolectric.shadows.ShadowLog.LogItem) Test(org.junit.Test)

Example 5 with LogItem

use of org.robolectric.shadows.ShadowLog.LogItem in project robolectric by robolectric.

the class ShadowPackageParser method callParsePackage.

/**
 * 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) {
            // PackageParser#setMinAspectRatio(Package)
            if (RuntimeEnvironment.getApiLevel() >= Build.VERSION_CODES.Q) {
                QHelper.setCallback(packageParser);
            }
            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);
    }
}
Also used : PackageParser(android.content.pm.PackageParser) LogItem(org.robolectric.shadows.ShadowLog.LogItem) Package(android.content.pm.PackageParser.Package) DisplayMetrics(android.util.DisplayMetrics)

Aggregations

LogItem (org.robolectric.shadows.ShadowLog.LogItem)9 Test (org.junit.Test)2 PackageParser (android.content.pm.PackageParser)1 Package (android.content.pm.PackageParser.Package)1 DisplayMetrics (android.util.DisplayMetrics)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Statement (org.junit.runners.model.Statement)1