• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.testing;
18 
19 import com.android.tv.data.ChannelImpl;
20 import com.android.tv.data.Lineup;
21 import com.android.tv.data.Program;
22 import com.android.tv.data.api.Channel;
23 import com.google.common.base.Function;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableListMultimap;
26 import com.google.common.collect.Iterables;
27 import com.google.common.collect.ListMultimap;
28 import java.util.concurrent.TimeUnit;
29 
30 /** EPG data for use in tests. */
31 public abstract class EpgTestData {
32 
33     public static final android.support.media.tv.Channel CHANNEL_10 =
34             new android.support.media.tv.Channel.Builder()
35                     .setDisplayName("Channel TEN")
36                     .setDisplayNumber("10")
37                     .build();
38     public static final android.support.media.tv.Channel CHANNEL_11 =
39             new android.support.media.tv.Channel.Builder()
40                     .setDisplayName("Channel Eleven")
41                     .setDisplayNumber("11")
42                     .build();
43     public static final android.support.media.tv.Channel CHANNEL_90_2 =
44             new android.support.media.tv.Channel.Builder()
45                     .setDisplayName("Channel Ninety dot Two")
46                     .setDisplayNumber("90.2")
47                     .build();
48 
49     public static final Lineup LINEUP_1 =
50             new Lineup(
51                     "lineup1",
52                     Lineup.LINEUP_SATELLITE,
53                     "Lineup one",
54                     "Location one",
55                     ImmutableList.of("1", "2.2"));
56     public static final Lineup LINEUP_2 =
57             new Lineup(
58                     "lineup2",
59                     Lineup.LINEUP_SATELLITE,
60                     "Lineup two",
61                     "Location two",
62                     ImmutableList.of("1", "2.3"));
63 
64     public static final Lineup LINEUP_90210 =
65             new Lineup(
66                     "test90210",
67                     Lineup.LINEUP_BROADCAST_DIGITAL,
68                     "Test 90210",
69                     "Beverly Hills",
70                     ImmutableList.of("90.2", "10"));
71 
72     // Programs start and end times are set relative to 0.
73     // Then when loaded they are offset by the {@link #getStartTimeMs}.
74     // Start and end time may be negative meaning they happen before "now".
75 
76     public static final Program PROGRAM_1 =
77             new Program.Builder()
78                     .setTitle("Program 1")
79                     .setStartTimeUtcMillis(0)
80                     .setEndTimeUtcMillis(TimeUnit.MINUTES.toMillis(30))
81                     .build();
82 
83     public static final Program PROGRAM_2 =
84             new Program.Builder()
85                     .setTitle("Program 2")
86                     .setStartTimeUtcMillis(TimeUnit.MINUTES.toMillis(30))
87                     .setEndTimeUtcMillis(TimeUnit.MINUTES.toMillis(60))
88                     .build();
89 
90     public static final EpgTestData DATA_90210 =
91             new EpgTestData() {
92 
93                 //  Thursday, June 1, 2017 4:00:00 PM GMT-07:00
94                 private final long testStartTimeMs = 1496358000000L;
95 
96                 @Override
97                 public ListMultimap<String, Lineup> getLineups() {
98                     ImmutableListMultimap.Builder<String, Lineup> builder =
99                             ImmutableListMultimap.builder();
100                     return builder.putAll("90210", LINEUP_1, LINEUP_2, LINEUP_90210).build();
101                 }
102 
103                 @Override
104                 public ListMultimap<String, Channel> getLineupChannels() {
105                     ImmutableListMultimap.Builder<String, Channel> builder =
106                             ImmutableListMultimap.builder();
107                     return builder.putAll(
108                                     LINEUP_90210.getId(), toTvChannels(CHANNEL_90_2, CHANNEL_10))
109                             .putAll(LINEUP_1.getId(), toTvChannels(CHANNEL_10, CHANNEL_11))
110                             .build();
111                 }
112 
113                 @Override
114                 public ListMultimap<String, Program> getEpgPrograms() {
115                     ImmutableListMultimap.Builder<String, Program> builder =
116                             ImmutableListMultimap.builder();
117                     return builder.putAll(
118                                     CHANNEL_10.getDisplayNumber(),
119                                     EpgTestData.updateTime(getStartTimeMs(), PROGRAM_1))
120                             .putAll(
121                                     CHANNEL_11.getDisplayNumber(),
122                                     EpgTestData.updateTime(getStartTimeMs(), PROGRAM_2))
123                             .build();
124                 }
125 
126                 @Override
127                 public long getStartTimeMs() {
128                     return testStartTimeMs;
129                 }
130             };
131 
getLineups()132     public abstract ListMultimap<String, Lineup> getLineups();
133 
getLineupChannels()134     public abstract ListMultimap<String, Channel> getLineupChannels();
135 
getEpgPrograms()136     public abstract ListMultimap<String, Program> getEpgPrograms();
137 
138     /** The starting time for this test data */
getStartTimeMs()139     public abstract long getStartTimeMs();
140 
141     /**
142      * Loads test data
143      *
144      * <p>
145      *
146      * <ul>
147      *   <li>Sets clock to {@link #getStartTimeMs()} and boot time to 12 hours before that
148      *   <li>Loads lineups
149      *   <li>Loads lineupChannels
150      *   <li>Loads epgPrograms
151      * </ul>
152      */
loadData(FakeClock clock, FakeEpgReader epgReader)153     public final void loadData(FakeClock clock, FakeEpgReader epgReader) {
154         clock.setBootTimeMillis(getStartTimeMs() + TimeUnit.HOURS.toMillis(-12));
155         clock.setCurrentTimeMillis(getStartTimeMs());
156         epgReader.zip2lineups.putAll(getLineups());
157         epgReader.lineup2Channels.putAll(getLineupChannels());
158         epgReader.epgChannelId2Programs.putAll(getEpgPrograms());
159     }
160 
loadData(TestSingletonApp testSingletonApp)161     public final void loadData(TestSingletonApp testSingletonApp) {
162         loadData(testSingletonApp.fakeClock, testSingletonApp.epgReader);
163     }
164 
toTvChannels(android.support.media.tv.Channel... channels)165     private static Iterable<Channel> toTvChannels(android.support.media.tv.Channel... channels) {
166         return Iterables.transform(
167                 ImmutableList.copyOf(channels),
168                 new Function<android.support.media.tv.Channel, Channel>() {
169                     @Override
170                     public Channel apply(android.support.media.tv.Channel original) {
171                         return toTvChannel(original);
172                     }
173                 });
174     }
175 
176     public static Channel toTvChannel(android.support.media.tv.Channel original) {
177         return new ChannelImpl.Builder()
178                 .setDisplayName(original.getDisplayName())
179                 .setDisplayNumber(original.getDisplayNumber())
180                 // TODO implement the reset
181                 .build();
182     }
183 
184     /** Add time to the startTime and stopTime of each program */
185     private static Iterable<Program> updateTime(long time, Program... programs) {
186         return Iterables.transform(
187                 ImmutableList.copyOf(programs),
188                 new Function<Program, Program>() {
189                     @Override
190                     public Program apply(Program p) {
191                         return new Program.Builder(p)
192                                 .setStartTimeUtcMillis(p.getStartTimeUtcMillis() + time)
193                                 .setEndTimeUtcMillis(p.getEndTimeUtcMillis() + time)
194                                 .build();
195                     }
196                 });
197     }
198 }
199