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.support.test.filters.SmallTest;
     20 
     21 import java.util.HashMap;
     22 import java.util.List;
     23 import java.util.Map;
     24 import java.util.concurrent.TimeUnit;
     25 
     26 /**
     27  * Unit tests for {@link RecentChannelEvaluator}.
     28  */
     29 @SmallTest
     30 public class RecentChannelEvaluatorTest extends EvaluatorTestCase<RecentChannelEvaluator> {
     31     private static final int DEFAULT_NUMBER_OF_CHANNELS = 4;
     32     private static final long DEFAULT_WATCH_START_TIME_MS =
     33             System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2);
     34     private static final long DEFAULT_WATCH_END_TIME_MS =
     35             System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
     36     private static final long DEFAULT_MAX_WATCH_DURATION_MS = TimeUnit.HOURS.toMillis(1);
     37 
     38     @Override
     39     public RecentChannelEvaluator createEvaluator() {
     40         return new RecentChannelEvaluator();
     41     }
     42 
     43     public void testOneChannelWithNoWatchLog() {
     44         long channelId = addChannel().getId();
     45         notifyChannelAndWatchLogLoaded();
     46 
     47         assertEquals(Recommender.Evaluator.NOT_RECOMMENDED,
     48                 mEvaluator.evaluateChannel(channelId));
     49     }
     50 
     51     public void testOneChannelWithRandomWatchLogs() {
     52         addChannel();
     53         addRandomWatchLogs(DEFAULT_WATCH_START_TIME_MS, DEFAULT_WATCH_END_TIME_MS,
     54                 DEFAULT_MAX_WATCH_DURATION_MS);
     55         notifyChannelAndWatchLogLoaded();
     56 
     57         assertChannelScoresValid();
     58     }
     59 
     60     public void testMultiChannelsWithNoWatchLog() {
     61         addChannels(DEFAULT_NUMBER_OF_CHANNELS);
     62         notifyChannelAndWatchLogLoaded();
     63 
     64         List<Long> channelIdList = getChannelIdListSorted();
     65         for (long channelId : channelIdList) {
     66             assertEquals(Recommender.Evaluator.NOT_RECOMMENDED,
     67                     mEvaluator.evaluateChannel(channelId));
     68         }
     69     }
     70 
     71     public void testMultiChannelsWithRandomWatchLogs() {
     72         addChannels(DEFAULT_NUMBER_OF_CHANNELS);
     73         addRandomWatchLogs(DEFAULT_WATCH_START_TIME_MS, DEFAULT_WATCH_END_TIME_MS,
     74                 DEFAULT_MAX_WATCH_DURATION_MS);
     75         notifyChannelAndWatchLogLoaded();
     76 
     77         assertChannelScoresValid();
     78     }
     79 
     80     public void testMultiChannelsWithSimpleWatchLogs() {
     81         addChannels(DEFAULT_NUMBER_OF_CHANNELS);
     82         // Every channel has one watch log with 1 hour. Also, for two channels
     83         // which has ID x and y (x < y), the channel y is watched later than the channel x.
     84         long latestWatchEndTimeMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2);
     85         List<Long> channelIdList = getChannelIdListSorted();
     86         for (long channelId : channelIdList) {
     87             addWatchLog(channelId, latestWatchEndTimeMs, TimeUnit.HOURS.toMillis(1));
     88             latestWatchEndTimeMs += TimeUnit.HOURS.toMillis(1);
     89         }
     90         notifyChannelAndWatchLogLoaded();
     91 
     92         assertChannelScoresValid();
     93         // Channel score must be increased as channel ID increased.
     94         double previousScore = Recommender.Evaluator.NOT_RECOMMENDED;
     95         for (long channelId : channelIdList) {
     96             double score = mEvaluator.evaluateChannel(channelId);
     97             assertTrue(previousScore <= score);
     98             previousScore = score;
     99         }
    100     }
    101 
    102     public void testScoreIncreasesWithNewWatchLog() {
    103         addChannels(DEFAULT_NUMBER_OF_CHANNELS);
    104         addRandomWatchLogs(DEFAULT_WATCH_START_TIME_MS, DEFAULT_WATCH_END_TIME_MS,
    105                 DEFAULT_MAX_WATCH_DURATION_MS);
    106         notifyChannelAndWatchLogLoaded();
    107 
    108         List<Long> channelIdList = getChannelIdListSorted();
    109         long latestWatchEndTimeMs = getLatestWatchEndTimeMs();
    110         for (long channelId : channelIdList) {
    111             double previousScore = mEvaluator.evaluateChannel(channelId);
    112 
    113             long durationMs = TimeUnit.MINUTES.toMillis(10);
    114             addWatchLog(channelId, latestWatchEndTimeMs, durationMs);
    115             latestWatchEndTimeMs += durationMs;
    116 
    117             // Score must be increased because recentness of the log increases.
    118             assertTrue(previousScore <= mEvaluator.evaluateChannel(channelId));
    119         }
    120     }
    121 
    122     public void testScoreDecreasesWithIncrementOfWatchedLogUpdatedTime() {
    123         addChannels(DEFAULT_NUMBER_OF_CHANNELS);
    124         addRandomWatchLogs(DEFAULT_WATCH_START_TIME_MS, DEFAULT_WATCH_END_TIME_MS,
    125                 DEFAULT_MAX_WATCH_DURATION_MS);
    126         notifyChannelAndWatchLogLoaded();
    127 
    128         Map<Long, Double> scores = new HashMap<>();
    129         List<Long> channelIdList = getChannelIdListSorted();
    130         long latestWatchedEndTimeMs = getLatestWatchEndTimeMs();
    131 
    132         for (long channelId : channelIdList) {
    133             scores.put(channelId, mEvaluator.evaluateChannel(channelId));
    134         }
    135 
    136         long newChannelId = addChannel().getId();
    137         addWatchLog(newChannelId, latestWatchedEndTimeMs, TimeUnit.MINUTES.toMillis(10));
    138 
    139         for (long channelId : channelIdList) {
    140             // Score must be decreased because LastWatchLogUpdateTime increases by new log.
    141             assertTrue(mEvaluator.evaluateChannel(channelId) <= scores.get(channelId));
    142         }
    143     }
    144 }
    145