Home | History | Annotate | Download | only in feature
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 
     17 package com.android.tv.common.feature;
     18 
     19 import android.content.Context;
     20 import android.support.annotation.VisibleForTesting;
     21 import android.util.Log;
     22 import com.android.tv.common.util.CommonUtils;
     23 
     24 /**
     25  * When run in a test harness this feature can be turned on or off, overriding the normal value.
     26  *
     27  * <p><b>Warning</b> making a feature testable will cause the code to stay in the APK and could leak
     28  * unreleased features.
     29  */
     30 public class TestableFeature implements Feature {
     31     private static final String TAG = "TestableFeature";
     32     private static final String DETAIL_MESSAGE =
     33             "TestableFeatures should only be changed in tests.";
     34 
     35     private final Feature mDelegate;
     36     private Boolean mTestValue = null;
     37 
     38     /** Creates testable feature. */
     39     public static TestableFeature createTestableFeature(Feature delegate) {
     40         return new TestableFeature(delegate);
     41     }
     42 
     43     /** Creates testable feature with initial value. */
     44     public static TestableFeature createTestableFeature(Feature delegate, Boolean initialValue) {
     45         return new TestableFeature(delegate, initialValue);
     46     }
     47 
     48     private TestableFeature(Feature delegate) {
     49         mDelegate = delegate;
     50     }
     51 
     52     private TestableFeature(Feature delegate, Boolean initialValue) {
     53         mDelegate = delegate;
     54         mTestValue = initialValue;
     55     }
     56 
     57     @VisibleForTesting
     58     public void enableForTest() {
     59         if (!CommonUtils.isRunningInTest()) {
     60             Log.e(TAG, "Not enabling for test:" + this, new IllegalStateException(DETAIL_MESSAGE));
     61         } else {
     62             mTestValue = true;
     63         }
     64     }
     65 
     66     @VisibleForTesting
     67     public void disableForTests() {
     68         if (!CommonUtils.isRunningInTest()) {
     69             Log.e(
     70                     TAG,
     71                     "Not disabling for test: " + this,
     72                     new IllegalStateException(DETAIL_MESSAGE));
     73         } else {
     74             mTestValue = false;
     75         }
     76     }
     77 
     78     @VisibleForTesting
     79     public void resetForTests() {
     80         if (!CommonUtils.isRunningInTest()) {
     81             Log.e(TAG, "Not resetting feature: " + this, new IllegalStateException(DETAIL_MESSAGE));
     82         } else {
     83             mTestValue = null;
     84         }
     85     }
     86 
     87     @Override
     88     public boolean isEnabled(Context context) {
     89         if (CommonUtils.isRunningInTest() && mTestValue != null) {
     90             return mTestValue;
     91         }
     92         return mDelegate.isEnabled(context);
     93     }
     94 
     95     @Override
     96     public String toString() {
     97         String msg = mDelegate.toString();
     98         if (CommonUtils.isRunningInTest()) {
     99             if (mTestValue == null) {
    100                 msg = "Testable Feature is unchanged: " + msg;
    101             } else {
    102                 msg = "Testable Feature is " + (mTestValue ? "on" : "off") + " was " + msg;
    103             }
    104         }
    105         return msg;
    106     }
    107 }
    108