Home | History | Annotate | Download | only in recommendation
      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 
     17 package com.android.tv.recommendation;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 import com.android.tv.testing.Utils;
     23 
     24 import java.util.Random;
     25 import java.util.concurrent.TimeUnit;
     26 
     27 /**
     28  * Unit tests for {@link ChannelRecord}.
     29  */
     30 @SmallTest
     31 public class ChannelRecordTest extends AndroidTestCase {
     32     private static final int CHANNEL_RECORD_MAX_HISTORY_SIZE = ChannelRecord.MAX_HISTORY_SIZE;
     33 
     34     private Random mRandom;
     35     private ChannelRecord mChannelRecord;
     36     private long mLatestWatchEndTimeMs;
     37 
     38     @Override
     39     public void setUp() throws Exception {
     40         super.setUp();
     41         mLatestWatchEndTimeMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
     42         mChannelRecord = new ChannelRecord(getContext(), null, false);
     43         mRandom = Utils.createTestRandom();
     44     }
     45 
     46     public void testGetLastWatchEndTime_noHistory() {
     47         assertEquals(0, mChannelRecord.getLastWatchEndTimeMs());
     48     }
     49 
     50     public void testGetLastWatchEndTime_oneHistory() {
     51         addWatchLog();
     52 
     53         assertEquals(mLatestWatchEndTimeMs, mChannelRecord.getLastWatchEndTimeMs());
     54     }
     55 
     56     public void testGetLastWatchEndTime_maxHistories() {
     57         for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE; ++i) {
     58             addWatchLog();
     59         }
     60 
     61         assertEquals(mLatestWatchEndTimeMs, mChannelRecord.getLastWatchEndTimeMs());
     62     }
     63 
     64     public void testGetLastWatchEndTime_moreThanMaxHistories() {
     65         for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE + 1; ++i) {
     66             addWatchLog();
     67         }
     68 
     69         assertEquals(mLatestWatchEndTimeMs, mChannelRecord.getLastWatchEndTimeMs());
     70     }
     71 
     72     public void testGetTotalWatchDuration_noHistory() {
     73         assertEquals(0, mChannelRecord.getTotalWatchDurationMs());
     74     }
     75 
     76     public void testGetTotalWatchDuration_oneHistory() {
     77         long durationMs = addWatchLog();
     78 
     79         assertEquals(durationMs, mChannelRecord.getTotalWatchDurationMs());
     80     }
     81 
     82     public void testGetTotalWatchDuration_maxHistories() {
     83         long totalWatchTimeMs = 0;
     84         for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE; ++i) {
     85             long durationMs = addWatchLog();
     86             totalWatchTimeMs += durationMs;
     87         }
     88 
     89         assertEquals(totalWatchTimeMs, mChannelRecord.getTotalWatchDurationMs());
     90     }
     91 
     92     public void testGetTotalWatchDuration_moreThanMaxHistories() {
     93         long totalWatchTimeMs = 0;
     94         long firstDurationMs = 0;
     95         for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE + 1; ++i) {
     96             long durationMs = addWatchLog();
     97             totalWatchTimeMs += durationMs;
     98             if (i == 0) {
     99                 firstDurationMs = durationMs;
    100             }
    101         }
    102 
    103         // Only latest CHANNEL_RECORD_MAX_HISTORY_SIZE logs are remained.
    104         assertEquals(totalWatchTimeMs - firstDurationMs, mChannelRecord.getTotalWatchDurationMs());
    105     }
    106 
    107     /**
    108      * Add new log history to channelRecord which its duration is lower than 1 minute.
    109      *
    110      * @return New watch log's duration time in milliseconds.
    111      */
    112     private long addWatchLog() {
    113         // Time hopping with random seconds.
    114         mLatestWatchEndTimeMs += TimeUnit.SECONDS.toMillis(mRandom.nextInt(60) + 1);
    115 
    116         long durationMs = TimeUnit.SECONDS.toMillis(mRandom.nextInt(60) + 1);
    117         mChannelRecord.logWatchHistory(new WatchedProgram(null,
    118                 mLatestWatchEndTimeMs, mLatestWatchEndTimeMs + durationMs));
    119         mLatestWatchEndTimeMs += durationMs;
    120 
    121         return durationMs;
    122     }
    123 }
    124