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.menu;
17
18import android.media.tv.TvTrackInfo;
19import android.os.SystemClock;
20import android.test.suitebuilder.annotation.MediumTest;
21
22import com.android.tv.BaseMainActivityTestCase;
23import com.android.tv.MainActivity;
24import com.android.tv.customization.CustomAction;
25import com.android.tv.testing.Constants;
26import com.android.tv.testing.testinput.ChannelStateData;
27import com.android.tv.testing.testinput.TvTestInputConstants;
28
29import java.util.Collections;
30import java.util.List;
31
32/**
33 * Tests for {@link TvOptionsRowAdapter}.
34 */
35@MediumTest
36public class TvOptionsRowAdapterTest extends BaseMainActivityTestCase {
37    private static final int WAIT_TRACK_SIZE_TIMEOUT_MS = 300;
38    public static final int TRACK_SIZE_CHECK_INTERVAL_MS = 10;
39
40    // TODO: Refactor TvOptionsRowAdapter so it does not rely on MainActivity
41    private TvOptionsRowAdapter mTvOptionsRowAdapter;
42
43    public TvOptionsRowAdapterTest() {
44        super(MainActivity.class);
45    }
46
47    @Override
48    protected void setUp() throws Exception {
49        super.setUp();
50        mTvOptionsRowAdapter = new TvOptionsRowAdapter(mActivity,
51                Collections.<CustomAction>emptyList());
52        tuneToChannel(TvTestInputConstants.CH_1_DEFAULT_DONT_MODIFY);
53        waitUntilAudioTracksHaveSize(1);
54        mTvOptionsRowAdapter.update();
55    }
56
57    public void testUpdateAudioAction_2tracks() {
58        ChannelStateData data = new ChannelStateData();
59        data.mTvTrackInfos.add(Constants.GENERIC_AUDIO_TRACK);
60        updateThenTune(data, TvTestInputConstants.CH_2);
61        waitUntilAudioTracksHaveSize(2);
62
63        boolean result = mTvOptionsRowAdapter.updateMultiAudioAction();
64        assertEquals("update Action had change", true, result);
65        assertEquals("Multi Audio enabled", true,
66                MenuAction.SELECT_AUDIO_LANGUAGE_ACTION.isEnabled());
67    }
68
69    public void testUpdateAudioAction_1track() {
70        ChannelStateData data = new ChannelStateData();
71        data.mTvTrackInfos.clear();
72        data.mTvTrackInfos.add(Constants.GENERIC_AUDIO_TRACK);
73        updateThenTune(data, TvTestInputConstants.CH_2);
74        waitUntilAudioTracksHaveSize(1);
75
76        boolean result = mTvOptionsRowAdapter.updateMultiAudioAction();
77        assertEquals("update Action had change", false, result);
78        assertEquals("Multi Audio enabled", false,
79                MenuAction.SELECT_AUDIO_LANGUAGE_ACTION.isEnabled());
80    }
81
82    public void testUpdateAudioAction_noTracks() {
83        ChannelStateData data = new ChannelStateData();
84        data.mTvTrackInfos.clear();
85        updateThenTune(data, TvTestInputConstants.CH_2);
86        waitUntilAudioTracksHaveSize(0);
87
88        boolean result = mTvOptionsRowAdapter.updateMultiAudioAction();
89        assertEquals("update Action had change", false, result);
90        assertEquals("Multi Audio enabled", false,
91                MenuAction.SELECT_AUDIO_LANGUAGE_ACTION.isEnabled());
92    }
93
94    private void waitUntilAudioTracksHaveSize(int expected) {
95        long start = SystemClock.elapsedRealtime();
96        int size = -1;
97        while (SystemClock.elapsedRealtime() < start + WAIT_TRACK_SIZE_TIMEOUT_MS) {
98            getInstrumentation().waitForIdleSync();
99            List<TvTrackInfo> tracks = mActivity.getTracks(TvTrackInfo.TYPE_AUDIO);
100            if (tracks != null) {
101                size = tracks.size();
102                if (size == expected) {
103                    return;
104                }
105            }
106            SystemClock.sleep(TRACK_SIZE_CHECK_INTERVAL_MS);
107        }
108        fail("Waited for " + WAIT_TRACK_SIZE_TIMEOUT_MS + " milliseconds for track size to be "
109                + expected + " but was " + size);
110    }
111}
112