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 /** 40 * Creates testable feature. 41 */ 42 public static TestableFeature createTestableFeature(Feature delegate) { 43 return new TestableFeature(delegate); 44 } 45 46 /** 47 * Creates testable feature with initial value. 48 */ 49 public static TestableFeature createTestableFeature(Feature delegate, Boolean initialValue) { 50 return new TestableFeature(delegate, initialValue); 51 } 52 53 private TestableFeature(Feature delegate) { 54 mDelegate = delegate; 55 } 56 57 private TestableFeature(Feature delegate, Boolean initialValue) { 58 mDelegate = delegate; 59 mTestValue = initialValue; 60 } 61 62 @VisibleForTesting 63 public void enableForTest() { 64 if (!TvCommonUtils.isRunningInTest()) { 65 Log.e(TAG, "Not enabling for test:" + this, 66 new IllegalStateException(DETAIL_MESSAGE)); 67 } else { 68 mTestValue = true; 69 } 70 } 71 72 @VisibleForTesting 73 public void disableForTests() { 74 if (!TvCommonUtils.isRunningInTest()) { 75 Log.e(TAG, "Not disabling for test: " + this, 76 new IllegalStateException(DETAIL_MESSAGE)); 77 } else { 78 mTestValue = false; 79 } 80 } 81 82 @VisibleForTesting 83 public void resetForTests() { 84 if (!TvCommonUtils.isRunningInTest()) { 85 Log.e(TAG, "Not resetting feature: " + this, new IllegalStateException(DETAIL_MESSAGE)); 86 } else { 87 mTestValue = null; 88 } 89 } 90 91 @Override 92 public boolean isEnabled(Context context) { 93 if (TvCommonUtils.isRunningInTest() && mTestValue != null) { 94 return mTestValue; 95 } 96 return mDelegate.isEnabled(context); 97 } 98 99 @Override 100 public String toString() { 101 String msg = mDelegate.toString(); 102 if (TvCommonUtils.isRunningInTest()) { 103 if (mTestValue == null) { 104 msg = "Testable Feature is unchanged: " + msg; 105 } else { 106 msg = "Testable Feature is " + (mTestValue ? "on" : "off") + " was " + msg; 107 } 108 } 109 return msg; 110 } 111 } 112