• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.settings.accessibility;
18 
19 import static com.android.settings.accessibility.FlashNotificationsUtil.ACTION_FLASH_NOTIFICATION_START_PREVIEW;
20 import static com.android.settings.accessibility.FlashNotificationsUtil.EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE;
21 import static com.android.settings.accessibility.FlashNotificationsUtil.TYPE_LONG_PREVIEW;
22 import static com.android.settings.accessibility.FlashNotificationsUtil.TYPE_SHORT_PREVIEW;
23 import static com.android.settings.accessibility.ShadowFlashNotificationsUtils.setFlashNotificationsState;
24 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import static org.mockito.ArgumentMatchers.any;
29 import static org.mockito.ArgumentMatchers.anyBoolean;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 
35 import android.content.ContentResolver;
36 import android.content.Context;
37 import android.content.Intent;
38 import android.provider.Settings;
39 import android.view.View;
40 
41 import androidx.lifecycle.Lifecycle;
42 import androidx.lifecycle.LifecycleOwner;
43 import androidx.preference.PreferenceScreen;
44 import androidx.preference.PreferenceViewHolder;
45 import androidx.test.core.app.ApplicationProvider;
46 
47 import com.android.settingslib.widget.ButtonPreference;
48 
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Rule;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.ArgumentCaptor;
55 import org.mockito.Mock;
56 import org.mockito.Spy;
57 import org.mockito.junit.MockitoJUnit;
58 import org.mockito.junit.MockitoRule;
59 import org.robolectric.RobolectricTestRunner;
60 import org.robolectric.annotation.Config;
61 
62 @RunWith(RobolectricTestRunner.class)
63 @Config(shadows = ShadowFlashNotificationsUtils.class)
64 public class FlashNotificationsPreviewPreferenceControllerTest {
65     private static final String PREFERENCE_KEY = "preference_key";
66 
67     @Rule
68     public MockitoRule mMockitoRule = MockitoJUnit.rule();
69     @Spy
70     private final Context mContext = ApplicationProvider.getApplicationContext();
71     @Spy
72     private ContentResolver mContentResolver = mContext.getContentResolver();
73     @Mock
74     private PreferenceScreen mPreferenceScreen;
75     private ButtonPreference mPreference;
76     private FlashNotificationsPreviewPreferenceController mController;
77 
78     @Before
setUp()79     public void setUp() {
80         when(mContext.getContentResolver()).thenReturn(mContentResolver);
81 
82         mPreference = new ButtonPreference(mContext);
83         mPreference.setKey(PREFERENCE_KEY);
84         final View rootView = View.inflate(mContext, mPreference.getLayoutResource(), null);
85         mPreference.onBindViewHolder(PreferenceViewHolder.createInstanceForTests(rootView));
86         when(mPreferenceScreen.findPreference(PREFERENCE_KEY)).thenReturn(mPreference);
87 
88         mController = new FlashNotificationsPreviewPreferenceController(mContext, PREFERENCE_KEY);
89         mController.displayPreference(mPreferenceScreen);
90     }
91 
92     @After
tearDown()93     public void tearDown() {
94         ShadowFlashNotificationsUtils.reset();
95     }
96 
97     @Test
testGetAvailabilityStatus()98     public void testGetAvailabilityStatus() {
99         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
100     }
101 
102     @Test
updateState_cameraOff_screenOff_notVisible()103     public void updateState_cameraOff_screenOff_notVisible() {
104         setFlashNotificationsState(FlashNotificationsUtil.State.OFF);
105 
106         mController.updateState(mPreference);
107 
108         assertThat(mPreference.isVisible()).isFalse();
109     }
110 
111     @Test
updateState_cameraOn_screenOff_isVisible()112     public void updateState_cameraOn_screenOff_isVisible() {
113         setFlashNotificationsState(FlashNotificationsUtil.State.CAMERA);
114 
115         mController.updateState(mPreference);
116 
117         assertThat(mPreference.isVisible()).isTrue();
118     }
119 
120     @Test
updateState_cameraOff_screenOn_isVisible()121     public void updateState_cameraOff_screenOn_isVisible() {
122         setFlashNotificationsState(FlashNotificationsUtil.State.SCREEN);
123 
124         mController.updateState(mPreference);
125 
126         assertThat(mPreference.isVisible()).isTrue();
127     }
128 
129     @Test
updateState_cameraOn_screenOn_isVisible()130     public void updateState_cameraOn_screenOn_isVisible() {
131         setFlashNotificationsState(FlashNotificationsUtil.State.CAMERA_SCREEN);
132 
133         mController.updateState(mPreference);
134 
135         assertThat(mPreference.isVisible()).isTrue();
136     }
137 
138     @Test
clickOnButton_assertAction()139     public void clickOnButton_assertAction() {
140         mPreference.getButton().callOnClick();
141 
142         ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
143         verify(mContext).sendBroadcastAsUser(captor.capture(), any());
144         Intent captured = captor.getValue();
145         assertThat(captured.getAction()).isEqualTo(ACTION_FLASH_NOTIFICATION_START_PREVIEW);
146     }
147 
148     @Test
clickOnButton_assertExtra()149     public void clickOnButton_assertExtra() {
150         mPreference.getButton().callOnClick();
151 
152         ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
153         verify(mContext).sendBroadcastAsUser(captor.capture(), any());
154         Intent captured = captor.getValue();
155         assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_LONG_PREVIEW))
156                 .isEqualTo(TYPE_SHORT_PREVIEW);
157     }
158 
159     @Test
onStateChanged_onResume_cameraUri_verifyRegister()160     public void onStateChanged_onResume_cameraUri_verifyRegister() {
161         mController.onStateChanged(mock(LifecycleOwner.class), Lifecycle.Event.ON_RESUME);
162 
163         verify(mContentResolver).registerContentObserver(
164                 eq(Settings.System.getUriFor(Settings.System.CAMERA_FLASH_NOTIFICATION)),
165                 anyBoolean(), eq(mController.mContentObserver));
166     }
167 
168     @Test
onStateChanged_onResume_screenUri_verifyRegister()169     public void onStateChanged_onResume_screenUri_verifyRegister() {
170         mController.onStateChanged(mock(LifecycleOwner.class), Lifecycle.Event.ON_RESUME);
171 
172         verify(mContentResolver).registerContentObserver(
173                 eq(Settings.System.getUriFor(Settings.System.SCREEN_FLASH_NOTIFICATION)),
174                 anyBoolean(), eq(mController.mContentObserver));
175     }
176 
177     @Test
onStateChanged_onPause_verifyUnregister()178     public void onStateChanged_onPause_verifyUnregister() {
179         mController.onStateChanged(mock(LifecycleOwner.class), Lifecycle.Event.ON_PAUSE);
180 
181         verify(mContentResolver).unregisterContentObserver(eq(mController.mContentObserver));
182     }
183 }
184