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.voiceinteraction.cts;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.os.Bundle;
     25 import android.test.ActivityInstrumentationTestCase2;
     26 import android.util.Log;
     27 
     28 import junit.framework.Assert;
     29 
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 import java.util.concurrent.CountDownLatch;
     33 import java.util.concurrent.TimeUnit;
     34 
     35 import android.voiceinteraction.common.Utils;
     36 
     37 public class VoiceInteractionTest extends ActivityInstrumentationTestCase2<TestStartActivity> {
     38     static final String TAG = "VoiceInteractionTest";
     39     private static final int TIMEOUT_MS = 20 * 1000;
     40 
     41     private TestStartActivity mTestActivity;
     42     private Context mContext;
     43     private TestResultsReceiver mReceiver;
     44     private Bundle mResults;
     45     private final CountDownLatch mLatch = new CountDownLatch(1);
     46     protected boolean mHasFeature;
     47     protected static final String FEATURE_VOICE_RECOGNIZERS = "android.software.voice_recognizers";
     48 
     49     public VoiceInteractionTest() {
     50         super(TestStartActivity.class);
     51     }
     52 
     53     @Override
     54     protected void setUp() throws Exception {
     55         super.setUp();
     56         startTestActivity();
     57         mContext = getInstrumentation().getTargetContext();
     58         mHasFeature = mContext.getPackageManager().hasSystemFeature(FEATURE_VOICE_RECOGNIZERS);
     59         if (mHasFeature) {
     60             mReceiver = new TestResultsReceiver();
     61             mContext.registerReceiver(mReceiver, new IntentFilter(Utils.BROADCAST_INTENT));
     62         }
     63     }
     64 
     65     @Override
     66     protected void tearDown() throws Exception {
     67         if (mHasFeature && mReceiver != null) {
     68             try {
     69                 mContext.unregisterReceiver(mReceiver);
     70             } catch (IllegalArgumentException e) {
     71                 // This exception is thrown if mReceiver in
     72                 // the above call to unregisterReceiver is never registered.
     73                 // If so, no harm done by ignoring this exception.
     74             }
     75             mReceiver = null;
     76         }
     77         super.tearDown();
     78     }
     79 
     80     private void startTestActivity() throws Exception {
     81         Intent intent = new Intent();
     82         intent.setAction("android.intent.action.TEST_START_ACTIVITY");
     83         intent.setComponent(new ComponentName(getInstrumentation().getContext(),
     84                 TestStartActivity.class));
     85         setActivityIntent(intent);
     86         mTestActivity = getActivity();
     87     }
     88 
     89     public void testAll() throws Exception {
     90         VoiceInteractionTestReceiver.waitSessionStarted(this, 5, TimeUnit.SECONDS);
     91 
     92         if (!mHasFeature) {
     93             Log.i(TAG, "The device doesn't support feature: " + FEATURE_VOICE_RECOGNIZERS);
     94             return;
     95         }
     96         if (!mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
     97             fail("Failed to receive broadcast in " + TIMEOUT_MS + "msec");
     98             return;
     99         }
    100         if (mResults == null) {
    101             fail("no results received at all!");
    102             return;
    103         }
    104         int numFails = 0;
    105         for (Utils.TestCaseType t : Utils.TestCaseType.values()) {
    106             String singleResult = mResults.getString(t.toString());
    107             if (singleResult == null) {
    108                 numFails++;
    109                 Log.i(TAG, "No testresults received for " + t);
    110             } else {
    111                 verifySingleTestcaseResult(t, singleResult);
    112             }
    113         }
    114         assertEquals(0, numFails);
    115         mTestActivity.finish();
    116     }
    117 
    118     private void verifySingleTestcaseResult(Utils.TestCaseType testCaseType, String result) {
    119         Log.i(TAG, "Recevied testresult: " + result + " for " + testCaseType);
    120         switch (testCaseType) {
    121           case ABORT_REQUEST_CANCEL_TEST:
    122               assertTrue(result.equals(Utils.ABORT_REQUEST_CANCEL_SUCCESS));
    123               break;
    124           case ABORT_REQUEST_TEST:
    125               assertTrue(result.equals(Utils.ABORT_REQUEST_SUCCESS));
    126               break;
    127           case COMMANDREQUEST_TEST:
    128               assertTrue(result.equals(Utils.COMMANDREQUEST_SUCCESS));
    129               break;
    130           case COMMANDREQUEST_CANCEL_TEST:
    131               assertTrue(result.equals(Utils.COMMANDREQUEST_CANCEL_SUCCESS));
    132               break;
    133           case COMPLETION_REQUEST_CANCEL_TEST:
    134               assertTrue(result.equals(Utils.COMPLETION_REQUEST_CANCEL_SUCCESS));
    135               break;
    136           case COMPLETION_REQUEST_TEST:
    137               assertTrue(result.equals(Utils.COMPLETION_REQUEST_SUCCESS));
    138               break;
    139           case CONFIRMATION_REQUEST_CANCEL_TEST:
    140               assertTrue(result.equals(Utils.CONFIRMATION_REQUEST_CANCEL_SUCCESS));
    141               break;
    142           case CONFIRMATION_REQUEST_TEST:
    143               assertTrue(result.equals(Utils.CONFIRMATION_REQUEST_SUCCESS));
    144               break;
    145           case PICKOPTION_REQUEST_CANCEL_TEST:
    146               assertTrue(result.equals(Utils.PICKOPTION_REQUEST_CANCEL_SUCCESS));
    147               break;
    148           case PICKOPTION_REQUEST_TEST:
    149               assertTrue(result.equals(Utils.PICKOPTION_REQUEST_SUCCESS));
    150               break;
    151           case SUPPORTS_COMMANDS_TEST:
    152               assertTrue(result.equals(Utils.SUPPORTS_COMMANDS_SUCCESS));
    153               break;
    154           default:
    155               Log.wtf(TAG, "not expected");
    156               break;
    157         }
    158         Log.i(TAG, testCaseType + " passed");
    159     }
    160 
    161 
    162     class TestResultsReceiver extends BroadcastReceiver {
    163         @Override
    164         public void onReceive(Context context, Intent intent) {
    165             if (intent.getAction().equalsIgnoreCase(Utils.BROADCAST_INTENT)) {
    166                 Log.i(TAG, "received broadcast with results ");
    167                 VoiceInteractionTest.this.mResults = intent.getExtras();
    168                 mLatch.countDown();
    169             }
    170         }
    171     }
    172 }
    173