1package com.android.tv.testing.uihelper;
2
3import static com.android.tv.testing.uihelper.UiDeviceAsserts.waitForCondition;
4
5import android.content.Context;
6import android.content.Intent;
7import android.content.res.Resources;
8import android.support.test.uiautomator.By;
9import android.support.test.uiautomator.BySelector;
10import android.support.test.uiautomator.UiDevice;
11import android.support.test.uiautomator.Until;
12import android.util.Log;
13
14import junit.framework.Assert;
15
16/**
17 * Helper for testing the Live TV Application.
18 */
19public class LiveChannelsUiDeviceHelper extends BaseUiDeviceHelper {
20    private static final String TAG = "LiveChannelsUiDevice";
21    private static final int APPLICATION_START_TIMEOUT_MSEC = 5000;
22
23    private final Context mContext;
24
25    public LiveChannelsUiDeviceHelper(UiDevice uiDevice, Resources targetResources,
26            Context context) {
27        super(uiDevice, targetResources);
28        mContext = context;
29    }
30
31    public void assertAppStarted() {
32        Intent intent = mContext.getPackageManager()
33                .getLaunchIntentForPackage(Constants.TV_APP_PACKAGE);
34        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);    // Clear out any previous instances
35        mContext.startActivity(intent);
36        // Wait for idle state before checking the channel banner because waitForCondition() has
37        // timeout.
38        mUiDevice.waitForIdle();
39        // Make sure that the activity is resumed.
40        waitForCondition(mUiDevice, Until.hasObject(Constants.TV_VIEW));
41
42        Assert.assertTrue(Constants.TV_APP_PACKAGE + " did not start", mUiDevice
43                .wait(Until.hasObject(By.pkg(Constants.TV_APP_PACKAGE).depth(0)),
44                        APPLICATION_START_TIMEOUT_MSEC));
45        BySelector welcome = ByResource.id(mTargetResources, com.android.tv.R.id.intro);
46        if (mUiDevice.hasObject(welcome)) {
47            Log.i(TAG, "Welcome screen shown. Clearing dialog by pressing back");
48            mUiDevice.pressBack();
49        }
50    }
51}