OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.test; |
| 6 |
| 7 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_LOW_END_D
EVICE; |
| 8 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_NON_LOW_E
ND_DEVICE; |
| 9 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_PHONE; |
| 10 import static org.chromium.base.test.util.Restriction.RESTRICTION_TYPE_TABLET; |
| 11 |
| 12 import android.test.InstrumentationTestCase; |
| 13 import android.text.TextUtils; |
| 14 |
| 15 import org.chromium.base.SysUtils; |
| 16 import org.chromium.base.test.util.Restriction; |
| 17 import org.chromium.ui.base.DeviceFormFactor; |
| 18 |
| 19 import java.lang.reflect.Method; |
| 20 |
| 21 /** |
| 22 * A version of {@link InstrumentationTestCase} that checks the {@link Restricti
on} annotation |
| 23 * before running each test. |
| 24 */ |
| 25 public class RestrictedInstrumentationTestCase extends InstrumentationTestCase { |
| 26 /** |
| 27 * Whether a test method should run based on the Restriction annotation. Th
is will test the |
| 28 * current test method being run by checking {@link InstrumentationTestCase#
getName()}. |
| 29 * @param testCase The instrumentationTestCase to check against. |
| 30 * @throws Exception |
| 31 */ |
| 32 public static boolean shouldRunTest(InstrumentationTestCase testCase) |
| 33 throws Exception { |
| 34 Method method = testCase.getClass().getMethod(testCase.getName(), (Class
[]) null); |
| 35 Restriction restrictions = method.getAnnotation(Restriction.class); |
| 36 if (restrictions != null) { |
| 37 for (String restriction : restrictions.value()) { |
| 38 if (TextUtils.equals(restriction, RESTRICTION_TYPE_PHONE) |
| 39 && DeviceFormFactor.isTablet( |
| 40 testCase.getInstrumentation().getTargetContext()
)) { |
| 41 return false; |
| 42 } |
| 43 if (TextUtils.equals(restriction, RESTRICTION_TYPE_TABLET) |
| 44 && !DeviceFormFactor.isTablet( |
| 45 testCase.getInstrumentation().getTargetContext()
)) { |
| 46 return false; |
| 47 } |
| 48 if (TextUtils.equals(restriction, RESTRICTION_TYPE_LOW_END_DEVIC
E) |
| 49 && !SysUtils.isLowEndDevice()) { |
| 50 return false; |
| 51 } |
| 52 if (TextUtils.equals(restriction, RESTRICTION_TYPE_NON_LOW_END_D
EVICE) |
| 53 && SysUtils.isLowEndDevice()) { |
| 54 return false; |
| 55 } |
| 56 } |
| 57 } |
| 58 return true; |
| 59 } |
| 60 |
| 61 @Override |
| 62 public void runTest() throws Throwable { |
| 63 boolean shouldRun = true; |
| 64 try { |
| 65 shouldRun = shouldRunTest(this); |
| 66 } catch (Exception e) { |
| 67 // eat the exception here; super.runTest() will catch it again and h
andle it properly |
| 68 } |
| 69 |
| 70 if (shouldRun) super.runTest(); |
| 71 } |
| 72 } |
OLD | NEW |