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

Unified Diff: chrome/test/android/javatests_staging/src/org/chromium/chrome/test/RestrictedInstrumentationTestCase.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 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
Index: chrome/test/android/javatests_staging/src/org/chromium/chrome/test/RestrictedInstrumentationTestCase.java
diff --git a/chrome/test/android/javatests_staging/src/org/chromium/chrome/test/RestrictedInstrumentationTestCase.java b/chrome/test/android/javatests_staging/src/org/chromium/chrome/test/RestrictedInstrumentationTestCase.java
new file mode 100644
index 0000000000000000000000000000000000000000..359b48212fa453eff237ba7fa9a4f2124eef775e
--- /dev/null
+++ b/chrome/test/android/javatests_staging/src/org/chromium/chrome/test/RestrictedInstrumentationTestCase.java
@@ -0,0 +1,72 @@
+// 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.chrome.test;
+
+import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_LOW_END_DEVICE;
+import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_END_DEVICE;
+import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_PHONE;
+import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_TABLET;
+
+import android.test.InstrumentationTestCase;
+import android.text.TextUtils;
+
+import org.chromium.base.SysUtils;
+import org.chromium.base.test.util.Restriction;
+import org.chromium.ui.base.DeviceFormFactor;
+
+import java.lang.reflect.Method;
+
+/**
+ * A version of {@link InstrumentationTestCase} that checks the {@link Restriction} annotation
+ * before running each test.
+ */
+public class RestrictedInstrumentationTestCase extends InstrumentationTestCase {
+ /**
+ * Whether a test method should run based on the Restriction annotation. This will test the
+ * current test method being run by checking {@link InstrumentationTestCase#getName()}.
+ * @param testCase The instrumentationTestCase to check against.
+ * @throws Exception
+ */
+ public static boolean shouldRunTest(InstrumentationTestCase testCase)
+ throws Exception {
+ Method method = testCase.getClass().getMethod(testCase.getName(), (Class[]) null);
+ Restriction restrictions = method.getAnnotation(Restriction.class);
+ if (restrictions != null) {
+ for (String restriction : restrictions.value()) {
+ if (TextUtils.equals(restriction, RESTRICTION_TYPE_PHONE)
+ && DeviceFormFactor.isTablet(
+ testCase.getInstrumentation().getTargetContext())) {
+ return false;
+ }
+ if (TextUtils.equals(restriction, RESTRICTION_TYPE_TABLET)
+ && !DeviceFormFactor.isTablet(
+ testCase.getInstrumentation().getTargetContext())) {
+ return false;
+ }
+ if (TextUtils.equals(restriction, RESTRICTION_TYPE_LOW_END_DEVICE)
+ && !SysUtils.isLowEndDevice()) {
+ return false;
+ }
+ if (TextUtils.equals(restriction, RESTRICTION_TYPE_NON_LOW_END_DEVICE)
+ && SysUtils.isLowEndDevice()) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public void runTest() throws Throwable {
+ boolean shouldRun = true;
+ try {
+ shouldRun = shouldRunTest(this);
+ } catch (Exception e) {
+ // eat the exception here; super.runTest() will catch it again and handle it properly
+ }
+
+ if (shouldRun) super.runTest();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698