aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android/tv/recommendation/ChannelRecordTest.java
blob: c76de8fb825e192cd8a49af95fd3fa61c8047575 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * 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.recommendation;

import android.support.test.filters.SmallTest;
import android.test.AndroidTestCase;

import com.android.tv.testing.Utils;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Unit tests for {@link ChannelRecord}.
 */
@SmallTest
public class ChannelRecordTest extends AndroidTestCase {
    private static final int CHANNEL_RECORD_MAX_HISTORY_SIZE = ChannelRecord.MAX_HISTORY_SIZE;

    private Random mRandom;
    private ChannelRecord mChannelRecord;
    private long mLatestWatchEndTimeMs;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mLatestWatchEndTimeMs = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1);
        mChannelRecord = new ChannelRecord(getContext(), null, false);
        mRandom = Utils.createTestRandom();
    }

    public void testGetLastWatchEndTime_noHistory() {
        assertEquals(0, mChannelRecord.getLastWatchEndTimeMs());
    }

    public void testGetLastWatchEndTime_oneHistory() {
        addWatchLog();

        assertEquals(mLatestWatchEndTimeMs, mChannelRecord.getLastWatchEndTimeMs());
    }

    public void testGetLastWatchEndTime_maxHistories() {
        for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE; ++i) {
            addWatchLog();
        }

        assertEquals(mLatestWatchEndTimeMs, mChannelRecord.getLastWatchEndTimeMs());
    }

    public void testGetLastWatchEndTime_moreThanMaxHistories() {
        for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE + 1; ++i) {
            addWatchLog();
        }

        assertEquals(mLatestWatchEndTimeMs, mChannelRecord.getLastWatchEndTimeMs());
    }

    public void testGetTotalWatchDuration_noHistory() {
        assertEquals(0, mChannelRecord.getTotalWatchDurationMs());
    }

    public void testGetTotalWatchDuration_oneHistory() {
        long durationMs = addWatchLog();

        assertEquals(durationMs, mChannelRecord.getTotalWatchDurationMs());
    }

    public void testGetTotalWatchDuration_maxHistories() {
        long totalWatchTimeMs = 0;
        for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE; ++i) {
            long durationMs = addWatchLog();
            totalWatchTimeMs += durationMs;
        }

        assertEquals(totalWatchTimeMs, mChannelRecord.getTotalWatchDurationMs());
    }

    public void testGetTotalWatchDuration_moreThanMaxHistories() {
        long totalWatchTimeMs = 0;
        long firstDurationMs = 0;
        for (int i = 0; i < CHANNEL_RECORD_MAX_HISTORY_SIZE + 1; ++i) {
            long durationMs = addWatchLog();
            totalWatchTimeMs += durationMs;
            if (i == 0) {
                firstDurationMs = durationMs;
            }
        }

        // Only latest CHANNEL_RECORD_MAX_HISTORY_SIZE logs are remained.
        assertEquals(totalWatchTimeMs - firstDurationMs, mChannelRecord.getTotalWatchDurationMs());
    }

    /**
     * Add new log history to channelRecord which its duration is lower than 1 minute.
     *
     * @return New watch log's duration time in milliseconds.
     */
    private long addWatchLog() {
        // Time hopping with random seconds.
        mLatestWatchEndTimeMs += TimeUnit.SECONDS.toMillis(mRandom.nextInt(60) + 1);

        long durationMs = TimeUnit.SECONDS.toMillis(mRandom.nextInt(60) + 1);
        mChannelRecord.logWatchHistory(new WatchedProgram(null,
                mLatestWatchEndTimeMs, mLatestWatchEndTimeMs + durationMs));
        mLatestWatchEndTimeMs += durationMs;

        return durationMs;
    }
}