Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1989)

Unified Diff: base/android/junit/src/org/chromium/base/CheckTest.java

Issue 1918403004: Add Check and DCheck java classes to base (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Converted crashing mechanism to throw an AssertionError instead Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/android/java/src/org/chromium/base/ThreadUtils.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/junit/src/org/chromium/base/CheckTest.java
diff --git a/base/android/junit/src/org/chromium/base/CheckTest.java b/base/android/junit/src/org/chromium/base/CheckTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..a99810eef14187b902835d653b74f63765d0222a
--- /dev/null
+++ b/base/android/junit/src/org/chromium/base/CheckTest.java
@@ -0,0 +1,598 @@
+// Copyright 2016 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.assertFalse;
+
+import org.chromium.testing.local.LocalRobolectricTestRunner;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.robolectric.annotation.Config;
+
+import java.lang.reflect.Field;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/** Unit tests for {@link Check}. */
+@RunWith(LocalRobolectricTestRunner.class)
+@Config(manifest = Config.NONE)
+public final class CheckTest {
+ /**
+ * This test ensures that {@code Check.WAIT_FOR_DEBUGGER} is never checked in as true.
+ */
+ @Test
+ public void testWaitForDebuggerDisabled() throws Exception {
+ final Field waitForDebuggerField = Check.class.getDeclaredField("WAIT_FOR_DEBUGGER");
+ waitForDebuggerField.setAccessible(true);
+ assertFalse(waitForDebuggerField.getBoolean(null));
+ }
+
+ @Test
+ public void testIsTrue() {
+ Check.isTrue(true);
+ Check.isTrue(Boolean.TRUE);
+
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isTrue(false);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isTrue(Boolean.FALSE);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isTrue(null);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+ }
+
+ @Test
+ public void testIsFalse() {
+ Check.isFalse(false);
+ Check.isFalse(Boolean.FALSE);
+
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isFalse(true);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isFalse(Boolean.TRUE);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+
+ try {
+ Check.isFalse(null);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+ }
+
+ @Test
+ public void testIsEqual() {
+ final List<String> l1 = new LinkedList<>();
+ final List<String> l2 = new LinkedList<>();
+
+ Check.isEqual((Object) null, (Object) null);
+ Check.isEqual(false, false);
+ Check.isEqual(1, 1);
+ Check.isEqual((short) 1, (short) 1);
+ Check.isEqual(1L, 1L);
+ Check.isEqual(1f, 1f);
+ Check.isEqual(1d, 1d);
+ Check.isEqual('a', 'a');
+ Check.isEqual("a", "a");
+ Check.isEqual(l1, l1);
+ Check.isEqual(l1, l2);
+ Check.isEqual(new boolean[] {true}, new boolean[] {true});
+ Check.isEqual(new byte[] {1}, new byte[] {1});
+ Check.isEqual(new char[] {'a'}, new char[] {'a'});
+ Check.isEqual(new short[] {1}, new short[] {1});
+ Check.isEqual(new int[] {1}, new int[] {1});
+ Check.isEqual(new long[] {1L}, new long[] {1L});
+ Check.isEqual(new float[] {1f}, new float[] {1f});
+ Check.isEqual(new double[] {1d}, new double[] {1d});
+
+ l1.add("value");
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isEqual(null, l1);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(false, true);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(1, 2);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual((short) 1, (short) 2);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(1L, 2L);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(1f, 2f);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(1d, 2d);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual('a', 'b');
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual("a", "b");
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(l1, l2);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new boolean[] {true}, new boolean[] {false});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new byte[] {1}, new byte[] {2});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new char[] {'a'}, new char[] {'b'});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new short[] {1}, new short[] {2});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new int[] {1}, new int[] {2});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new long[] {1L}, new long[] {2L});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new float[] {1f}, new float[] {2f});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEqual(new double[] {1d}, new double[] {2d});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ }
+
+ @Test
+ public void testIsNotEqual() {
+ final List<String> l1 = new LinkedList<>();
+ final List<String> l2 = new LinkedList<>();
+
+ l1.add("value");
+
+ Check.isNotEqual(null, l1);
+ Check.isNotEqual(false, true);
+ Check.isNotEqual(1, 2);
+ Check.isNotEqual((short) 1, (short) 2);
+ Check.isNotEqual(1L, 2L);
+ Check.isNotEqual(1f, 2f);
+ Check.isNotEqual(1d, 2d);
+ Check.isNotEqual('a', 'b');
+ Check.isNotEqual("a", "b");
+ Check.isNotEqual(l1, l2);
+ Check.isNotEqual(new boolean[] {true}, new boolean[] {false});
+ Check.isNotEqual(new byte[] {1}, new byte[] {2});
+ Check.isNotEqual(new char[] {'a'}, new char[] {'b'});
+ Check.isNotEqual(new short[] {1}, new short[] {2});
+ Check.isNotEqual(new int[] {1}, new int[] {2});
+ Check.isNotEqual(new long[] {1L}, new long[] {2L});
+ Check.isNotEqual(new float[] {1f}, new float[] {2f});
+ Check.isNotEqual(new double[] {1d}, new double[] {2d});
+
+ l1.remove(0);
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isNotEqual((Object) null, (Object) null);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(false, false);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(1, 1);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual((short) 1, (short) 1);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(1L, 1L);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(1f, 1f);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(1d, 1d);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual('a', 'a');
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual("a", "a");
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(l1, l1);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(l1, l2);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new boolean[] {true}, new boolean[] {true});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new byte[] {1}, new byte[] {1});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new char[] {'a'}, new char[] {'a'});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new short[] {1}, new short[] {1});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new int[] {1}, new int[] {1});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new long[] {1L}, new long[] {1L});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new float[] {1f}, new float[] {1f});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEqual(new double[] {1d}, new double[] {1d});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ }
+
+ @Test
+ public void testIsNull() {
+ Check.isNull(null);
+
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isNull(new Object());
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ }
+
+ @Test
+ public void testIsNotNull() {
+ Check.isNotNull(new Object());
+
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isNotNull(null);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ }
+
+ @Test
+ public void testIsEmpty() {
+ final List<String> l = new LinkedList<>();
+
+ Check.isEmpty("");
+ Check.isEmpty(new Object[] { });
+ Check.isEmpty(new boolean[] { });
+ Check.isEmpty(new byte[] { });
+ Check.isEmpty(new char[] { });
+ Check.isEmpty(new short[] { });
+ Check.isEmpty(new int[] { });
+ Check.isEmpty(new long[] { });
+ Check.isEmpty(new float[] { });
+ Check.isEmpty(new double[] { });
+ Check.isEmpty(l);
+
+ l.add("value");
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isEmpty("value");
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new Object[] {"value"});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new boolean[] {true});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new byte[] {1});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new char[] {'a'});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new short[] {1});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new int[] {1});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new long[] {1L});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new float[] {1f});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(new double[] {1d});
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isEmpty(l);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ }
+
+ @Test
+ public void testIsNotEmpty() {
+ final List<String> l = new LinkedList<>();
+
+ l.add("value");
+
+ Check.isNotEmpty("value");
+ Check.isNotEmpty(new Object[] {"value"});
+ Check.isNotEmpty(new boolean[] {true});
+ Check.isNotEmpty(new byte[] {1});
+ Check.isNotEmpty(new char[] {'a'});
+ Check.isNotEmpty(new short[] {1});
+ Check.isNotEmpty(new int[] {1});
+ Check.isNotEmpty(new long[] {1L});
+ Check.isNotEmpty(new float[] {1f});
+ Check.isNotEmpty(new double[] {1d});
+ Check.isNotEmpty(l);
+
+ l.remove(0);
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.isNotEmpty("");
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new Object[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new boolean[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new byte[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new char[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new short[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new int[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new long[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new float[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(new double[] { });
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ try {
+ Check.isNotEmpty(l);
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+
+ }
+
+ @Test
+ public void testNotReached() {
+ boolean ranPastAssertion = false;
+
+ try {
+ Check.notReached();
+ ranPastAssertion = true;
+ } catch (final AssertionError e) { }
+ assertFalse(ranPastAssertion);
+ }
+
+ @Test
+ public void testFormatting() {
+ final String formatString = "format string %1$d %2$s";
+ try {
+ Check.isEqual(1, 2, formatString, 999, "my string");
+ } catch (final AssertionError e) {
+ assertEquals(e.getMessage(),
+ "Expected compareTo to return 0 but returned -1. [1] [2] - "
+ + "format string 999 my string");
+ }
+ }
+}
« no previous file with comments | « base/android/java/src/org/chromium/base/ThreadUtils.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698