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.junit.Assert.assertEquals; 20import static org.junit.Assert.assertFalse; 21import static org.junit.Assert.assertTrue; 22import 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; 32import android.test.suitebuilder.annotation.SmallTest; 33 34import com.android.server.telecom.SystemStateProvider; 35import com.android.server.telecom.SystemStateProvider.SystemStateListener; 36 37import org.junit.After; 38import org.junit.Before; 39import org.junit.Test; 40import org.junit.runner.RunWith; 41import org.junit.runners.JUnit4; 42import org.mockito.ArgumentCaptor; 43import org.mockito.Mock; 44import org.mockito.MockitoAnnotations; 45 46/** 47 * Unit tests for SystemStateProvider 48 */ 49@RunWith(JUnit4.class) 50public class SystemStateProviderTest extends TelecomTestCase { 51 52 @Mock Context mContext; 53 @Mock SystemStateListener mSystemStateListener; 54 @Mock UiModeManager mUiModeManager; 55 @Mock Intent mIntentEnter; 56 @Mock Intent mIntentExit; 57 58 @Override 59 @Before 60 public void setUp() throws Exception { 61 super.setUp(); 62 MockitoAnnotations.initMocks(this); 63 } 64 65 @Override 66 @After 67 public void tearDown() throws Exception { 68 super.tearDown(); 69 } 70 71 @SmallTest 72 @Test 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 82 @SmallTest 83 @Test 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 90 @SmallTest 91 @Test 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 98 @SmallTest 99 @Test 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 110 @SmallTest 111 @Test 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} 130