Commit c0c05897 authored by dgn's avatar dgn Committed by Commit bot

Add junit tests for //base/android

Adds a gyp and gn build target: base_junit_tests
Adds test suite: base_junit_tests

Depends on https://codereview.chromium.org/1048153002/
BUG=448030,472152

Review URL: https://codereview.chromium.org/1051343002

Cr-Commit-Position: refs/heads/master@{#326861}
parent e5983b7f
...@@ -1463,6 +1463,15 @@ if (is_android) { ...@@ -1463,6 +1463,15 @@ if (is_android) {
DEPRECATED_java_in_dir = "test/android/javatests/src" DEPRECATED_java_in_dir = "test/android/javatests/src"
} }
# GYP: //base.gyp:base_junit_tests
junit_binary("base_junit_tests") {
java_files = [ "android/junit/src/org/chromium/base/LogTest.java" ]
deps = [
":base_java",
":base_java_test_support",
]
}
# GYP: //base.gyp:base_java_application_state # GYP: //base.gyp:base_java_application_state
# GYP: //base.gyp:base_java_library_load_from_apk_status_codes # GYP: //base.gyp:base_java_library_load_from_apk_status_codes
# GYP: //base.gyp:base_java_library_process_type # GYP: //base.gyp:base_java_library_process_type
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.chromium.testing.local.LocalRobolectricTestRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.shadows.ShadowLog;
import java.util.List;
/** Unit tests for {@link Log}. */
@RunWith(LocalRobolectricTestRunner.class)
@Config(manifest = Config.NONE, shadows = {LogTest.PermissiveShadowLog.class})
public class LogTest {
/** Test method for {@link Log#makeTag(String)} */
@Test
public void testMakeTag() {
assertEquals("chromium.Foo", Log.makeTag("Foo"));
assertEquals("chromium", Log.makeTag(null));
assertEquals("chromium", Log.makeTag(""));
}
/** Tests that the computed call origin is the correct one. */
@Test
public void callOriginTest() {
Log.d("Foo", "Bar");
List<ShadowLog.LogItem> logs = ShadowLog.getLogs();
assertEquals("Only one log should be written", 1, logs.size());
assertTrue("The origin of the log message (" + logs.get(0).msg + ") looks wrong.",
logs.get(0).msg.matches("\\[LogTest.java:\\d+\\].*"));
}
/** Tests that exceptions provided to the log functions are properly recognized and printed. */
@Test
public void exceptionLoggingTest() {
Throwable t = new Throwable() {
@Override
public String toString() {
return "MyThrowable";
}
};
Throwable t2 = new Throwable() {
@Override
public String toString() {
return "MyOtherThrowable";
}
};
List<ShadowLog.LogItem> logs = ShadowLog.getLogs();
// The throwable gets printed out
Log.i("Foo", "Bar", t);
assertEquals(t, logs.get(logs.size() - 1).throwable);
assertEquals("Bar", logs.get(logs.size() - 1).msg);
// The throwable can be both added to the message itself and printed out
Log.i("Foo", "Bar %s", t);
assertEquals(t, logs.get(logs.size() - 1).throwable);
assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg);
// Non throwable are properly identified
Log.i("Foo", "Bar %s", t, "Baz");
assertNull(logs.get(logs.size() - 1).throwable);
assertEquals("Bar MyThrowable", logs.get(logs.size() - 1).msg);
// The last throwable is the one used that is going to be printed out
Log.i("Foo", "Bar %s %s", t, t2);
assertEquals(t2, logs.get(logs.size() - 1).throwable);
assertEquals("Bar MyThrowable MyOtherThrowable", logs.get(logs.size() - 1).msg);
}
/** Needed to allow debug/verbose logging that is disabled by default. */
@Implements(android.util.Log.class)
public static class PermissiveShadowLog extends ShadowLog {
@Implementation
public static boolean isLoggable(String tag, int level) {
return true;
}
}
}
...@@ -1500,6 +1500,23 @@ ...@@ -1500,6 +1500,23 @@
}, },
'includes': [ '../build/java.gypi' ], 'includes': [ '../build/java.gypi' ],
}, },
{
# GN: //base:base_junit_tests
'target_name': 'base_junit_tests',
'type': 'none',
'dependencies': [
'base_java',
'base_java_test_support',
'../testing/android/junit/junit_test.gyp:junit_test_support',
],
'variables': {
'main_class': 'org.chromium.testing.local.JunitTestMain',
'src_paths': [
'../base/android/junit/',
],
},
'includes': [ '../build/host_jar.gypi' ],
},
{ {
# GN: //base:base_javatests # GN: //base:base_javatests
'target_name': 'base_javatests', 'target_name': 'base_javatests',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment