aboutsummaryrefslogtreecommitdiff
path: root/tests/common/src/com/android/tv/testing/uihelper/LiveChannelsUiDeviceHelper.java
blob: 4b7c1f8924927abe1b2970cd44ed6fc40ece1368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.tv.testing.uihelper;

import static com.android.tv.testing.uihelper.UiDeviceAsserts.waitForCondition;
import static junit.framework.TestCase.assertTrue;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;
import android.util.Log;
import com.android.tv.common.CommonConstants;
import com.android.tv.testing.utils.Utils;
import junit.framework.Assert;

/** Helper for testing the Live TV Application. */
public class LiveChannelsUiDeviceHelper extends BaseUiDeviceHelper {
    private static final String TAG = "LiveChannelsUiDevice";
    private static final int APPLICATION_START_TIMEOUT_MSEC = 5000;

    private final Context mContext;

    public LiveChannelsUiDeviceHelper(
            UiDevice uiDevice, Resources targetResources, Context context) {
        super(uiDevice, targetResources);
        mContext = context;
    }

    public void assertAppStarted() {
        assertTrue("TvActivity should be enabled.", Utils.isTvActivityEnabled(mContext));
        Intent intent =
                mContext.getPackageManager().getLaunchIntentForPackage(Constants.TV_APP_PACKAGE);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear out any previous instances
        mContext.startActivity(intent);
        // Wait for idle state before checking the channel banner because waitForCondition() has
        // timeout.
        mUiDevice.waitForIdle();
        // Make sure that the activity is resumed.
        waitForCondition(mUiDevice, Until.hasObject(Constants.TV_VIEW));

        Assert.assertTrue(
            Constants.TV_APP_PACKAGE + " did not start",
                mUiDevice.wait(
                        Until.hasObject(By.pkg(Constants.TV_APP_PACKAGE).depth(0)),
                        APPLICATION_START_TIMEOUT_MSEC));
        BySelector welcome = ByResource.id(mTargetResources, com.android.tv.R.id.intro);
        if (mUiDevice.hasObject(welcome)) {
            Log.i(TAG, "Welcome screen shown. Clearing dialog by pressing back");
            mUiDevice.pressBack();
        }
    }

    public void assertAppStopped() {
        while (mUiDevice.hasObject(By.pkg(CommonConstants.BASE_PACKAGE).depth(0))) {
            mUiDevice.pressBack();
            mUiDevice.waitForIdle();
        }
    }
}