1 /* 2 * Copyright (C) 2016 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 package com.android.settings.dashboard.conditional; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.IntentFilter; 21 import android.graphics.drawable.Icon; 22 import com.android.internal.logging.nano.MetricsProto; 23 import com.android.settings.testutils.SettingsRobolectricTestRunner; 24 import com.android.settings.TestConfig; 25 import com.android.settings.core.instrumentation.MetricsFeatureProvider; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.mockito.Mock; 30 import org.mockito.MockitoAnnotations; 31 import org.robolectric.annotation.Config; 32 import org.robolectric.RuntimeEnvironment; 33 34 import static com.google.common.truth.Truth.assertThat; 35 import static org.mockito.Matchers.any; 36 import static org.mockito.Matchers.eq; 37 import static org.mockito.Mockito.mock; 38 import static org.mockito.Mockito.never; 39 import static org.mockito.Mockito.spy; 40 import static org.mockito.Mockito.verify; 41 import static org.mockito.Mockito.when; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 45 public class ConditionTest { 46 47 @Mock 48 private ConditionManager mConditionManager; 49 @Mock 50 private MetricsFeatureProvider mMetricsFeatureProvider; 51 52 private Context mContext; 53 private TestCondition mCondition; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mContext = spy(RuntimeEnvironment.application); 59 mCondition = new TestCondition(mConditionManager, mMetricsFeatureProvider); 60 when(mConditionManager.getContext()).thenReturn(mContext); 61 } 62 63 @Test 64 public void initialize_shouldNotBeSilenced() { 65 assertThat(mCondition.isSilenced()).isFalse(); 66 } 67 68 @Test 69 public void silence_shouldNotifyDataChangeAndLog() { 70 mCondition.silence(); 71 72 assertThat(mCondition.isSilenced()).isTrue(); 73 verify(mConditionManager).notifyChanged(mCondition); 74 verify(mMetricsFeatureProvider).action(any(Context.class), 75 eq(MetricsProto.MetricsEvent.ACTION_SETTINGS_CONDITION_DISMISS), 76 eq(TestCondition.TEST_METRIC_CONSTANT)); 77 } 78 79 @Test 80 public void onSilenceChanged_silenced_shouldRegisterReceiver() { 81 mCondition.onSilenceChanged(true); 82 83 verify(mContext).registerReceiver( 84 TestCondition.mReceiver, TestCondition.TESTS_INTENT_FILTER); 85 } 86 87 @Test 88 public void onSilenceChanged_notSilenced_registered_shouldUnregisterReceiver() { 89 mCondition.onSilenceChanged(true); 90 91 mCondition.onSilenceChanged(false); 92 93 verify(mContext).unregisterReceiver(TestCondition.mReceiver); 94 } 95 96 @Test 97 public void onSilenceChanged_notSilenced_notRegistered_shouldNotCrash() { 98 mCondition.onSilenceChanged(false); 99 100 verify(mContext, never()).unregisterReceiver(TestCondition.mReceiver); 101 // no crash 102 } 103 104 private static final class TestCondition extends Condition { 105 106 private static final int TEST_METRIC_CONSTANT = 1234; 107 private static final IntentFilter TESTS_INTENT_FILTER = new IntentFilter("TestIntent"); 108 private static final BroadcastReceiver mReceiver = mock(BroadcastReceiver.class); 109 110 TestCondition(ConditionManager manager, 111 MetricsFeatureProvider metricsFeatureProvider) { 112 super(manager, metricsFeatureProvider); 113 } 114 115 @Override 116 public void refreshState() { 117 118 } 119 120 @Override 121 public int getMetricsConstant() { 122 return TEST_METRIC_CONSTANT; 123 } 124 125 @Override 126 public Icon getIcon() { 127 return null; 128 } 129 130 @Override 131 public CharSequence getTitle() { 132 return null; 133 } 134 135 @Override 136 public CharSequence getSummary() { 137 return null; 138 } 139 140 @Override 141 public CharSequence[] getActions() { 142 return new CharSequence[0]; 143 } 144 145 @Override 146 public void onPrimaryClick() { 147 148 } 149 150 @Override 151 public void onActionClick(int index) { 152 153 } 154 155 @Override 156 public BroadcastReceiver getReceiver() { 157 return mReceiver; 158 } 159 160 @Override 161 public IntentFilter getIntentFilter() { 162 return TESTS_INTENT_FILTER; 163 } 164 165 } 166 } 167