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