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