SystemStateProviderTest.java revision a3799ae6aafba8ccd6448a7c4337acd3460b49f2
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
17package com.android.server.telecom.tests;
18
19import static org.mockito.Matchers.any;
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.when;
22
23import android.app.UiModeManager;
24import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.IntentFilter;
28import android.content.res.Configuration;
29import android.test.suitebuilder.annotation.SmallTest;
30
31import com.android.server.telecom.SystemStateProvider;
32import com.android.server.telecom.SystemStateProvider.SystemStateListener;
33
34import org.mockito.ArgumentCaptor;
35import org.mockito.Mock;
36import org.mockito.MockitoAnnotations;
37
38/**
39 * Unit tests for SystemStateProvider
40 */
41public class SystemStateProviderTest extends TelecomTestCase {
42
43    SystemStateProvider mSystemStateProvider;
44
45    @Mock Context mContext;
46    @Mock SystemStateListener mSystemStateListener;
47    @Mock UiModeManager mUiModeManager;
48    @Mock Intent mIntentEnter;
49    @Mock Intent mIntentExit;
50
51    @Override
52    public void setUp() throws Exception {
53        super.setUp();
54        MockitoAnnotations.initMocks(this);
55    }
56
57    @Override
58    public void tearDown() throws Exception {
59        super.tearDown();
60    }
61
62    @SmallTest
63    public void testListeners() throws Exception {
64        SystemStateProvider systemStateProvider = new SystemStateProvider(mContext);
65
66        assertFalse(systemStateProvider.removeListener(mSystemStateListener));
67        systemStateProvider.addListener(mSystemStateListener);
68        assertTrue(systemStateProvider.removeListener(mSystemStateListener));
69        assertFalse(systemStateProvider.removeListener(mSystemStateListener));
70    }
71
72    @SmallTest
73    public void testQuerySystemForCarMode_True() {
74        when(mContext.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(mUiModeManager);
75        when(mUiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_CAR);
76        assertTrue(new SystemStateProvider(mContext).isCarMode());
77    }
78
79    @SmallTest
80    public void testQuerySystemForCarMode_False() {
81        when(mContext.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(mUiModeManager);
82        when(mUiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_NORMAL);
83        assertFalse(new SystemStateProvider(mContext).isCarMode());
84    }
85
86    @SmallTest
87    public void testReceiverAndIntentFilter() {
88        ArgumentCaptor<IntentFilter> intentFilter = ArgumentCaptor.forClass(IntentFilter.class);
89        new SystemStateProvider(mContext);
90        verify(mContext).registerReceiver(any(BroadcastReceiver.class), intentFilter.capture());
91
92        assertEquals(2, intentFilter.getValue().countActions());
93        assertEquals(UiModeManager.ACTION_ENTER_CAR_MODE, intentFilter.getValue().getAction(0));
94        assertEquals(UiModeManager.ACTION_EXIT_CAR_MODE, intentFilter.getValue().getAction(1));
95    }
96
97    @SmallTest
98    public void testOnEnterExitCarMode() {
99        ArgumentCaptor<BroadcastReceiver> receiver =
100                ArgumentCaptor.forClass(BroadcastReceiver.class);
101        new SystemStateProvider(mContext).addListener(mSystemStateListener);
102
103        verify(mContext).registerReceiver(receiver.capture(), any(IntentFilter.class));
104
105        when(mIntentEnter.getAction()).thenReturn(UiModeManager.ACTION_ENTER_CAR_MODE);
106        receiver.getValue().onReceive(mContext, mIntentEnter);
107        verify(mSystemStateListener).onCarModeChanged(true);
108
109        when(mIntentExit.getAction()).thenReturn(UiModeManager.ACTION_EXIT_CAR_MODE);
110        receiver.getValue().onReceive(mContext, mIntentExit);
111        verify(mSystemStateListener).onCarModeChanged(false);
112
113        receiver.getValue().onReceive(mContext, new Intent("invalid action"));
114    }
115}
116