diff options
Diffstat (limited to 'tests/unit/src/com/android/tv')
14 files changed, 431 insertions, 63 deletions
diff --git a/tests/unit/src/com/android/tv/BaseMainActivityTestCase.java b/tests/unit/src/com/android/tv/BaseMainActivityTestCase.java index 208085d6..e2f620ed 100644 --- a/tests/unit/src/com/android/tv/BaseMainActivityTestCase.java +++ b/tests/unit/src/com/android/tv/BaseMainActivityTestCase.java @@ -18,6 +18,7 @@ package com.android.tv; import android.content.Context; import android.os.SystemClock; import android.test.ActivityInstrumentationTestCase2; +import android.text.TextUtils; import com.android.tv.data.Channel; import com.android.tv.data.ChannelDataManager; @@ -131,7 +132,7 @@ public abstract class BaseMainActivityTestCase waitUntilChannelLoadingFinish(); List<Channel> channelList = mActivity.getChannelDataManager().getChannelList(); for (Channel c : channelList) { - if (c.getDisplayName().equals(displayName)) { + if (TextUtils.equals(c.getDisplayName(), displayName)) { return c; } } diff --git a/tests/unit/src/com/android/tv/FeaturesTest.java b/tests/unit/src/com/android/tv/FeaturesTest.java new file mode 100644 index 00000000..a0a6a5be --- /dev/null +++ b/tests/unit/src/com/android/tv/FeaturesTest.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.tv; + +import android.test.suitebuilder.annotation.SmallTest; + +import junit.framework.TestCase; + +/** + * Test for features. + */ +@SmallTest +public class FeaturesTest extends TestCase { + + public void testPropertyFeatureKeyLength() { + // This forces the class to be loaded and verifies all PropertyFeature key lengths. + // If any keys are too long the test will fail to load. + assertEquals(false, Features.TEST_FEATURE.isEnabled(null)); + } +} diff --git a/tests/unit/src/com/android/tv/MainActivityTest.java b/tests/unit/src/com/android/tv/MainActivityTest.java index 04271a7b..a09111e8 100644 --- a/tests/unit/src/com/android/tv/MainActivityTest.java +++ b/tests/unit/src/com/android/tv/MainActivityTest.java @@ -58,7 +58,7 @@ public class MainActivityTest extends BaseMainActivityTestCase { private void showProgramGuide() throws Throwable { // Run on UI thread so views can be modified - runTestOnUiThread(new Runnable() { + getInstrumentation().runOnMainSync(new Runnable() { @Override public void run() { mActivity.getOverlayManager().showProgramGuide(); diff --git a/tests/unit/src/com/android/tv/data/ChannelDataManagerTest.java b/tests/unit/src/com/android/tv/data/ChannelDataManagerTest.java index b239d0ed..fa127369 100644 --- a/tests/unit/src/com/android/tv/data/ChannelDataManagerTest.java +++ b/tests/unit/src/com/android/tv/data/ChannelDataManagerTest.java @@ -25,9 +25,9 @@ import android.database.Cursor; import android.media.tv.TvContract; import android.media.tv.TvContract.Channels; import android.net.Uri; -import android.os.HandlerThread; import android.test.AndroidTestCase; import android.test.MoreAsserts; +import android.test.UiThreadTest; import android.test.mock.MockContentProvider; import android.test.mock.MockContentResolver; import android.test.mock.MockCursor; @@ -39,6 +39,7 @@ import android.util.SparseArray; import com.android.tv.analytics.StubTracker; import com.android.tv.testing.ChannelInfo; import com.android.tv.testing.Constants; +import com.android.tv.testing.Utils; import com.android.tv.util.TvInputManagerHelper; import org.mockito.Matchers; @@ -51,9 +52,10 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** - * Test for {@link com.android.tv.data.ChannelDataManager} + * Test for {@link ChannelDataManager} * * A test method may include tests for multiple methods to minimize the DB access. + * Note that all the methods of {@link ChannelDataManager} should be called from the UI thread. */ @SmallTest public class ChannelDataManagerTest extends AndroidTestCase { @@ -68,7 +70,6 @@ public class ChannelDataManagerTest extends AndroidTestCase { private static final String COLUMN_LOCKED = "locked"; private ChannelDataManager mChannelDataManager; - private HandlerThread mHandlerThread; private TestChannelDataManagerListener mListener; private FakeContentResolver mContentResolver; private FakeContentProvider mContentProvider; @@ -77,26 +78,32 @@ public class ChannelDataManagerTest extends AndroidTestCase { protected void setUp() throws Exception { super.setUp(); assertTrue("More than 2 channels to test", Constants.UNIT_TEST_CHANNEL_COUNT > 2); - TvInputManagerHelper mockHelper = Mockito.mock(TvInputManagerHelper.class); - Mockito.when(mockHelper.hasTvInputInfo(Matchers.anyString())).thenReturn(true); mContentProvider = new FakeContentProvider(getContext()); mContentResolver = new FakeContentResolver(); mContentResolver.addProvider(TvContract.AUTHORITY, mContentProvider); - mHandlerThread = new HandlerThread(TAG); - mHandlerThread.start(); - mChannelDataManager = new ChannelDataManager(getContext(), mockHelper, new StubTracker(), - mContentResolver, mHandlerThread.getLooper()); mListener = new TestChannelDataManagerListener(); - mChannelDataManager.addListener(mListener); - + Utils.runOnMainSync(new Runnable() { + @Override + public void run() { + TvInputManagerHelper mockHelper = Mockito.mock(TvInputManagerHelper.class); + Mockito.when(mockHelper.hasTvInputInfo(Matchers.anyString())).thenReturn(true); + mChannelDataManager = new ChannelDataManager(getContext(), mockHelper, + new StubTracker(), mContentResolver); + mChannelDataManager.addListener(mListener); + } + }); } @Override protected void tearDown() throws Exception { + Utils.runOnMainSync(new Runnable() { + @Override + public void run() { + mChannelDataManager.stop(); + } + }); super.tearDown(); - mHandlerThread.quitSafely(); - mChannelDataManager.stop(); } private void startAndWaitForComplete() throws Exception { @@ -114,6 +121,7 @@ public class ChannelDataManagerTest extends AndroidTestCase { startAndWaitForComplete(); } + @UiThreadTest public void testIsDbLoadFinished() throws Exception { startAndWaitForComplete(); assertTrue(mChannelDataManager.isDbLoadFinished()); @@ -125,6 +133,7 @@ public class ChannelDataManagerTest extends AndroidTestCase { * - {@link ChannelDataManager#getChannelList} * - {@link ChannelDataManager#getChannel} */ + @UiThreadTest public void testGetChannels() throws Exception { startAndWaitForComplete(); @@ -159,6 +168,7 @@ public class ChannelDataManagerTest extends AndroidTestCase { /** * Test for {@link ChannelDataManager#getChannelCount} when no channel is available. */ + @UiThreadTest public void testGetChannels_noChannels() throws Exception { mContentProvider.clear(); startAndWaitForComplete(); @@ -170,6 +180,7 @@ public class ChannelDataManagerTest extends AndroidTestCase { * - {@link ChannelDataManager#updateBrowsable} * - {@link ChannelDataManager#applyUpdatedValuesToDb} */ + @UiThreadTest public void testBrowsable() throws Exception { startAndWaitForComplete(); @@ -208,6 +219,7 @@ public class ChannelDataManagerTest extends AndroidTestCase { * - {@link ChannelDataManager#updateBrowsable} * - {@link ChannelDataManager#applyUpdatedValuesToDb} */ + @UiThreadTest public void testBrowsable_skipNotification() throws Exception { startAndWaitForComplete(); @@ -241,6 +253,7 @@ public class ChannelDataManagerTest extends AndroidTestCase { * - {@link ChannelDataManager#updateLocked} * - {@link ChannelDataManager#applyUpdatedValuesToDb} */ + @UiThreadTest public void testLocked() throws Exception { startAndWaitForComplete(); @@ -269,6 +282,7 @@ public class ChannelDataManagerTest extends AndroidTestCase { /** * Test ChannelDataManager when channels in TvContract are updated, removed, or added. */ + @UiThreadTest public void testChannelListChanged() throws Exception { startAndWaitForComplete(); @@ -336,8 +350,8 @@ public class ChannelDataManagerTest extends AndroidTestCase { if (DEBUG) { Log.d(TAG, "onChanged(uri=" + uri + ", observer=" + observer + ")"); } - // Do not call {@link ContentObserver#onChange} directly - // to run it on the {@link #mHandlerThread}. + // Do not call {@link ContentObserver#onChange} directly to run it on the correct + // thread. if (observer != null) { observer.dispatchChange(false, uri); } else { diff --git a/tests/unit/src/com/android/tv/data/ChannelNumberTest.java b/tests/unit/src/com/android/tv/data/ChannelNumberTest.java index 1935b4d9..1dd18da0 100644 --- a/tests/unit/src/com/android/tv/data/ChannelNumberTest.java +++ b/tests/unit/src/com/android/tv/data/ChannelNumberTest.java @@ -43,6 +43,9 @@ public class ChannelNumberTest extends TestCase { public void testParseChannelNumber() { assertNull(parseChannelNumber("")); assertNull(parseChannelNumber(" ")); + assertNull(parseChannelNumber("abcd12")); + assertNull(parseChannelNumber("12abcd")); + assertNull(parseChannelNumber("-12")); assertChannelEquals(parseChannelNumber("1"), "1", false, ""); assertChannelEquals(parseChannelNumber("1234 4321"), "1234", true, "4321"); assertChannelEquals(parseChannelNumber("3-4"), "3", true, "4"); @@ -76,6 +79,8 @@ public class ChannelNumberTest extends TestCase { assertEquals("compareTo(null,null)", 0, ChannelNumber.compare(null, null)); assertEquals("compareTo(1,1)", 0, ChannelNumber.compare("1", "1")); assertEquals("compareTo(null,1)<0", true, ChannelNumber.compare(null, "1") < 0); + assertEquals("compareTo(mal-formatted,1)<0", true, ChannelNumber.compare("abcd", "1") < 0); + assertEquals("compareTo(mal-formatted,1)<0", true, ChannelNumber.compare(".4", "1") < 0); assertEquals("compareTo(1,null)>0", true, ChannelNumber.compare("1", null) > 0); } diff --git a/tests/unit/src/com/android/tv/data/ProgramDataManagerTest.java b/tests/unit/src/com/android/tv/data/ProgramDataManagerTest.java index 60e3e210..78b3a930 100644 --- a/tests/unit/src/com/android/tv/data/ProgramDataManagerTest.java +++ b/tests/unit/src/com/android/tv/data/ProgramDataManagerTest.java @@ -27,7 +27,6 @@ import android.test.mock.MockContentProvider; import android.test.mock.MockContentResolver; import android.test.mock.MockCursor; import android.test.suitebuilder.annotation.SmallTest; -import android.text.TextUtils; import android.util.Log; import android.util.SparseArray; @@ -80,6 +79,7 @@ public class ProgramDataManagerTest extends AndroidTestCase { mHandlerThread.start(); mProgramDataManager = new ProgramDataManager( mContentResolver, mClock, mHandlerThread.getLooper()); + mProgramDataManager.setPrefetchEnabled(true); mProgramDataManager.addListener(mListener); } @@ -95,14 +95,6 @@ public class ProgramDataManagerTest extends AndroidTestCase { assertTrue(mListener.programUpdatedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS)); } - private static boolean equals(ProgramInfo lhs, long lhsStarTimeMs, Program rhs) { - return TextUtils.equals(lhs.title, rhs.getTitle()) - && TextUtils.equals(lhs.episode, rhs.getEpisodeTitle()) - && TextUtils.equals(lhs.description, rhs.getDescription()) - && lhsStarTimeMs == rhs.getStartTimeUtcMillis() - && lhsStarTimeMs + lhs.durationMs == rhs.getEndTimeUtcMillis(); - } - /** * Test for {@link ProgramInfo#getIndex} and {@link ProgramInfo#getStartTimeMs}. */ @@ -150,8 +142,7 @@ public class ProgramDataManagerTest extends AndroidTestCase { for (Program program : programs) { ProgramInfo programInfoAt = stub.build(getContext(), index); long startTimeMs = stub.getStartTimeMs(index, channelId); - assertTrue(program.toString() + " differ from " + programInfoAt, - equals(programInfoAt, startTimeMs, program)); + assertProgramEquals(startTimeMs, programInfoAt, program); index++; } // Case #2: Corner cases where there's a program that starts at the start of the range. @@ -194,13 +185,12 @@ public class ProgramDataManagerTest extends AndroidTestCase { TestProgramDataManagerOnCurrentProgramUpdatedListener listener = new TestProgramDataManagerOnCurrentProgramUpdatedListener(); mProgramDataManager.addOnCurrentProgramUpdatedListener(testChannelId, listener); - assertTrue(listener.currentProgramUpdatedLatch.await(WAIT_TIME_OUT_MS, - TimeUnit.MILLISECONDS)); + assertTrue( + listener.currentProgramUpdatedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS)); assertEquals(testChannelId, listener.updatedChannelId); - assertTrue(ProgramDataManagerTest.equals( - nextProgramInfo, nextProgramStartTimeMs, - mProgramDataManager.getCurrentProgram(testChannelId))); - assertEquals(listener.updatedProgram, mProgramDataManager.getCurrentProgram(testChannelId)); + Program currentProgram = mProgramDataManager.getCurrentProgram(testChannelId); + assertProgramEquals(nextProgramStartTimeMs, nextProgramInfo, currentProgram); + assertEquals(listener.updatedProgram, currentProgram); } /** @@ -220,8 +210,8 @@ public class ProgramDataManagerTest extends AndroidTestCase { mContentProvider.simulateAppend(testChannelId); assertTrue(mListener.programUpdatedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS)); programList = mProgramDataManager.getPrograms(testChannelId, mClock.currentTimeMillis()); - assertTrue(lastProgramEndTime - < programList.get(programList.size() - 1).getEndTimeUtcMillis()); + assertTrue( + lastProgramEndTime < programList.get(programList.size() - 1).getEndTimeUtcMillis()); } /** @@ -239,6 +229,16 @@ public class ProgramDataManagerTest extends AndroidTestCase { TimeUnit.MILLISECONDS)); } + public static void assertProgramEquals(long expectedStartTime, ProgramInfo expectedInfo, + Program actualProgram) { + assertEquals("title", expectedInfo.title, actualProgram.getTitle()); + assertEquals("episode", expectedInfo.episode, actualProgram.getEpisodeTitle()); + assertEquals("description", expectedInfo.description, actualProgram.getDescription()); + assertEquals("startTime", expectedStartTime, actualProgram.getStartTimeUtcMillis()); + assertEquals("endTime", expectedStartTime + expectedInfo.durationMs, + actualProgram.getEndTimeUtcMillis()); + } + private class FakeContentResolver extends MockContentResolver { @Override public void notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork) { diff --git a/tests/unit/src/com/android/tv/data/WatchedHistoryManagerTest.java b/tests/unit/src/com/android/tv/data/WatchedHistoryManagerTest.java new file mode 100644 index 00000000..e5da60eb --- /dev/null +++ b/tests/unit/src/com/android/tv/data/WatchedHistoryManagerTest.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tv.data; + +import android.test.AndroidTestCase; +import android.test.UiThreadTest; +import android.test.suitebuilder.annotation.SmallTest; + +import com.android.tv.data.WatchedHistoryManager.WatchedRecord; +import com.android.tv.testing.Utils; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * Test for {@link com.android.tv.data.WatchedHistoryManagerTest} + */ +@SmallTest +public class WatchedHistoryManagerTest extends AndroidTestCase { + private static final boolean DEBUG = false; + private static final String TAG = "WatchedHistoryManager"; + + // Wait time for expected success. + private static final long WAIT_TIME_OUT_MS = 1000L; + private static final int MAX_HISTORY_SIZE = 100; + + private WatchedHistoryManager mWatchedHistoryManager; + private TestWatchedHistoryManagerListener mListener; + + @Override + protected void setUp() throws Exception { + super.setUp(); + Utils.runOnMainSync(new Runnable() { + @Override + public void run() { + mWatchedHistoryManager = new WatchedHistoryManager(getContext(), MAX_HISTORY_SIZE); + mListener = new TestWatchedHistoryManagerListener(); + mWatchedHistoryManager.setListener(mListener); + } + }); + } + + private void startAndWaitForComplete() throws Exception { + mWatchedHistoryManager.start(); + try { + assertTrue(mListener.loadFinishedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS)); + } catch (InterruptedException e) { + throw e; + } + } + + @UiThreadTest + public void testIsLoaded() throws Exception { + assertFalse(mWatchedHistoryManager.isLoaded()); + startAndWaitForComplete(); + assertTrue(mWatchedHistoryManager.isLoaded()); + } + + @UiThreadTest + public void testLogChannelViewStop() throws Exception { + startAndWaitForComplete(); + long fakeId = 100000000; + long time = System.currentTimeMillis(); + long duration = TimeUnit.MINUTES.toMillis(10); + Channel channel = new Channel.Builder().setId(fakeId).build(); + mWatchedHistoryManager.logChannelViewStop(channel, time, duration); + + WatchedRecord record = mWatchedHistoryManager.getRecord(0); + WatchedRecord recordFromSharedPreferences = + mWatchedHistoryManager.getRecordFromSharedPreferences(0); + assertEquals(record.channelId, fakeId); + assertEquals(record.watchedStartTime, time - duration); + assertEquals(record.duration, duration); + assertEquals(record, recordFromSharedPreferences); + } + + @UiThreadTest + public void testCircularHistoryQueue() throws Exception { + startAndWaitForComplete(); + final long startChannelId = 100000000; + long time = System.currentTimeMillis(); + long duration = TimeUnit.MINUTES.toMillis(10); + + int size = MAX_HISTORY_SIZE * 2; + for (int i = 0; i < size; ++i) { + Channel channel = new Channel.Builder().setId(startChannelId + i).build(); + mWatchedHistoryManager.logChannelViewStop(channel, time + duration * i, duration); + } + for (int i = 0; i < MAX_HISTORY_SIZE; ++i) { + WatchedRecord record = mWatchedHistoryManager.getRecord(i); + WatchedRecord recordFromSharedPreferences = + mWatchedHistoryManager.getRecordFromSharedPreferences(i); + assertEquals(record, recordFromSharedPreferences); + assertEquals(record.channelId, startChannelId + size - 1 - i); + } + // Since the WatchedHistory is a circular queue, the value for 0 and maxHistorySize + // are same. + assertEquals(mWatchedHistoryManager.getRecordFromSharedPreferences(0), + mWatchedHistoryManager.getRecordFromSharedPreferences(MAX_HISTORY_SIZE)); + } + + @UiThreadTest + public void testWatchedRecordEquals() { + assertTrue(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(1, 2, 3))); + assertFalse(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(1, 2, 4))); + assertFalse(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(1, 4, 3))); + assertFalse(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(4, 2, 3))); + } + + @UiThreadTest + public void testEncodeDecodeWatchedRecord() throws Exception { + long fakeId = 100000000; + long time = System.currentTimeMillis(); + long duration = TimeUnit.MINUTES.toMillis(10); + WatchedRecord record = new WatchedRecord(fakeId, time, duration); + WatchedRecord sameRecord = mWatchedHistoryManager.decode( + mWatchedHistoryManager.encode(record)); + assertEquals(record, sameRecord); + } + + private class TestWatchedHistoryManagerListener implements WatchedHistoryManager.Listener { + public CountDownLatch loadFinishedLatch = new CountDownLatch(1); + + @Override + public void onLoadFinished() { + loadFinishedLatch.countDown(); + } + + @Override + public void onNewRecordAdded(WatchedRecord watchedRecord) { } + } +} diff --git a/tests/unit/src/com/android/tv/recommendation/ChannelRecordTest.java b/tests/unit/src/com/android/tv/recommendation/ChannelRecordTest.java index 44026386..bf8fecde 100644 --- a/tests/unit/src/com/android/tv/recommendation/ChannelRecordTest.java +++ b/tests/unit/src/com/android/tv/recommendation/ChannelRecordTest.java @@ -19,6 +19,8 @@ package com.android.tv.recommendation; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; +import com.android.tv.testing.Utils; + import java.util.Random; import java.util.concurrent.TimeUnit; @@ -38,7 +40,7 @@ public class ChannelRecordTest extends AndroidTestCase { super.setUp(); mLatestWatchEndTimeMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1); mChannelRecord = new ChannelRecord(getContext(), null, false); - mRandom = RecommendationUtils.createTestRandom(); + mRandom = Utils.createTestRandom(); } public void testGetLastWatchEndTime_noHistory() { diff --git a/tests/unit/src/com/android/tv/recommendation/EvaluatorTestCase.java b/tests/unit/src/com/android/tv/recommendation/EvaluatorTestCase.java index febeeda1..0255947b 100644 --- a/tests/unit/src/com/android/tv/recommendation/EvaluatorTestCase.java +++ b/tests/unit/src/com/android/tv/recommendation/EvaluatorTestCase.java @@ -17,11 +17,11 @@ package com.android.tv.recommendation; import android.test.AndroidTestCase; -import android.test.suitebuilder.annotation.SmallTest; import com.android.tv.data.Channel; import com.android.tv.recommendation.RecommendationUtils.ChannelRecordSortedMapHelper; import com.android.tv.recommendation.Recommender.Evaluator; +import com.android.tv.testing.Utils; import java.util.ArrayList; import java.util.List; @@ -47,7 +47,7 @@ public abstract class EvaluatorTestCase<T extends Evaluator> extends AndroidTest mEvaluator = createEvaluator(); mEvaluator.setRecommender(mRecommender); mChannelRecordSortedMap.setRecommender(mRecommender); - mChannelRecordSortedMap.resetRandom(RecommendationUtils.createTestRandom()); + mChannelRecordSortedMap.resetRandom(Utils.createTestRandom()); } /** diff --git a/tests/unit/src/com/android/tv/recommendation/RecommendationUtils.java b/tests/unit/src/com/android/tv/recommendation/RecommendationUtils.java index 8e57a267..4a613995 100644 --- a/tests/unit/src/com/android/tv/recommendation/RecommendationUtils.java +++ b/tests/unit/src/com/android/tv/recommendation/RecommendationUtils.java @@ -17,21 +17,18 @@ package com.android.tv.recommendation; import android.content.Context; -import android.util.Log; import com.android.tv.data.Channel; +import com.android.tv.testing.Utils; import org.mockito.Matchers; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; -import java.util.Date; import java.util.List; -import java.util.Locale; import java.util.Random; import java.util.TreeMap; import java.util.concurrent.TimeUnit; @@ -39,23 +36,6 @@ import java.util.concurrent.TimeUnit; public class RecommendationUtils { private static final String TAG = "RecommendationUtils"; private static final long INVALID_CHANNEL_ID = -1; - private static final long DEFAULT_RANDOM_SEED = getSeed(); - - private static long getSeed() { - // Set random seed as the date to track failed test data easily. - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.getDefault()); - String today = dateFormat.format(new Date()); - Log.d(TAG, "Today's random seed is " + today); - return Long.valueOf(today); - } - - /** - * Return the Random class which is needed to make random data for testing. - * Default seed of the random is today's date. - */ - public static Random createTestRandom() { - return new Random(DEFAULT_RANDOM_SEED); - } /** * Create a mock RecommendationDataManager backed by a {@link ChannelRecordSortedMapHelper}. @@ -88,7 +68,7 @@ public class RecommendationUtils { public static class ChannelRecordSortedMapHelper extends TreeMap<Long, ChannelRecord> { private final Context mContext; private Recommender mRecommender; - private Random mRandom = createTestRandom(); + private Random mRandom = Utils.createTestRandom(); public ChannelRecordSortedMapHelper(Context context) { mContext = context; diff --git a/tests/unit/src/com/android/tv/recommendation/RecommenderTest.java b/tests/unit/src/com/android/tv/recommendation/RecommenderTest.java index 62c5b4ab..0ca67052 100644 --- a/tests/unit/src/com/android/tv/recommendation/RecommenderTest.java +++ b/tests/unit/src/com/android/tv/recommendation/RecommenderTest.java @@ -22,6 +22,7 @@ import android.test.suitebuilder.annotation.SmallTest; import com.android.tv.data.Channel; import com.android.tv.recommendation.RecommendationUtils.ChannelRecordSortedMapHelper; +import com.android.tv.testing.Utils; import java.util.ArrayList; import java.util.Arrays; @@ -78,7 +79,7 @@ public class RecommenderTest extends AndroidTestCase { mChannelRecordSortedMap = new ChannelRecordSortedMapHelper(getContext()); mDataManager = RecommendationUtils .createMockRecommendationDataManager(mChannelRecordSortedMap); - mChannelRecordSortedMap.resetRandom(RecommendationUtils.createTestRandom()); + mChannelRecordSortedMap.resetRandom(Utils.createTestRandom()); } public void testRecommendChannels_includeRecommendedOnly_allChannelsHaveNoScore() { diff --git a/tests/unit/src/com/android/tv/util/ImageCacheTest.java b/tests/unit/src/com/android/tv/util/ImageCacheTest.java index 396372b2..74d5ef72 100644 --- a/tests/unit/src/com/android/tv/util/ImageCacheTest.java +++ b/tests/unit/src/com/android/tv/util/ImageCacheTest.java @@ -73,4 +73,4 @@ public class ImageCacheTest extends TestCase { mImageCache.putIfNeeded( INFO_200); assertSame("after", INFO_100, mImageCache.get(KEY)); } -} +}
\ No newline at end of file diff --git a/tests/unit/src/com/android/tv/util/MultiLongSparseArrayTest.java b/tests/unit/src/com/android/tv/util/MultiLongSparseArrayTest.java new file mode 100644 index 00000000..d44ffdef --- /dev/null +++ b/tests/unit/src/com/android/tv/util/MultiLongSparseArrayTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.tv.util; + +import android.test.MoreAsserts; +import android.test.suitebuilder.annotation.SmallTest; + +import junit.framework.TestCase; + +import java.util.Collections; + +/** + * Tests for {@link MultiLongSparseArray}. + */ +@SmallTest +public class MultiLongSparseArrayTest extends TestCase { + + public void testEmpty() { + MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); + assertSame(Collections.EMPTY_SET, sparseArray.get(0)); + } + + public void testOneElement() { + MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); + sparseArray.put(0, "foo"); + MoreAsserts.assertContentsInAnyOrder(sparseArray.get(0), "foo"); + } + + public void testTwoElements() { + MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); + sparseArray.put(0, "foo"); + sparseArray.put(0, "bar"); + MoreAsserts.assertContentsInAnyOrder(sparseArray.get(0), "foo", "bar"); + } + + + public void testClearEmptyCache() { + MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); + sparseArray.clearEmptyCache(); + assertEquals(0, sparseArray.getEmptyCacheSize()); + sparseArray.put(0, "foo"); + sparseArray.remove(0, "foo"); + assertEquals(1, sparseArray.getEmptyCacheSize()); + sparseArray.clearEmptyCache(); + assertEquals(0, sparseArray.getEmptyCacheSize()); + } + + public void testMaxEmptyCacheSize() { + MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); + sparseArray.clearEmptyCache(); + assertEquals(0, sparseArray.getEmptyCacheSize()); + for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { + sparseArray.put(i, "foo"); + } + for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { + sparseArray.remove(i, "foo"); + } + assertEquals(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT, + sparseArray.getEmptyCacheSize()); + sparseArray.clearEmptyCache(); + assertEquals(0, sparseArray.getEmptyCacheSize()); + } + + public void testReuseEmptySets() { + MultiLongSparseArray<String> sparseArray = new MultiLongSparseArray<>(); + sparseArray.clearEmptyCache(); + assertEquals(0, sparseArray.getEmptyCacheSize()); + // create a bunch of sets + for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { + sparseArray.put(i, "foo"); + } + // remove them so they are all put in the cache. + for (int i = 0; i <= MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT + 2; i++) { + sparseArray.remove(i, "foo"); + } + assertEquals(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT, + sparseArray.getEmptyCacheSize()); + + // now create elements, that use the cached empty sets. + for (int i = 0; i < MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT; i++) { + sparseArray.put(10 + i, "bar"); + assertEquals(MultiLongSparseArray.DEFAULT_MAX_EMPTIES_KEPT - i - 1, + sparseArray.getEmptyCacheSize()); + } + } +} diff --git a/tests/unit/src/com/android/tv/util/UtilsTest_GetMultiAudioString.java b/tests/unit/src/com/android/tv/util/UtilsTest_GetMultiAudioString.java new file mode 100644 index 00000000..601ed661 --- /dev/null +++ b/tests/unit/src/com/android/tv/util/UtilsTest_GetMultiAudioString.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.tv.util; + +import android.content.Context; +import android.media.tv.TvTrackInfo; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +/** + * Tests for {@link com.android.tv.util.Utils#getMultiAudioString}. + */ +@SmallTest +public class UtilsTest_GetMultiAudioString extends AndroidTestCase { + private static final String TRACK_ID = "test_track_id"; + private static final int AUDIO_SAMPLE_RATE = 48000; + + public void testAudioTrackLanguage() { + Context context = getContext(); + assertEquals("Korean", + Utils.getMultiAudioString(context, createAudioTrackInfo("kor"), false)); + assertEquals("English", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng"), false)); + assertEquals("Unknown language", + Utils.getMultiAudioString(context, createAudioTrackInfo(null), false)); + // TODO: Check whether the following tests are expected. + assertEquals("", Utils.getMultiAudioString(context, createAudioTrackInfo(""), false)); + assertEquals("abc", Utils.getMultiAudioString(context, createAudioTrackInfo("abc"), false)); + } + + public void testAudioTrackCount() { + Context context = getContext(); + assertEquals("English", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", -1), false)); + assertEquals("English", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 0), false)); + assertEquals("English (mono)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 1), false)); + assertEquals("English (stereo)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 2), false)); + assertEquals("English (3 channels)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 3), false)); + assertEquals("English (4 channels)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 4), false)); + assertEquals("English (5 channels)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 5), false)); + assertEquals("English (5.1 surround)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 6), false)); + assertEquals("English (7 channels)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 7), false)); + assertEquals("English (7.1 surround)", + Utils.getMultiAudioString(context, createAudioTrackInfo("eng", 8), false)); + } + + public void testShowSampleRate() { + assertEquals("Korean (48kHz)", + Utils.getMultiAudioString(getContext(), createAudioTrackInfo("kor", 0), true)); + assertEquals("Korean (7.1 surround, 48kHz)", + Utils.getMultiAudioString(getContext(), createAudioTrackInfo("kor", 8), true)); + } + + private static TvTrackInfo createAudioTrackInfo(String language) { + return createAudioTrackInfo(language, 0); + } + + private static TvTrackInfo createAudioTrackInfo(String language, int channelCount) { + return new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO, TRACK_ID) + .setLanguage(language).setAudioChannelCount(channelCount) + .setAudioSampleRate(AUDIO_SAMPLE_RATE).build(); + } +} |