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 android.telecom.cts;
18 
19 import android.telecom.Call;
20 import android.telecom.Connection;
21 
22 /**
23  * Verifies Telecom behavior with regards to interactions with a wired headset. These tests
24  * validate behavior that occurs as a result of short pressing or long pressing a wired headset's
25  * media button.
26  */
27 public class WiredHeadsetTest extends BaseTelecomTestWithMockServices {
28 
29     // TODO(b/272362532): Figure out a way to mock out audio routing in CTS; this class depends on a
30     // wired headset being attached to operate as expected.
31     // We do this here instead of renaming the tests because the CTS test runner complains that the
32     // class has no tests.
33     private static final boolean DISABLE_TESTS = true;
34 
35     @Override
setUp()36     protected void setUp() throws Exception {
37         super.setUp();
38         if (mShouldTestTelecom && !DISABLE_TESTS) {
39             setupConnectionService(null, FLAG_REGISTER | FLAG_ENABLE);
40         }
41     }
42 
43     @Override
tearDown()44     protected void tearDown() throws Exception {
45         super.tearDown();
46     }
47 
testIncomingCallShortPress_acceptsCall()48     public void testIncomingCallShortPress_acceptsCall() throws Exception {
49         if (!mShouldTestTelecom || DISABLE_TESTS) {
50             return;
51         }
52 
53         addAndVerifyNewIncomingCall(createTestNumber(), null);
54         final MockConnection connection = verifyConnectionForIncomingCall();
55 
56         final Call call = mInCallCallbacks.getService().getLastCall();
57         assertCallState(call, Call.STATE_RINGING);
58         assertConnectionState(connection, Connection.STATE_RINGING);
59 
60         sendMediaButtonShortPress();
61         assertCallState(call,  Call.STATE_ACTIVE);
62         assertConnectionState(connection, Connection.STATE_ACTIVE);
63     }
64 
testIncomingCallLongPress_rejectsCall()65     public void testIncomingCallLongPress_rejectsCall() throws Exception {
66         if (!mShouldTestTelecom || DISABLE_TESTS) {
67             return;
68         }
69 
70         addAndVerifyNewIncomingCall(createTestNumber(), null);
71         final MockConnection connection = verifyConnectionForIncomingCall();
72 
73         final Call call = mInCallCallbacks.getService().getLastCall();
74         assertCallState(call, Call.STATE_RINGING);
75         assertConnectionState(connection, Connection.STATE_RINGING);
76 
77         sendMediaButtonLongPress();
78         assertCallState(call, Call.STATE_DISCONNECTED);
79         assertConnectionState(connection, Connection.STATE_DISCONNECTED);
80     }
81 
testInCallLongPress_togglesMute()82     public void testInCallLongPress_togglesMute() throws Exception {
83         if (!mShouldTestTelecom || DISABLE_TESTS) {
84             return;
85         }
86 
87         placeAndVerifyCall();
88         final MockConnection connection = verifyConnectionForOutgoingCall();
89         final MockInCallService incallService = mInCallCallbacks.getService();
90 
91         // Verify that sending short presses in succession toggles the mute state of the
92         // connection.
93         // Before the audio state is changed for the first time, the connection might not
94         // know about its audio state yet.
95         assertMuteState(incallService, false);
96         sendMediaButtonLongPress();
97         assertMuteState(connection, true);
98         assertMuteState(incallService, true);
99         sendMediaButtonLongPress();
100         assertMuteState(connection, false);
101         assertMuteState(incallService, false);
102     }
103 
testInCallShortPress_hangupCall()104     public void testInCallShortPress_hangupCall() throws Exception {
105         if (!mShouldTestTelecom || DISABLE_TESTS) {
106             return;
107         }
108 
109         placeAndVerifyCall();
110         final MockConnection connection = verifyConnectionForOutgoingCall();
111 
112         final Call call = mInCallCallbacks.getService().getLastCall();
113         assertCallState(call, Call.STATE_DIALING);
114 
115         connection.setActive();
116         assertCallState(call, Call.STATE_ACTIVE);
117 
118         sendMediaButtonShortPress();
119         assertCallState(call, Call.STATE_DISCONNECTED);
120         assertConnectionState(connection, Connection.STATE_DISCONNECTED);
121     }
122 
sendMediaButtonShortPress()123     private void sendMediaButtonShortPress() throws Exception {
124         sendMediaButtonPress(false /* longPress */);
125     }
126 
sendMediaButtonLongPress()127     private void sendMediaButtonLongPress() throws Exception {
128         sendMediaButtonPress(true /* longPress */);
129     }
130 
sendMediaButtonPress(boolean longPress)131     private void sendMediaButtonPress(boolean longPress) throws Exception {
132         // request 3 seconds press when long press needed for stability
133         final String command = "input keyevent " + (longPress ? "--longpress 3" : "--shortpress")
134                 + " KEYCODE_HEADSETHOOK";
135         TestUtils.executeShellCommand(getInstrumentation(), command);
136     }
137 }
138