Home | History | Annotate | Download | only in uihelper
      1 /*
      2  * Copyright (C) 2017 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 package com.android.tv.testing.uihelper;
     17 
     18 import static com.android.tv.testing.uihelper.UiDeviceAsserts.waitForCondition;
     19 import static junit.framework.TestCase.assertTrue;
     20 
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.res.Resources;
     24 import android.support.test.uiautomator.By;
     25 import android.support.test.uiautomator.BySelector;
     26 import android.support.test.uiautomator.UiDevice;
     27 import android.support.test.uiautomator.Until;
     28 import android.util.Log;
     29 import com.android.tv.common.CommonConstants;
     30 import com.android.tv.testing.utils.Utils;
     31 import junit.framework.Assert;
     32 
     33 /** Helper for testing the Live TV Application. */
     34 public class LiveChannelsUiDeviceHelper extends BaseUiDeviceHelper {
     35     private static final String TAG = "LiveChannelsUiDevice";
     36     private static final int APPLICATION_START_TIMEOUT_MSEC = 5000;
     37 
     38     private final Context mContext;
     39 
     40     public LiveChannelsUiDeviceHelper(
     41             UiDevice uiDevice, Resources targetResources, Context context) {
     42         super(uiDevice, targetResources);
     43         mContext = context;
     44     }
     45 
     46     public void assertAppStarted() {
     47         assertTrue("TvActivity should be enabled.", Utils.isTvActivityEnabled(mContext));
     48         Intent intent =
     49                 mContext.getPackageManager().getLaunchIntentForPackage(Constants.TV_APP_PACKAGE);
     50         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear out any previous instances
     51         mContext.startActivity(intent);
     52         // Wait for idle state before checking the channel banner because waitForCondition() has
     53         // timeout.
     54         mUiDevice.waitForIdle();
     55         // Make sure that the activity is resumed.
     56         waitForCondition(mUiDevice, Until.hasObject(Constants.TV_VIEW));
     57 
     58         Assert.assertTrue(
     59             Constants.TV_APP_PACKAGE + " did not start",
     60                 mUiDevice.wait(
     61                         Until.hasObject(By.pkg(Constants.TV_APP_PACKAGE).depth(0)),
     62                         APPLICATION_START_TIMEOUT_MSEC));
     63         BySelector welcome = ByResource.id(mTargetResources, com.android.tv.R.id.intro);
     64         if (mUiDevice.hasObject(welcome)) {
     65             Log.i(TAG, "Welcome screen shown. Clearing dialog by pressing back");
     66             mUiDevice.pressBack();
     67         }
     68     }
     69 
     70     public void assertAppStopped() {
     71         while (mUiDevice.hasObject(By.pkg(CommonConstants.BASE_PACKAGE).depth(0))) {
     72             mUiDevice.pressBack();
     73             mUiDevice.waitForIdle();
     74         }
     75     }
     76 }
     77