OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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.base.test.util; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.net.ConnectivityManager; |
| 9 import android.net.NetworkInfo; |
| 10 import android.text.TextUtils; |
| 11 |
| 12 import junit.framework.TestCase; |
| 13 |
| 14 import org.chromium.base.Log; |
| 15 import org.chromium.base.SysUtils; |
| 16 |
| 17 import java.lang.reflect.Method; |
| 18 |
| 19 /** |
| 20 * Checks if any restrictions exist and skip the test if it meets those restrict
ions. |
| 21 */ |
| 22 public class RestrictionSkipCheck extends SkipCheck { |
| 23 |
| 24 private static final String TAG = "base_test"; |
| 25 |
| 26 private final Context mTargetContext; |
| 27 |
| 28 public RestrictionSkipCheck(Context targetContext) { |
| 29 mTargetContext = targetContext; |
| 30 } |
| 31 |
| 32 @Override |
| 33 public boolean shouldSkip(TestCase testCase) { |
| 34 Method method = getTestMethod(testCase); |
| 35 if (method == null) return true; |
| 36 |
| 37 for (Restriction restriction : getAnnotations(method, Restriction.class)
) { |
| 38 for (String restrictionVal : restriction.value()) { |
| 39 if (restrictionApplies(restrictionVal)) { |
| 40 Log.i(TAG, "Test " + testCase.getClass().getName() + "#" |
| 41 + testCase.getName() + " skipped because of restrict
ion " |
| 42 + restriction); |
| 43 return true; |
| 44 } |
| 45 } |
| 46 } |
| 47 return false; |
| 48 } |
| 49 |
| 50 protected boolean restrictionApplies(String restriction) { |
| 51 if (TextUtils.equals(restriction, Restriction.RESTRICTION_TYPE_LOW_END_D
EVICE) |
| 52 && !SysUtils.isLowEndDevice()) { |
| 53 return true; |
| 54 } |
| 55 if (TextUtils.equals(restriction, Restriction.RESTRICTION_TYPE_NON_LOW_E
ND_DEVICE) |
| 56 && SysUtils.isLowEndDevice()) { |
| 57 return true; |
| 58 } |
| 59 if (TextUtils.equals(restriction, Restriction.RESTRICTION_TYPE_INTERNET) |
| 60 && !isNetworkAvailable()) { |
| 61 return true; |
| 62 } |
| 63 return false; |
| 64 } |
| 65 |
| 66 private boolean isNetworkAvailable() { |
| 67 final ConnectivityManager connectivityManager = (ConnectivityManager) |
| 68 mTargetContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 69 final NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetwo
rkInfo(); |
| 70 return activeNetworkInfo != null && activeNetworkInfo.isConnected(); |
| 71 } |
| 72 } |
OLD | NEW |