InCallWakeLockControllerTest.java revision a3799ae6aafba8ccd6448a7c4337acd3460b49f2
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
17package com.android.server.telecom.tests;
18
19import static org.mockito.Mockito.mock;
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.when;
22import static org.mockito.Mockito.never;
23
24import android.os.PowerManager;
25import android.test.suitebuilder.annotation.SmallTest;
26
27import com.android.server.telecom.Call;
28import com.android.server.telecom.CallState;
29import com.android.server.telecom.CallsManager;
30import com.android.server.telecom.InCallWakeLockController;
31import com.android.server.telecom.TelecomWakeLock;
32
33import org.mockito.Mock;
34
35public 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
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
53    public void tearDown() throws Exception {
54        mInCallWakeLockController = null;
55        super.tearDown();
56    }
57
58    @SmallTest
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
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
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
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
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
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