aboutsummaryrefslogtreecommitdiff
path: root/tests/common/src/com/android/tv/testing/FakeEpgReader.java
blob: 710ada554c62e0b823c1e78dc08e8a3c715451a8 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2017 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.testing;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Range;
import com.android.tv.data.ChannelImpl;
import com.android.tv.data.ChannelNumber;
import com.android.tv.data.Lineup;
import com.android.tv.data.Program;
import com.android.tv.data.api.Channel;
import com.android.tv.data.epg.EpgReader;
import com.android.tv.dvr.data.SeriesInfo;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/** Fake {@link EpgReader} for testing. */
public final class FakeEpgReader implements EpgReader {
    public final ListMultimap<String, Lineup> zip2lineups = LinkedListMultimap.create(2);
    public final ListMultimap<String, Channel> lineup2Channels = LinkedListMultimap.create(2);
    public final ListMultimap<String, Program> epgChannelId2Programs = LinkedListMultimap.create(2);
    public final FakeClock fakeClock;

    public FakeEpgReader(FakeClock fakeClock) {
        this.fakeClock = fakeClock;
    }

    @Override
    public boolean isAvailable() {
        return true;
    }

    @Override
    public long getEpgTimestamp() {
        return fakeClock.currentTimeMillis();
    }

    @Override
    public void setRegionCode(String regionCode) {}

    @Override
    public List<Lineup> getLineups(@NonNull String postalCode) {
        return zip2lineups.get(postalCode);
    }

    @Override
    public List<String> getChannelNumbers(@NonNull String lineupId) {
        return null;
    }

    @Override
    public Set<EpgChannel> getChannels(Set<Channel> inputChannels, @NonNull String lineupId) {
        Set<EpgChannel> result = new HashSet<>();
        List<Channel> lineupChannels = lineup2Channels.get(lineupId);
        for (Channel channel : lineupChannels) {
            Channel match =
                    Iterables.find(
                            inputChannels,
                            new Predicate<Channel>() {
                                @Override
                                public boolean apply(@Nullable Channel inputChannel) {
                                    return ChannelNumber.equivalent(
                                            inputChannel.getDisplayNumber(),
                                            channel.getDisplayNumber());
                                }
                            },
                            null);
            if (match != null) {
                ChannelImpl updatedChannel = new ChannelImpl.Builder(match).build();
                updatedChannel.setLogoUri(channel.getLogoUri());
                result.add(EpgChannel.createEpgChannel(updatedChannel, channel.getDisplayNumber()));
            }
        }
        return result;
    }

    @Override
    public void preloadChannels(@NonNull String lineupId) {}

    @Override
    public void clearCachedChannels(@NonNull String lineupId) {}

    @Override
    public List<Program> getPrograms(EpgChannel epgChannel) {
        return ImmutableList.copyOf(
                Iterables.transform(
                        epgChannelId2Programs.get(epgChannel.getEpgChannelId()),
                        updateWith(epgChannel)));
    }

    @Override
    public Map<EpgChannel, Collection<Program>> getPrograms(
            @NonNull Set<EpgChannel> epgChannels, long duration) {
        Range<Long> validRange =
                Range.create(
                        fakeClock.currentTimeMillis(), fakeClock.currentTimeMillis() + duration);
        ImmutableMap.Builder<EpgChannel, Collection<Program>> mapBuilder = ImmutableMap.builder();
        for (EpgChannel epgChannel : epgChannels) {
            Iterable<Program> programs = getPrograms(epgChannel);

            mapBuilder.put(
                    epgChannel,
                    ImmutableList.copyOf(Iterables.filter(programs, isProgramDuring(validRange))));
        }
        return mapBuilder.build();
    }

    protected Function<Program, Program> updateWith(final EpgChannel channel) {
        return new Function<Program, Program>() {
            @Nullable
            @Override
            public Program apply(@Nullable Program program) {
                return new Program.Builder(program)
                        .setChannelId(channel.getChannel().getId())
                        .setPackageName(channel.getChannel().getPackageName())
                        .build();
            }
        };
    }

    /**
     * True if the start time or the end time is {@link Range#contains contained (inclusive)} in the
     * range
     */
    protected Predicate<Program> isProgramDuring(final Range<Long> validRange) {
        return new Predicate<Program>() {
            @Override
            public boolean apply(@Nullable Program program) {
                return validRange.contains(program.getStartTimeUtcMillis())
                        || validRange.contains(program.getEndTimeUtcMillis());
            }
        };
    }

    @Override
    public SeriesInfo getSeriesInfo(@NonNull String seriesId) {
        return null;
    }
}