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