1 /*
2  * Copyright (C) 2014 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 
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 import static org.mockito.Mockito.never;
23 
24 import android.os.PowerManager;
25 import android.test.suitebuilder.annotation.SmallTest;
26 
27 import com.android.server.telecom.Call;
28 import com.android.server.telecom.CallState;
29 import com.android.server.telecom.CallsManager;
30 import com.android.server.telecom.InCallWakeLockController;
31 import com.android.server.telecom.TelecomWakeLock;
32 
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
38 import org.mockito.Mock;
39 
40 @RunWith(JUnit4.class)
41 public class InCallWakeLockControllerTest extends TelecomTestCase {
42 
43     @Mock CallsManager mCallsManager;
44     @Mock Call mCall;
45     @Mock TelecomWakeLock.WakeLockAdapter mWakeLockAdapter;
46     private InCallWakeLockController mInCallWakeLockController;
47 
48     @Override
49     @Before
setUp()50     public void setUp() throws Exception {
51         super.setUp();
52         TelecomWakeLock telecomWakeLock = new TelecomWakeLock(
53                 null, //context never used due to mock WakeLockAdapter
54                 mWakeLockAdapter, PowerManager.FULL_WAKE_LOCK,
55                 InCallWakeLockControllerTest.class.getSimpleName());
56         mInCallWakeLockController = new InCallWakeLockController(telecomWakeLock, mCallsManager);
57     }
58 
59     @Override
60     @After
tearDown()61     public void tearDown() throws Exception {
62         mInCallWakeLockController = null;
63         super.tearDown();
64     }
65 
66     @SmallTest
67     @Test
testRingingCallAdded()68     public void testRingingCallAdded() throws Exception {
69         when(mCallsManager.getRingingOrSimulatedRingingCall()).thenReturn(mCall);
70         when(mWakeLockAdapter.isHeld()).thenReturn(false);
71 
72         mInCallWakeLockController.onCallAdded(mCall);
73 
74         verify(mWakeLockAdapter).acquire();
75     }
76 
77     @SmallTest
78     @Test
testNonRingingCallAdded()79     public void testNonRingingCallAdded() throws Exception {
80         when(mCallsManager.getRingingOrSimulatedRingingCall()).thenReturn(null);
81         when(mWakeLockAdapter.isHeld()).thenReturn(false);
82 
83         mInCallWakeLockController.onCallAdded(mCall);
84 
85         verify(mWakeLockAdapter, never()).acquire();
86     }
87 
88     @SmallTest
89     @Test
testRingingCallTransition()90     public void testRingingCallTransition() throws Exception {
91         when(mCallsManager.getRingingOrSimulatedRingingCall()).thenReturn(mCall);
92         when(mWakeLockAdapter.isHeld()).thenReturn(false);
93 
94         mInCallWakeLockController.onCallStateChanged(mCall, CallState.NEW, CallState.RINGING);
95 
96         verify(mWakeLockAdapter).acquire();
97     }
98 
99     @SmallTest
100     @Test
testRingingCallRemoved()101     public void testRingingCallRemoved() throws Exception {
102         when(mCallsManager.getRingingOrSimulatedRingingCall()).thenReturn(null);
103         when(mWakeLockAdapter.isHeld()).thenReturn(false);
104 
105         mInCallWakeLockController.onCallRemoved(mCall);
106 
107         verify(mWakeLockAdapter, never()).acquire();
108     }
109 
110     @SmallTest
111     @Test
testWakeLockReleased()112     public void testWakeLockReleased() throws Exception {
113         when(mCallsManager.getRingingOrSimulatedRingingCall()).thenReturn(null);
114         when(mWakeLockAdapter.isHeld()).thenReturn(true);
115 
116         mInCallWakeLockController.onCallRemoved(mCall);
117 
118         verify(mWakeLockAdapter).release(0);
119     }
120 
121     @SmallTest
122     @Test
testAcquireWakeLockWhenHeld()123     public void testAcquireWakeLockWhenHeld() throws Exception {
124         when(mCallsManager.getRingingOrSimulatedRingingCall()).thenReturn(mCall);
125         when(mWakeLockAdapter.isHeld()).thenReturn(true);
126 
127         mInCallWakeLockController.onCallAdded(mock(Call.class));
128 
129         verify(mWakeLockAdapter, never()).acquire();
130     }
131 }
132