Home | History | Annotate | Download | only in activities
      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.testing.activities;
     17 
     18 import static android.support.test.InstrumentationRegistry.getInstrumentation;
     19 
     20 import android.content.Context;
     21 import android.os.SystemClock;
     22 import android.support.test.rule.ActivityTestRule;
     23 import android.text.TextUtils;
     24 import com.android.tv.MainActivity;
     25 import com.android.tv.data.ChannelDataManager;
     26 import com.android.tv.data.api.Channel;
     27 import com.android.tv.testing.data.ChannelInfo;
     28 import com.android.tv.testing.testinput.ChannelStateData;
     29 import com.android.tv.testing.testinput.TestInputControlConnection;
     30 import com.android.tv.testing.testinput.TestInputControlUtils;
     31 import com.android.tv.testing.testinput.TvTestInputConstants;
     32 import java.util.List;
     33 import org.junit.Before;
     34 import org.junit.Rule;
     35 
     36 /** Base TestCase for tests that need a {@link MainActivity}. */
     37 public abstract class BaseMainActivityTestCase {
     38     private static final String TAG = "BaseMainActivityTest";
     39     private static final int CHANNEL_LOADING_CHECK_INTERVAL_MS = 10;
     40 
     41     @Rule
     42     public ActivityTestRule<MainActivity> mActivityTestRule =
     43             new ActivityTestRule<>(MainActivity.class);
     44 
     45     protected final TestInputControlConnection mConnection = new TestInputControlConnection();
     46 
     47     protected MainActivity mActivity;
     48 
     49     @Before
     50     public void setUp() {
     51         mActivity = mActivityTestRule.getActivity();
     52         // TODO: ensure the SampleInputs are setup.
     53         getInstrumentation()
     54                 .getTargetContext()
     55                 .bindService(
     56                         TestInputControlUtils.createIntent(),
     57                         mConnection,
     58                         Context.BIND_AUTO_CREATE);
     59     }
     60 
     61     @Before
     62     public void tearDown() {
     63         if (mConnection.isBound()) {
     64             getInstrumentation().getTargetContext().unbindService(mConnection);
     65         }
     66     }
     67 
     68     /**
     69      * Tune to {@code channel}.
     70      *
     71      * @param channel the channel to tune to.
     72      */
     73     protected void tuneToChannel(final Channel channel) {
     74         // Run on UI thread so views can be modified
     75         getInstrumentation()
     76                 .runOnMainSync(
     77                         new Runnable() {
     78                             @Override
     79                             public void run() {
     80                                 mActivity.tuneToChannel(channel);
     81                             }
     82                         });
     83     }
     84 
     85     /** Sleep until @{@link ChannelDataManager#isDbLoadFinished()} is true. */
     86     protected void waitUntilChannelLoadingFinish() {
     87         ChannelDataManager channelDataManager = mActivity.getChannelDataManager();
     88         while (!channelDataManager.isDbLoadFinished()) {
     89             getInstrumentation().waitForIdleSync();
     90             SystemClock.sleep(CHANNEL_LOADING_CHECK_INTERVAL_MS);
     91         }
     92     }
     93 
     94     /**
     95      * Tune to the channel with {@code name}.
     96      *
     97      * @param name the name of the channel to find.
     98      */
     99     protected void tuneToChannel(String name) {
    100         Channel c = findChannelWithName(name);
    101         tuneToChannel(c);
    102     }
    103 
    104     /** Tune to channel. */
    105     protected void tuneToChannel(ChannelInfo channel) {
    106         tuneToChannel(channel.name);
    107     }
    108 
    109     /**
    110      * Update the channel state to {@code data} then tune to that channel.
    111      *
    112      * @param data the state to update the channel with.
    113      * @param channel the channel to tune to
    114      */
    115     protected void updateThenTune(ChannelStateData data, ChannelInfo channel) {
    116         if (channel.equals(TvTestInputConstants.CH_1_DEFAULT_DONT_MODIFY)) {
    117             throw new IllegalArgumentException(
    118                     "By convention "
    119                             + TvTestInputConstants.CH_1_DEFAULT_DONT_MODIFY.name
    120                             + " should not be modified.");
    121         }
    122         mConnection.updateChannelState(channel, data);
    123         tuneToChannel(channel);
    124     }
    125 
    126     private Channel findChannelWithName(String displayName) {
    127         waitUntilChannelLoadingFinish();
    128         Channel channel = null;
    129         List<Channel> channelList = mActivity.getChannelDataManager().getChannelList();
    130         for (Channel c : channelList) {
    131             if (TextUtils.equals(c.getDisplayName(), displayName)) {
    132                 channel = c;
    133                 break;
    134             }
    135         }
    136         if (channel == null) {
    137             throw new AssertionError("'" + displayName + "' channel not found");
    138         }
    139         return channel;
    140     }
    141 }
    142