1 /*
2  * Copyright (C) 2019 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 package com.android.tv.guide;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.ArgumentMatchers.anyLong;
21 
22 import com.android.tv.common.flags.impl.DefaultUiFlags;
23 import com.android.tv.data.ChannelDataManager;
24 import com.android.tv.data.ChannelImpl;
25 import com.android.tv.data.GenreItems;
26 import com.android.tv.data.ProgramDataManager;
27 import com.android.tv.data.ProgramImpl;
28 import com.android.tv.data.api.Channel;
29 import com.android.tv.data.api.Program;
30 import com.android.tv.testing.TestSingletonApp;
31 import com.android.tv.testing.TvRobolectricTestRunner;
32 import com.android.tv.testing.constants.ConfigConstants;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RuntimeEnvironment;
41 import org.robolectric.annotation.Config;
42 
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.concurrent.TimeUnit;
46 
47 /** Tests for {@link ProgramTableAdapter}. */
48 @RunWith(TvRobolectricTestRunner.class)
49 @Config(sdk = ConfigConstants.SDK, application = TestSingletonApp.class)
50 public class ProgramTableAdapterTest {
51 
52     @Mock private ProgramGuide mProgramGuide;
53     @Mock private ChannelDataManager mChannelDataManager;
54     @Mock private ProgramDataManager mProgramDataManager;
55     private ProgramManager mProgramManager;
56 
57     //  Thursday, June 1, 2017 1:00:00 PM GMT-07:00
58     private final long mTestStartTimeMs = 1496347200000L;
59     // Thursday, June 1, 2017 8:00:00 PM GMT-07:00
60     private final long mEightPM = 1496372400000L;
61     private DefaultUiFlags mUiFlags;
62 
63     @Before
setup()64     public void setup() {
65         MockitoAnnotations.initMocks(this);
66         TestSingletonApp app = (TestSingletonApp) RuntimeEnvironment.application;
67         app.fakeClock.setBootTimeMillis(mTestStartTimeMs + TimeUnit.HOURS.toMillis(-12));
68         app.fakeClock.setCurrentTimeMillis(mTestStartTimeMs);
69         mUiFlags = new DefaultUiFlags();
70         mProgramManager =
71                 new ProgramManager(
72                         app.getTvInputManagerHelper(),
73                         mChannelDataManager,
74                         mProgramDataManager,
75                         null,
76                         null);
77     }
78 
79     @Test
testOnTableEntryChanged()80     public void testOnTableEntryChanged() {
81         Mockito.when(mProgramGuide.getProgramManager()).thenReturn(mProgramManager);
82         Mockito.when(mProgramDataManager.getCurrentProgram(anyLong()))
83                 .thenAnswer(
84                         invocation -> {
85                             long id = (long) invocation.getArguments()[0];
86                             return buildProgramForTesting(
87                                     id, id, (int) id % GenreItems.getGenreCount());
88                         });
89         ProgramTableAdapter programTableAdapter =
90                 new ProgramTableAdapter(RuntimeEnvironment.application, mProgramGuide, mUiFlags);
91         mProgramManager.setChannels(buildChannelForTesting(1, 2, 3));
92         assertThat(mProgramManager.getChannelCount()).isEqualTo(3);
93 
94         // set genre ID to 1. Then channel 1 is in the filtered list but channel 2 is not.
95         mProgramManager.resetChannelListWithGenre(1);
96         assertThat(mProgramManager.getChannelCount()).isEqualTo(1);
97         assertThat(mProgramManager.getChannelIndex(2)).isEqualTo(-1);
98 
99         // should be no exception when onTableEntryChanged() is called
100         programTableAdapter.onTableEntryChanged(
101                 ProgramManager.createTableEntryForTest(
102                         2,
103                         mProgramDataManager.getCurrentProgram(2),
104                         null,
105                         mTestStartTimeMs,
106                         mEightPM,
107                         false));
108     }
109 
buildChannelForTesting(long... ids)110     private List<Channel> buildChannelForTesting(long... ids) {
111         List<Channel> channels = new ArrayList<>();
112         for (long id : ids) {
113             channels.add(new ChannelImpl.Builder().setId(id).build());
114         }
115         return channels;
116     }
117 
buildProgramForTesting(long id, long channelId, int genreId)118     private Program buildProgramForTesting(long id, long channelId, int genreId) {
119         return new ProgramImpl.Builder()
120                 .setId(id)
121                 .setChannelId(channelId)
122                 .setCanonicalGenres(GenreItems.getCanonicalGenre(genreId))
123                 .build();
124     }
125 }
126