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 */
16package com.android.tv;
17
18import static android.support.test.InstrumentationRegistry.getInstrumentation;
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21
22import android.support.test.filters.MediumTest;
23import android.view.View;
24import android.widget.TextView;
25
26import com.android.tv.data.Channel;
27import com.android.tv.testing.testinput.TvTestInputConstants;
28import com.android.tv.ui.ChannelBannerView;
29
30import org.junit.Test;
31
32import java.util.List;
33
34/**
35 * Tests for {@link MainActivity}.
36 */
37@MediumTest
38public class MainActivityTest extends BaseMainActivityTestCase {
39    @Test
40    public void testInitialConditions() {
41        waitUntilChannelLoadingFinish();
42        List<Channel> channelList = mActivity.getChannelDataManager().getChannelList();
43        assertTrue("Expected at least one channel", channelList.size() > 0);
44    }
45
46    @Test
47    public void testTuneToChannel() {
48        tuneToChannel(TvTestInputConstants.CH_2);
49        assertChannelBannerShown(true);
50        assertChannelName(TvTestInputConstants.CH_2.name);
51    }
52
53    @Test
54    public void testShowProgramGuide() {
55        tuneToChannel(TvTestInputConstants.CH_2);
56        showProgramGuide();
57        getInstrumentation().waitForIdleSync();
58        assertChannelBannerShown(false);
59        assertProgramGuide(true);
60    }
61
62    private void showProgramGuide() {
63        // Run on UI thread so views can be modified
64        getInstrumentation().runOnMainSync(new Runnable() {
65            @Override
66            public void run() {
67                mActivity.getOverlayManager().showProgramGuide();
68            }
69        });
70    }
71
72    private void assertChannelName(String displayName) {
73        TextView channelNameView = (TextView) mActivity.findViewById(R.id.channel_name);
74        assertEquals("Channel Name", displayName, channelNameView.getText());
75    }
76
77    private void assertProgramGuide(boolean isShown) {
78        assertViewIsShown("Program Guide", R.id.program_guide, isShown);
79    }
80
81    private ChannelBannerView assertChannelBannerShown(boolean isShown) {
82        View v = assertExpectedBannerSceneClassShown(ChannelBannerView.class, isShown);
83        return (ChannelBannerView) v;
84    }
85
86    private View assertExpectedBannerSceneClassShown(Class<ChannelBannerView> expectedClass,
87            boolean expectedShown) {
88        View v = assertViewIsShown(expectedClass.getSimpleName(), R.id.scene_transition_common,
89                expectedShown);
90        if (v != null) {
91            assertEquals(expectedClass, v.getClass());
92        }
93        return v;
94    }
95
96    private View assertViewIsShown(String viewName, int viewId, boolean expected) {
97        View view = mActivity.findViewById(viewId);
98        if (view == null) {
99            if (expected) {
100                throw new AssertionError("View " + viewName + " not found");
101            } else {
102                return null;
103            }
104        }
105        assertEquals(viewName + " shown", expected, view.isShown());
106        return view;
107    }
108}
109