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.mockito.Mock;
34 
35 public class InCallWakeLockControllerTest extends TelecomTestCase {
36 
37     @Mock CallsManager mCallsManager;
38     @Mock Call mCall;
39     @Mock TelecomWakeLock.WakeLockAdapter mWakeLockAdapter;
40     private InCallWakeLockController mInCallWakeLockController;
41 
42     @Override
setUp()43     public void setUp() throws Exception {
44         super.setUp();
45         TelecomWakeLock telecomWakeLock = new TelecomWakeLock(
46                 null, //context never used due to mock WakeLockAdapter
47                 mWakeLockAdapter, PowerManager.FULL_WAKE_LOCK,
48                 InCallWakeLockControllerTest.class.getSimpleName());
49         mInCallWakeLockController = new InCallWakeLockController(telecomWakeLock, mCallsManager);
50     }
51 
52     @Override
tearDown()53     public void tearDown() throws Exception {
54         mInCallWakeLockController = null;
55         super.tearDown();
56     }
57 
58     @SmallTest
testRingingCallAdded()59     public void testRingingCallAdded() throws Exception {
60         when(mCallsManager.getRingingCall()).thenReturn(mCall);
61         when(mWakeLockAdapter.isHeld()).thenReturn(false);
62 
63         mInCallWakeLockController.onCallAdded(mCall);
64 
65         verify(mWakeLockAdapter).acquire();
66     }
67 
68     @SmallTest
testNonRingingCallAdded()69     public void testNonRingingCallAdded() throws Exception {
70         when(mCallsManager.getRingingCall()).thenReturn(null);
71         when(mWakeLockAdapter.isHeld()).thenReturn(false);
72 
73         mInCallWakeLockController.onCallAdded(mCall);
74 
75         verify(mWakeLockAdapter, never()).acquire();
76     }
77 
78     @SmallTest
testRingingCallTransition()79     public void testRingingCallTransition() throws Exception {
80         when(mCallsManager.getRingingCall()).thenReturn(mCall);
81         when(mWakeLockAdapter.isHeld()).thenReturn(false);
82 
83         mInCallWakeLockController.onCallStateChanged(mCall, CallState.NEW, CallState.RINGING);
84 
85         verify(mWakeLockAdapter).acquire();
86     }
87 
88     @SmallTest
testRingingCallRemoved()89     public void testRingingCallRemoved() throws Exception {
90         when(mCallsManager.getRingingCall()).thenReturn(null);
91         when(mWakeLockAdapter.isHeld()).thenReturn(false);
92 
93         mInCallWakeLockController.onCallRemoved(mCall);
94 
95         verify(mWakeLockAdapter, never()).acquire();
96     }
97 
98     @SmallTest
testWakeLockReleased()99     public void testWakeLockReleased() throws Exception {
100         when(mCallsManager.getRingingCall()).thenReturn(null);
101         when(mWakeLockAdapter.isHeld()).thenReturn(true);
102 
103         mInCallWakeLockController.onCallRemoved(mCall);
104 
105         verify(mWakeLockAdapter).release(0);
106     }
107 
108     @SmallTest
testAcquireWakeLockWhenHeld()109     public void testAcquireWakeLockWhenHeld() throws Exception {
110         when(mCallsManager.getRingingCall()).thenReturn(mCall);
111         when(mWakeLockAdapter.isHeld()).thenReturn(true);
112 
113         mInCallWakeLockController.onCallAdded(mock(Call.class));
114 
115         verify(mWakeLockAdapter, never()).acquire();
116     }
117 }
118