Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 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 | |
| 17 | package com.android.server.telecom.tests; |
| 18 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertFalse; |
| 21 | import static org.junit.Assert.assertTrue; |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 22 | import static org.mockito.Matchers.any; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.app.UiModeManager; |
| 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
| 31 | import android.content.res.Configuration; |
Hall Liu | a3799ae | 2016-03-16 11:37:51 -0700 | [diff] [blame] | 32 | import android.test.suitebuilder.annotation.SmallTest; |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 33 | |
| 34 | import com.android.server.telecom.SystemStateProvider; |
| 35 | import com.android.server.telecom.SystemStateProvider.SystemStateListener; |
| 36 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 37 | import org.junit.After; |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.junit.runners.JUnit4; |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 42 | import org.mockito.ArgumentCaptor; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.MockitoAnnotations; |
| 45 | |
| 46 | /** |
| 47 | * Unit tests for SystemStateProvider |
| 48 | */ |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 49 | @RunWith(JUnit4.class) |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 50 | public class SystemStateProviderTest extends TelecomTestCase { |
| 51 | |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 52 | @Mock Context mContext; |
| 53 | @Mock SystemStateListener mSystemStateListener; |
| 54 | @Mock UiModeManager mUiModeManager; |
| 55 | @Mock Intent mIntentEnter; |
| 56 | @Mock Intent mIntentExit; |
| 57 | |
| 58 | @Override |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 59 | @Before |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 60 | public void setUp() throws Exception { |
| 61 | super.setUp(); |
| 62 | MockitoAnnotations.initMocks(this); |
| 63 | } |
| 64 | |
| 65 | @Override |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 66 | @After |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 67 | public void tearDown() throws Exception { |
| 68 | super.tearDown(); |
| 69 | } |
| 70 | |
Hall Liu | a3799ae | 2016-03-16 11:37:51 -0700 | [diff] [blame] | 71 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 72 | @Test |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 73 | 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 Liu | a3799ae | 2016-03-16 11:37:51 -0700 | [diff] [blame] | 82 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 83 | @Test |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 84 | 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 Liu | a3799ae | 2016-03-16 11:37:51 -0700 | [diff] [blame] | 90 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 91 | @Test |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 92 | 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 Liu | a3799ae | 2016-03-16 11:37:51 -0700 | [diff] [blame] | 98 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 99 | @Test |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 100 | 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 Liu | a3799ae | 2016-03-16 11:37:51 -0700 | [diff] [blame] | 110 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 111 | @Test |
Santos Cordon | f78a72f | 2016-01-21 22:10:32 +0000 | [diff] [blame] | 112 | 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 | } |