1/* 2 * Copyright (C) 2015 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 17package com.android.tv.tests.ui; 18 19import static com.android.tv.testing.uihelper.UiDeviceAsserts.assertWaitForCondition; 20 21import android.content.Context; 22import android.content.res.Resources; 23import android.support.test.uiautomator.UiDevice; 24import android.support.test.uiautomator.Until; 25import android.test.InstrumentationTestCase; 26 27import com.android.tv.testing.ChannelInfo; 28import com.android.tv.testing.testinput.ChannelStateData; 29import com.android.tv.testing.testinput.TestInputControlConnection; 30import com.android.tv.testing.testinput.TestInputControlUtils; 31import com.android.tv.testing.uihelper.Constants; 32import com.android.tv.testing.uihelper.LiveChannelsUiDeviceHelper; 33import com.android.tv.testing.uihelper.MenuHelper; 34import com.android.tv.testing.uihelper.SidePanelHelper; 35import com.android.tv.testing.uihelper.UiDeviceUtils; 36 37/** 38 * Base test case for LiveChannel UI tests. 39 */ 40public abstract class LiveChannelsTestCase extends InstrumentationTestCase { 41 protected final TestInputControlConnection mConnection = new TestInputControlConnection(); 42 43 protected UiDevice mDevice; 44 protected Resources mTargetResources; 45 protected MenuHelper mMenuHelper; 46 protected SidePanelHelper mSidePanelHelper; 47 protected LiveChannelsUiDeviceHelper mLiveChannelsHelper; 48 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 Context context = getInstrumentation().getContext(); 53 context.bindService(TestInputControlUtils.createIntent(), mConnection, 54 Context.BIND_AUTO_CREATE); 55 mDevice = UiDevice.getInstance(getInstrumentation()); 56 mTargetResources = getInstrumentation().getTargetContext().getResources(); 57 mMenuHelper = new MenuHelper(mDevice, mTargetResources); 58 mSidePanelHelper = new SidePanelHelper(mDevice, mTargetResources); 59 mLiveChannelsHelper = new LiveChannelsUiDeviceHelper(mDevice, mTargetResources, context); 60 } 61 62 @Override 63 protected void tearDown() throws Exception { 64 if (mConnection.isBound()) { 65 getInstrumentation().getContext().unbindService(mConnection); 66 } 67 68 // TODO: robustly handle left over pops from failed tests. 69 // Clear any side panel, menu, ... 70 // Scene container should not be checked here because pressing the BACK key in some scenes 71 // might launch the home screen. 72 if (mDevice.hasObject(Constants.SIDE_PANEL) || mDevice.hasObject(Constants.MENU) || mDevice 73 .hasObject(Constants.PROGRAM_GUIDE)) { 74 mDevice.pressBack(); 75 } 76 super.tearDown(); 77 } 78 79 /** 80 * Send the keys for the channel number of {@code channel} and press the DPAD 81 * center. 82 * 83 * <p>Usually this will tune to the given channel. 84 */ 85 protected void pressKeysForChannel(ChannelInfo channel) { 86 UiDeviceUtils.pressKeys(mDevice, channel.number); 87 assertWaitForCondition(mDevice, Until.hasObject(Constants.KEYPAD_CHANNEL_SWITCH)); 88 mDevice.pressDPadCenter(); 89 } 90 91 /** 92 * Update the channel state to {@code data} then tune to that channel. 93 * 94 * @param data the state to update the channel with. 95 * @param channel the channel to tune to 96 */ 97 protected void updateThenTune(ChannelStateData data, ChannelInfo channel) { 98 mConnection.updateChannelState(channel, data); 99 pressKeysForChannel(channel); 100 } 101} 102