• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.cellbroadcastreceiver.unit;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.anyString;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.SharedPreferences;
29 import android.media.AudioManager;
30 import android.os.HandlerThread;
31 import android.preference.PreferenceManager;
32 
33 import com.android.cellbroadcastreceiver.CellBroadcastAlertReminder;
34 import com.android.cellbroadcastreceiver.CellBroadcastSettings;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 
41 public class CellBroadcastAlertReminderTest extends
42         CellBroadcastServiceTestCase<CellBroadcastAlertReminder> {
43 
44     private static final long MAX_INIT_WAIT_MS = 5000;
45 
46     @Mock
47     private SharedPreferences mSharedPreferences;
48     @Mock
49     private Context mMockContext;
50 
51     private Object mLock = new Object();
52     private boolean mReady;
53 
CellBroadcastAlertReminderTest()54     public CellBroadcastAlertReminderTest() {
55         super(CellBroadcastAlertReminder.class);
56     }
57 
58     private class PhoneStateListenerHandler extends HandlerThread {
59 
60         private Runnable mFunction;
61 
PhoneStateListenerHandler(String name, Runnable func)62         PhoneStateListenerHandler(String name, Runnable func) {
63             super(name);
64             mFunction = func;
65         }
66 
67         @Override
onLooperPrepared()68         public void onLooperPrepared() {
69             mFunction.run();
70             setReady(true);
71         }
72     }
73 
waitUntilReady()74     protected void waitUntilReady() {
75         synchronized (mLock) {
76             if (!mReady) {
77                 try {
78                     mLock.wait(MAX_INIT_WAIT_MS);
79                 } catch (InterruptedException ie) {
80                 }
81 
82                 if (!mReady) {
83                     fail("Telephony tests failed to initialize");
84                 }
85             }
86         }
87     }
88 
setReady(boolean ready)89     protected void setReady(boolean ready) {
90         synchronized (mLock) {
91             mReady = ready;
92             mLock.notifyAll();
93         }
94     }
95 
96     @Before
setUp()97     public void setUp() throws Exception {
98         super.setUp();
99         MockitoAnnotations.initMocks(this);
100     }
101 
102     @After
tearDown()103     public void tearDown() throws Exception {
104         super.tearDown();
105     }
106 
107     /**
108      * When the reminder is set to vibrate, vibrate method should be called once.
109      */
testStartServiceVibrate()110     public void testStartServiceVibrate() throws Throwable {
111         PhoneStateListenerHandler phoneStateListenerHandler = new PhoneStateListenerHandler(
112                 "testStartServiceVibrate",
113                 () -> {
114                     Intent intent = new Intent(mContext, CellBroadcastAlertReminder.class);
115                     intent.setAction(CellBroadcastAlertReminder.ACTION_PLAY_ALERT_REMINDER);
116                     intent.putExtra(CellBroadcastAlertReminder.ALERT_REMINDER_VIBRATE_EXTRA,
117                             true);
118                     startService(intent);
119                 });
120         phoneStateListenerHandler.start();
121         waitUntilReady();
122 
123         verify(mMockedVibrator).vibrate(any());
124         phoneStateListenerHandler.quit();
125     }
126 
127     /**
128      * When the reminder is not set to vibrate, vibrate method should not be called.
129      */
testStartServiceNotVibrate()130     public void testStartServiceNotVibrate() throws Throwable {
131         PhoneStateListenerHandler phoneStateListenerHandler = new PhoneStateListenerHandler(
132                 "testStartServiceNotVibrate",
133                 () -> {
134                     Intent intent = new Intent(mContext, CellBroadcastAlertReminder.class);
135                     intent.setAction(CellBroadcastAlertReminder.ACTION_PLAY_ALERT_REMINDER);
136                     intent.putExtra(CellBroadcastAlertReminder.ALERT_REMINDER_VIBRATE_EXTRA,
137                             false);
138                     startService(intent);
139                 });
140         phoneStateListenerHandler.start();
141         waitUntilReady();
142 
143         verify(mMockedVibrator, never()).vibrate(any());
144         phoneStateListenerHandler.quit();
145     }
146 
testStartServicePlayAlertReminderForWatch()147     public void testStartServicePlayAlertReminderForWatch() {
148         setWatchFeatureEnabled(true);
149         doReturn("1").when(PreferenceManager.getDefaultSharedPreferences(mContext)).getString(
150                 CellBroadcastSettings.KEY_ALERT_REMINDER_INTERVAL, null);
151         doReturn(1).when(mMockedAudioManager).getStreamVolume(anyInt());
152         doReturn(AudioManager.RINGER_MODE_NORMAL).when(
153                 mMockedAudioManager).getRingerMode();
154         PhoneStateListenerHandler listenerHandler = new PhoneStateListenerHandler(
155                 "testStartServicePlayAlertReminderForWatch",
156                 () -> {
157                     Intent intent = new Intent(mContext, CellBroadcastAlertReminder.class);
158                     intent.setAction(CellBroadcastAlertReminder.ACTION_PLAY_ALERT_REMINDER);
159                     intent.putExtra(CellBroadcastAlertReminder.ALERT_REMINDER_VIBRATE_EXTRA,
160                             false);
161                     startService(intent);
162                 });
163 
164         listenerHandler.start();
165         waitUntilReady();
166 
167         verify(mMockedAudioManager).getStreamVolume(AudioManager.STREAM_ALARM);
168         listenerHandler.quit();
169     }
170 
testQueueAlertReminderReturnFalseIfIntervalNull()171     public void testQueueAlertReminderReturnFalseIfIntervalNull() {
172         doReturn(mSharedPreferences).when(mMockContext).getSharedPreferences(anyString(), anyInt());
173         doReturn(null).when(mSharedPreferences).getString(
174                 CellBroadcastSettings.KEY_ALERT_REMINDER_INTERVAL, null);
175         assertFalse(CellBroadcastAlertReminder.queueAlertReminder(mMockContext, 1, true));
176     }
177 
testQueueAlertReminderReturnFalseIfIntervalStringNotANumber()178     public void testQueueAlertReminderReturnFalseIfIntervalStringNotANumber() {
179         doReturn(mSharedPreferences).when(mMockContext).getSharedPreferences(anyString(), anyInt());
180         doReturn("NotANumber").when(mSharedPreferences).getString(
181                 CellBroadcastSettings.KEY_ALERT_REMINDER_INTERVAL, null);
182         assertFalse(CellBroadcastAlertReminder.queueAlertReminder(mMockContext, 1, true));
183     }
184 
testQueueAlertReminderReturnFalseIfIntervalZero()185     public void testQueueAlertReminderReturnFalseIfIntervalZero() {
186         doReturn(mSharedPreferences).when(mMockContext).getSharedPreferences(anyString(), anyInt());
187         doReturn("0").when(mSharedPreferences).getString(
188                 CellBroadcastSettings.KEY_ALERT_REMINDER_INTERVAL, null);
189         assertFalse(CellBroadcastAlertReminder.queueAlertReminder(mMockContext, 1, true));
190     }
191 
testQueueAlertReminderReturnFalseIfIntervalOneButNotFirstTime()192     public void testQueueAlertReminderReturnFalseIfIntervalOneButNotFirstTime() {
193         doReturn(mSharedPreferences).when(mMockContext).getSharedPreferences(anyString(), anyInt());
194         doReturn("1").when(mSharedPreferences).getString(
195                 CellBroadcastSettings.KEY_ALERT_REMINDER_INTERVAL, null);
196         assertFalse(CellBroadcastAlertReminder.queueAlertReminder(mMockContext, 1, false));
197     }
198 }
199