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 17package com.android.tv.data; 18 19import android.test.AndroidTestCase; 20import android.test.UiThreadTest; 21import android.test.suitebuilder.annotation.SmallTest; 22import android.test.suitebuilder.annotation.Suppress; 23 24import com.android.tv.data.WatchedHistoryManager.WatchedRecord; 25import com.android.tv.testing.Utils; 26 27import java.util.concurrent.CountDownLatch; 28import java.util.concurrent.TimeUnit; 29 30/** 31 * Test for {@link com.android.tv.data.WatchedHistoryManagerTest} 32 */ 33@SmallTest 34@Suppress // http://b/27156462 35public class WatchedHistoryManagerTest extends AndroidTestCase { 36 private static final boolean DEBUG = false; 37 private static final String TAG = "WatchedHistoryManager"; 38 39 // Wait time for expected success. 40 private static final long WAIT_TIME_OUT_MS = 1000L; 41 private static final int MAX_HISTORY_SIZE = 100; 42 43 private WatchedHistoryManager mWatchedHistoryManager; 44 private TestWatchedHistoryManagerListener mListener; 45 46 @Override 47 protected void setUp() throws Exception { 48 super.setUp(); 49 Utils.runOnMainSync(new Runnable() { 50 @Override 51 public void run() { 52 mWatchedHistoryManager = new WatchedHistoryManager(getContext(), MAX_HISTORY_SIZE); 53 mListener = new TestWatchedHistoryManagerListener(); 54 mWatchedHistoryManager.setListener(mListener); 55 } 56 }); 57 } 58 59 private void startAndWaitForComplete() throws Exception { 60 mWatchedHistoryManager.start(); 61 try { 62 assertTrue(mListener.loadFinishedLatch.await(WAIT_TIME_OUT_MS, TimeUnit.MILLISECONDS)); 63 } catch (InterruptedException e) { 64 throw e; 65 } 66 } 67 68 @UiThreadTest 69 public void testIsLoaded() throws Exception { 70 assertFalse(mWatchedHistoryManager.isLoaded()); 71 startAndWaitForComplete(); 72 assertTrue(mWatchedHistoryManager.isLoaded()); 73 } 74 75 @UiThreadTest 76 public void testLogChannelViewStop() throws Exception { 77 startAndWaitForComplete(); 78 long fakeId = 100000000; 79 long time = System.currentTimeMillis(); 80 long duration = TimeUnit.MINUTES.toMillis(10); 81 Channel channel = new Channel.Builder().setId(fakeId).build(); 82 mWatchedHistoryManager.logChannelViewStop(channel, time, duration); 83 84 WatchedRecord record = mWatchedHistoryManager.getRecord(0); 85 WatchedRecord recordFromSharedPreferences = 86 mWatchedHistoryManager.getRecordFromSharedPreferences(0); 87 assertEquals(record.channelId, fakeId); 88 assertEquals(record.watchedStartTime, time - duration); 89 assertEquals(record.duration, duration); 90 assertEquals(record, recordFromSharedPreferences); 91 } 92 93 @UiThreadTest 94 public void testCircularHistoryQueue() throws Exception { 95 startAndWaitForComplete(); 96 final long startChannelId = 100000000; 97 long time = System.currentTimeMillis(); 98 long duration = TimeUnit.MINUTES.toMillis(10); 99 100 int size = MAX_HISTORY_SIZE * 2; 101 for (int i = 0; i < size; ++i) { 102 Channel channel = new Channel.Builder().setId(startChannelId + i).build(); 103 mWatchedHistoryManager.logChannelViewStop(channel, time + duration * i, duration); 104 } 105 for (int i = 0; i < MAX_HISTORY_SIZE; ++i) { 106 WatchedRecord record = mWatchedHistoryManager.getRecord(i); 107 WatchedRecord recordFromSharedPreferences = 108 mWatchedHistoryManager.getRecordFromSharedPreferences(i); 109 assertEquals(record, recordFromSharedPreferences); 110 assertEquals(record.channelId, startChannelId + size - 1 - i); 111 } 112 // Since the WatchedHistory is a circular queue, the value for 0 and maxHistorySize 113 // are same. 114 assertEquals(mWatchedHistoryManager.getRecordFromSharedPreferences(0), 115 mWatchedHistoryManager.getRecordFromSharedPreferences(MAX_HISTORY_SIZE)); 116 } 117 118 @UiThreadTest 119 public void testWatchedRecordEquals() { 120 assertTrue(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(1, 2, 3))); 121 assertFalse(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(1, 2, 4))); 122 assertFalse(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(1, 4, 3))); 123 assertFalse(new WatchedRecord(1, 2, 3).equals(new WatchedRecord(4, 2, 3))); 124 } 125 126 @UiThreadTest 127 public void testEncodeDecodeWatchedRecord() throws Exception { 128 long fakeId = 100000000; 129 long time = System.currentTimeMillis(); 130 long duration = TimeUnit.MINUTES.toMillis(10); 131 WatchedRecord record = new WatchedRecord(fakeId, time, duration); 132 WatchedRecord sameRecord = mWatchedHistoryManager.decode( 133 mWatchedHistoryManager.encode(record)); 134 assertEquals(record, sameRecord); 135 } 136 137 private class TestWatchedHistoryManagerListener implements WatchedHistoryManager.Listener { 138 public CountDownLatch loadFinishedLatch = new CountDownLatch(1); 139 140 @Override 141 public void onLoadFinished() { 142 loadFinishedLatch.countDown(); 143 } 144 145 @Override 146 public void onNewRecordAdded(WatchedRecord watchedRecord) { } 147 } 148} 149