aboutsummaryrefslogtreecommitdiff
path: root/tests/func/src/com/android/tv/tests/ui/LiveChannelsTestController.java
blob: 03d30ca41f9b69f46e93346085f9da8371409c96 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2018 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.tests.ui;

import static junit.framework.Assert.assertTrue;

import android.app.Instrumentation;
import android.app.UiAutomation;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.Configurator;
import android.support.test.uiautomator.SearchCondition;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;
import android.view.InputDevice;
import android.view.KeyEvent;
import com.android.tv.testing.data.ChannelInfo;
import com.android.tv.testing.testinput.ChannelStateData;
import com.android.tv.testing.testinput.TestInputControlConnection;
import com.android.tv.testing.testinput.TestInputControlUtils;
import com.android.tv.testing.uihelper.Constants;
import com.android.tv.testing.uihelper.LiveChannelsUiDeviceHelper;
import com.android.tv.testing.uihelper.MenuHelper;
import com.android.tv.testing.uihelper.SidePanelHelper;
import com.android.tv.testing.uihelper.UiDeviceAsserts;
import com.android.tv.testing.uihelper.UiDeviceUtils;
import org.junit.rules.ExternalResource;

/** UIHelpers and TestInputController for LiveChannels. */
public class LiveChannelsTestController extends ExternalResource {

    private final TestInputControlConnection mConnection = new TestInputControlConnection();

    public MenuHelper menuHelper;
    public SidePanelHelper sidePanelHelper;
    public LiveChannelsUiDeviceHelper liveChannelsHelper;

    private UiDevice mDevice;
    private Resources mTargetResources;
    private Instrumentation mInstrumentation;

    private void assertPressKeyUp(int keyCode, boolean sync) {
        assertPressKey(KeyEvent.ACTION_UP, keyCode, sync);
    }

    private void assertPressKey(int action, int keyCode, boolean sync) {
        long eventTime = SystemClock.uptimeMillis();
        KeyEvent event =
                new KeyEvent(
                        eventTime,
                        eventTime,
                        action,
                        keyCode,
                        0,
                        0,
                        -1,
                        0,
                        0,
                        InputDevice.SOURCE_KEYBOARD);
        assertTrue("Failed to inject key up event:" + event, injectEvent(event, sync));
    }

    private UiAutomation getUiAutomation() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            int flags = Configurator.getInstance().getUiAutomationFlags();
            return mInstrumentation.getUiAutomation(flags);
        } else {
            return mInstrumentation.getUiAutomation();
        }
    }

    @Override
    protected void before() throws Exception {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        Context context = mInstrumentation.getContext();
        context.bindService(
                TestInputControlUtils.createIntent(), mConnection, Context.BIND_AUTO_CREATE);
        mDevice = UiDevice.getInstance(mInstrumentation);
        mTargetResources = mInstrumentation.getTargetContext().getResources();
        menuHelper = new MenuHelper(mDevice, mTargetResources);
        sidePanelHelper = new SidePanelHelper(mDevice, mTargetResources);
        liveChannelsHelper = new LiveChannelsUiDeviceHelper(mDevice, mTargetResources, context);
    }

    @Override
    protected void after() {
        if (mConnection.isBound()) {
            mInstrumentation.getContext().unbindService(mConnection);
        }

        // TODO: robustly handle left over pops from failed tests.
        // Clear any side panel, menu, ...
        // Scene container should not be checked here because pressing the BACK key in some scenes
        // might launch the home screen.
        if (mDevice.hasObject(Constants.SIDE_PANEL)
                || mDevice.hasObject(Constants.MENU)
                || mDevice.hasObject(Constants.PROGRAM_GUIDE)) {
            mDevice.pressBack();
        }
        // To destroy the activity to make sure next test case's activity launch check works well.
        mDevice.pressBack();
    }

    /**
     * Update the channel state to {@code data} then tune to that channel.
     *
     * @param data the state to update the channel with.
     * @param channel the channel to tune to
     */
    protected void updateThenTune(ChannelStateData data, ChannelInfo channel) {
        mConnection.updateChannelState(channel, data);
        pressKeysForChannel(channel);
    }

    /**
     * Send the keys for the channel number of {@code channel} and press the DPAD center.
     *
     * <p>Usually this will tune to the given channel.
     */
    public void pressKeysForChannel(ChannelInfo channel) {
        UiDeviceUtils.pressKeys(mDevice, channel.number);
        UiDeviceAsserts.assertWaitForCondition(
                mDevice, Until.hasObject(Constants.KEYPAD_CHANNEL_SWITCH));
        mDevice.pressDPadCenter();
    }

    public void assertHas(BySelector bySelector, boolean expected) {
        UiDeviceAsserts.assertHas(mDevice, bySelector, expected);
    }

    public void assertWaitForCondition(SearchCondition<Boolean> booleanSearchCondition) {
        UiDeviceAsserts.assertWaitForCondition(mDevice, booleanSearchCondition);
    }

    public void assertWaitForCondition(SearchCondition<Boolean> gone, long timeout) {
        UiDeviceAsserts.assertWaitForCondition(mDevice, gone, timeout);
    }

    public void assertWaitUntilFocused(BySelector bySelector) {
        UiDeviceAsserts.assertWaitUntilFocused(mDevice, bySelector);
    }

    public Resources getTargetResources() {
        return mTargetResources;
    }

    public UiDevice getUiDevice() {
        return mDevice;
    }

    public boolean injectEvent(KeyEvent event, boolean sync) {
        return getUiAutomation().injectInputEvent(event, sync);
    }

    public void pressBack() {
        mDevice.pressBack();
    }

    public void pressDPadCenter() {
        pressDPadCenter(1);
    }

    public void pressDPadCenter(int repeat) {
        UiDevice.getInstance(mInstrumentation).waitForIdle();
        for (int i = 0; i < repeat; ++i) {
            assertPressKey(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, false);
            if (i < repeat - 1) {
                assertPressKeyUp(KeyEvent.KEYCODE_DPAD_CENTER, false);
            }
        }
        // Send last key event synchronously.
        assertPressKeyUp(KeyEvent.KEYCODE_DPAD_CENTER, true);
    }

    public void pressDPadDown() {
        mDevice.pressDPadDown();
    }

    public void pressDPadLeft() {
        mDevice.pressDPadLeft();
    }

    public void pressDPadRight() {
        mDevice.pressDPadRight();
    }

    public void pressDPadUp() {
        mDevice.pressDPadUp();
    }

    public void pressEnter() {
        mDevice.pressEnter();
    }

    public void pressHome() {
        mDevice.pressHome();
    }

    public void pressKeyCode(int keyCode) {
        mDevice.pressKeyCode(keyCode);
    }

    public void pressMenu() {
        mDevice.pressMenu();
    }

    public void waitForIdle() {
        mDevice.waitForIdle();
    }

    public void waitForIdleSync() {
        mInstrumentation.waitForIdleSync();
    }
}