blob: 033f929664fea2b3dd75bad54c87828409243ef9 [file] [log] [blame]
Santos Cordonf78a72f2016-01-21 22:10:32 +00001/*
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
17package com.android.server.telecom.tests;
18
Hall Liuc8a396b2017-12-27 18:23:28 -080019import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertTrue;
Santos Cordonf78a72f2016-01-21 22:10:32 +000022import static org.mockito.Matchers.any;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.app.UiModeManager;
27import android.content.BroadcastReceiver;
28import android.content.Context;
29import android.content.Intent;
30import android.content.IntentFilter;
31import android.content.res.Configuration;
Hall Liua3799ae2016-03-16 11:37:51 -070032import android.test.suitebuilder.annotation.SmallTest;
Santos Cordonf78a72f2016-01-21 22:10:32 +000033
34import com.android.server.telecom.SystemStateProvider;
35import com.android.server.telecom.SystemStateProvider.SystemStateListener;
36
Hall Liuc8a396b2017-12-27 18:23:28 -080037import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.junit.runners.JUnit4;
Santos Cordonf78a72f2016-01-21 22:10:32 +000042import org.mockito.ArgumentCaptor;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46/**
47 * Unit tests for SystemStateProvider
48 */
Hall Liuc8a396b2017-12-27 18:23:28 -080049@RunWith(JUnit4.class)
Santos Cordonf78a72f2016-01-21 22:10:32 +000050public class SystemStateProviderTest extends TelecomTestCase {
51
Santos Cordonf78a72f2016-01-21 22:10:32 +000052 @Mock Context mContext;
53 @Mock SystemStateListener mSystemStateListener;
54 @Mock UiModeManager mUiModeManager;
55 @Mock Intent mIntentEnter;
56 @Mock Intent mIntentExit;
57
58 @Override
Hall Liuc8a396b2017-12-27 18:23:28 -080059 @Before
Santos Cordonf78a72f2016-01-21 22:10:32 +000060 public void setUp() throws Exception {
61 super.setUp();
62 MockitoAnnotations.initMocks(this);
63 }
64
65 @Override
Hall Liuc8a396b2017-12-27 18:23:28 -080066 @After
Santos Cordonf78a72f2016-01-21 22:10:32 +000067 public void tearDown() throws Exception {
68 super.tearDown();
69 }
70
Hall Liua3799ae2016-03-16 11:37:51 -070071 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -080072 @Test
Santos Cordonf78a72f2016-01-21 22:10:32 +000073 public void testListeners() throws Exception {
74 SystemStateProvider systemStateProvider = new SystemStateProvider(mContext);
75
76 assertFalse(systemStateProvider.removeListener(mSystemStateListener));
77 systemStateProvider.addListener(mSystemStateListener);
78 assertTrue(systemStateProvider.removeListener(mSystemStateListener));
79 assertFalse(systemStateProvider.removeListener(mSystemStateListener));
80 }
81
Hall Liua3799ae2016-03-16 11:37:51 -070082 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -080083 @Test
Santos Cordonf78a72f2016-01-21 22:10:32 +000084 public void testQuerySystemForCarMode_True() {
85 when(mContext.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(mUiModeManager);
86 when(mUiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_CAR);
87 assertTrue(new SystemStateProvider(mContext).isCarMode());
88 }
89
Hall Liua3799ae2016-03-16 11:37:51 -070090 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -080091 @Test
Santos Cordonf78a72f2016-01-21 22:10:32 +000092 public void testQuerySystemForCarMode_False() {
93 when(mContext.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(mUiModeManager);
94 when(mUiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_NORMAL);
95 assertFalse(new SystemStateProvider(mContext).isCarMode());
96 }
97
Hall Liua3799ae2016-03-16 11:37:51 -070098 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -080099 @Test
Santos Cordonf78a72f2016-01-21 22:10:32 +0000100 public void testReceiverAndIntentFilter() {
101 ArgumentCaptor<IntentFilter> intentFilter = ArgumentCaptor.forClass(IntentFilter.class);
102 new SystemStateProvider(mContext);
103 verify(mContext).registerReceiver(any(BroadcastReceiver.class), intentFilter.capture());
104
105 assertEquals(2, intentFilter.getValue().countActions());
106 assertEquals(UiModeManager.ACTION_ENTER_CAR_MODE, intentFilter.getValue().getAction(0));
107 assertEquals(UiModeManager.ACTION_EXIT_CAR_MODE, intentFilter.getValue().getAction(1));
108 }
109
Hall Liua3799ae2016-03-16 11:37:51 -0700110 @SmallTest
Hall Liuc8a396b2017-12-27 18:23:28 -0800111 @Test
Santos Cordonf78a72f2016-01-21 22:10:32 +0000112 public void testOnEnterExitCarMode() {
113 ArgumentCaptor<BroadcastReceiver> receiver =
114 ArgumentCaptor.forClass(BroadcastReceiver.class);
115 new SystemStateProvider(mContext).addListener(mSystemStateListener);
116
117 verify(mContext).registerReceiver(receiver.capture(), any(IntentFilter.class));
118
119 when(mIntentEnter.getAction()).thenReturn(UiModeManager.ACTION_ENTER_CAR_MODE);
120 receiver.getValue().onReceive(mContext, mIntentEnter);
121 verify(mSystemStateListener).onCarModeChanged(true);
122
123 when(mIntentExit.getAction()).thenReturn(UiModeManager.ACTION_EXIT_CAR_MODE);
124 receiver.getValue().onReceive(mContext, mIntentExit);
125 verify(mSystemStateListener).onCarModeChanged(false);
126
127 receiver.getValue().onReceive(mContext, new Intent("invalid action"));
128 }
129}