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