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 
     23 import com.android.tv.common.TvCommonUtils;
     24 
     25 /**
     26  * When run in a test harness this feature can be turned on or off, overriding the normal value.
     27  *
     28  * <p><b>Warning</b> making a feature testable will cause the code to stay in the APK and
     29  * could leak unreleased features.
     30  */
     31 public class TestableFeature implements Feature {
     32     private final static String TAG = "TestableFeature";
     33     private final static String DETAIL_MESSAGE
     34             = "TestableFeatures should only be changed in tests.";
     35 
     36     private final Feature mDelegate;
     37     private Boolean mTestValue = null;
     38 
     39     public static TestableFeature createTestableFeature(Feature delegate) {
     40         return new TestableFeature(delegate);
     41     }
     42 
     43     private TestableFeature(Feature delegate) {
     44         mDelegate = delegate;
     45     }
     46 
     47     @VisibleForTesting
     48     public void enableForTest() {
     49         if (!TvCommonUtils.isRunningInTest()) {
     50             Log.e(TAG, "Not enabling for test:" + this,
     51                     new IllegalStateException(DETAIL_MESSAGE));
     52         } else {
     53             mTestValue = true;
     54         }
     55     }
     56 
     57     @VisibleForTesting
     58     public void disableForTests() {
     59         if (!TvCommonUtils.isRunningInTest()) {
     60             Log.e(TAG, "Not disabling for test: " + this,
     61                     new IllegalStateException(DETAIL_MESSAGE));
     62         } else {
     63             mTestValue = false;
     64         }
     65     }
     66 
     67     @VisibleForTesting
     68     public void resetForTests() {
     69         if (!TvCommonUtils.isRunningInTest()) {
     70             Log.e(TAG, "Not resetting feature: " + this, new IllegalStateException(DETAIL_MESSAGE));
     71         } else {
     72             mTestValue = null;
     73         }
     74     }
     75 
     76     @Override
     77     public boolean isEnabled(Context context) {
     78         if (TvCommonUtils.isRunningInTest() && mTestValue != null) {
     79             return mTestValue;
     80         }
     81         return mDelegate.isEnabled(context);
     82     }
     83 
     84     @Override
     85     public String toString() {
     86         String msg = mDelegate.toString();
     87         if (TvCommonUtils.isRunningInTest()) {
     88             if (mTestValue == null) {
     89                 msg = "Testable Feature is unchanged: " + msg;
     90             } else {
     91                 msg = "Testable Feature is " + (mTestValue ? "on" : "off") + " was " + msg;
     92             }
     93         }
     94         return msg;
     95     }
     96 }
     97