Home | History | Annotate | Download | only in cts
      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 static android.telecom.cts.TestUtils.shouldTestTelecom;
     20 
     21 import android.telecom.cts.MockInCallService.InCallServiceCallbacks;
     22 
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.net.Uri;
     26 import android.telecom.Call;
     27 import android.telecom.InCallService;
     28 import android.test.InstrumentationTestCase;
     29 import android.text.TextUtils;
     30 
     31 import java.util.concurrent.TimeUnit;
     32 
     33 /**
     34  * Sanity test that adding a new call via the CALL intent works correctly.
     35  */
     36 public class BasicInCallServiceTest extends InstrumentationTestCase {
     37 
     38     private static final Uri TEST_NUMBER = Uri.fromParts("tel", "555-1234", null);
     39 
     40     private Context mContext;
     41     private String mPreviousDefaultDialer = null;
     42 
     43     @Override
     44     protected void setUp() throws Exception {
     45         super.setUp();
     46         mContext = getInstrumentation().getContext();
     47         mPreviousDefaultDialer = TestUtils.getDefaultDialer(getInstrumentation());
     48         TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
     49     }
     50 
     51     @Override
     52     protected void tearDown() throws Exception {
     53         if (!TextUtils.isEmpty(mPreviousDefaultDialer)) {
     54             TestUtils.setDefaultDialer(getInstrumentation(), mPreviousDefaultDialer);
     55         }
     56         super.tearDown();
     57     }
     58 
     59     /**
     60      * Tests that when sending a CALL intent via the Telecom -> Telephony stack, Telecom
     61      * binds to the registered {@link InCallService}s and adds a new call. This test will
     62      * actually place a phone call to the number 7. It should still pass even if there is no
     63      * SIM card inserted.
     64      */
     65     public void testTelephonyCall_bindsToInCallServiceAndAddsCall() {
     66         if (!shouldTestTelecom(mContext)) {
     67             return;
     68         }
     69 
     70         final Intent intent = new Intent(Intent.ACTION_CALL, TEST_NUMBER);
     71         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     72 
     73         final InCallServiceCallbacks callbacks = createCallbacks();
     74 
     75         MockInCallService.setCallbacks(callbacks);
     76 
     77         mContext.startActivity(intent);
     78 
     79         try {
     80             if (callbacks.lock.tryAcquire(TestUtils.WAIT_FOR_CALL_ADDED_TIMEOUT_S,
     81                         TimeUnit.SECONDS)) {
     82                 return;
     83             }
     84         } catch (InterruptedException e) {
     85         }
     86 
     87         fail("No call added to InCallService.");
     88     }
     89 
     90     private MockInCallService.InCallServiceCallbacks createCallbacks() {
     91         final InCallServiceCallbacks callbacks = new InCallServiceCallbacks() {
     92             @Override
     93             public void onCallAdded(Call call, int numCalls) {
     94                 assertEquals("InCallService should have 1 call after adding call", 1, numCalls);
     95                 call.disconnect();
     96                 lock.release();
     97             }
     98         };
     99         return callbacks;
    100     }
    101 }
    102