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.Constants.FOCUSED_VIEW;
20import static com.android.tv.testing.uihelper.Constants.MENU;
21import static com.android.tv.testing.uihelper.UiDeviceAsserts.assertWaitForCondition;
22
23import android.support.test.filters.SdkSuppress;
24import android.support.test.uiautomator.BySelector;
25import android.support.test.uiautomator.UiObject2;
26import android.support.test.uiautomator.Until;
27import android.test.suitebuilder.annotation.SmallTest;
28import android.view.KeyEvent;
29
30import com.android.tv.R;
31import com.android.tv.testing.testinput.TvTestInputConstants;
32import com.android.tv.testing.uihelper.DialogHelper;
33
34@SmallTest
35@SdkSuppress(minSdkVersion = 23)
36public class PlayControlsRowViewTest extends LiveChannelsTestCase {
37    private static final int BUTTON_INDEX_PLAY_PAUSE = 2;
38
39    private BySelector mBySettingsSidePanel;
40
41    @Override
42    protected void setUp() throws Exception {
43        super.setUp();
44        mLiveChannelsHelper.assertAppStarted();
45        pressKeysForChannel(TvTestInputConstants.CH_1_DEFAULT_DONT_MODIFY);
46        // Tune to a new channel to ensure that the channel is changed.
47        mDevice.pressDPadUp();
48        getInstrumentation().waitForIdleSync();
49        mBySettingsSidePanel = mSidePanelHelper.bySidePanelTitled(
50                R.string.side_panel_title_settings);
51    }
52
53    /**
54     * Test the normal case. The play/pause button should have focus initially.
55     */
56    public void testFocusedViewInNormalCase() {
57        mMenuHelper.showMenu();
58        mMenuHelper.assertNavigateToPlayControlsRow();
59        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
60        mDevice.pressBack();
61    }
62
63    /**
64     * Tests the case when the forwarding action is disabled.
65     * In this case, the button corresponding to the action is disabled, so play/pause button should
66     * have the focus.
67     */
68    public void testFocusedViewWithDisabledActionForward() {
69        // Fast forward button
70        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD);
71        mMenuHelper.assertWaitForMenu();
72        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
73        mDevice.pressBack();
74
75        // Next button
76        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_NEXT);
77        mMenuHelper.assertWaitForMenu();
78        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
79        mDevice.pressBack();
80    }
81
82    /**
83     * Tests the case when the rewinding action is disabled.
84     * In this case, the button corresponding to the action is disabled, so play/pause button should
85     * have the focus.
86     */
87    public void testFocusedViewWithDisabledActionBackward() {
88        // Previous button
89        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
90        mMenuHelper.assertWaitForMenu();
91        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
92        mDevice.pressBack();
93
94        // Rewind button
95        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_REWIND);
96        mMenuHelper.assertWaitForMenu();
97        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
98        mDevice.pressBack();
99    }
100
101    public void testFocusedViewInMenu() {
102        mMenuHelper.showMenu();
103        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_PLAY);
104        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
105        mMenuHelper.assertNavigateToRow(R.string.menu_title_channels);
106        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_NEXT);
107        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
108    }
109
110    public void testKeepPausedWhileParentalControlChange() {
111        // Pause the playback.
112        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_PAUSE);
113        mMenuHelper.assertWaitForMenu();
114        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
115        // Show parental controls fragment.
116        mMenuHelper.assertPressOptionsSettings();
117        assertWaitForCondition(mDevice, Until.hasObject(mBySettingsSidePanel));
118        mSidePanelHelper.assertNavigateToItem(R.string.settings_parental_controls);
119        mDevice.pressDPadCenter();
120        DialogHelper dialogHelper = new DialogHelper(mDevice, mTargetResources);
121        dialogHelper.assertWaitForPinDialogOpen();
122        dialogHelper.enterPinCodes();
123        dialogHelper.assertWaitForPinDialogClose();
124        BySelector bySidePanel = mSidePanelHelper.bySidePanelTitled(
125                R.string.menu_parental_controls);
126        assertWaitForCondition(mDevice, Until.hasObject(bySidePanel));
127        mDevice.pressEnter();
128        mDevice.pressEnter();
129        mDevice.pressBack();
130        mDevice.pressBack();
131        // Return to the main menu.
132        mMenuHelper.assertWaitForMenu();
133        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
134    }
135
136    public void testKeepPausedAfterVisitingHome() {
137        // Pause the playback.
138        mDevice.pressKeyCode(KeyEvent.KEYCODE_MEDIA_PAUSE);
139        mMenuHelper.assertWaitForMenu();
140        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
141        // Press HOME twice to visit the home screen and return to Live TV.
142        mDevice.pressHome();
143        // Wait until home screen is shown.
144        mDevice.waitForIdle();
145        mDevice.pressHome();
146        // Wait until TV is resumed.
147        mDevice.waitForIdle();
148        // Return to the main menu.
149        mMenuHelper.assertWaitForMenu();
150        assertButtonHasFocus(BUTTON_INDEX_PLAY_PAUSE);
151    }
152
153    private void assertButtonHasFocus(int expectedButtonIndex) {
154        UiObject2 menu = mDevice.findObject(MENU);
155        UiObject2 focusedView = menu.findObject(FOCUSED_VIEW);
156        assertNotNull("Play controls row doesn't have a focused child.", focusedView);
157        UiObject2 focusedButtonGroup = focusedView.getParent();
158        assertNotNull("The focused item should have parent", focusedButtonGroup);
159        UiObject2 controlBar = focusedButtonGroup.getParent();
160        assertNotNull("The focused item should have grandparent", controlBar);
161        assertTrue("The grandparent should have more than five children",
162                controlBar.getChildCount() >= 5);
163        assertEquals(controlBar.getChildren().get(expectedButtonIndex), focusedButtonGroup);
164    }
165}
166