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