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