aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android/tv/data/ChannelTest.java
blob: 69fcb858c803edebe945f908a513868da744028e (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * 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.data;

import static org.junit.Assert.assertEquals;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.support.test.filters.SmallTest;

import com.android.tv.testing.ComparatorTester;
import com.android.tv.util.TvInputManagerHelper;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.Comparator;

/**
 * Tests for {@link Channel}.
 */
@SmallTest
public class ChannelTest {
    // Used for testing TV inputs with invalid input package. This could happen when a TV input is
    // uninstalled while drawing an app link card.
    private static final String INVALID_TV_INPUT_PACKAGE_NAME =
            "com.android.tv.invalid_tv_input";
    // Used for testing TV inputs defined inside of Live TV.
    private static final String LIVE_CHANNELS_PACKAGE_NAME = "com.android.tv";
    // Used for testing a TV input which doesn't have its leanback launcher activity.
    private static final String NONE_LEANBACK_TV_INPUT_PACKAGE_NAME =
            "com.android.tv.none_leanback_tv_input";
    // Used for testing a TV input which has its leanback launcher activity.
    private static final String LEANBACK_TV_INPUT_PACKAGE_NAME =
            "com.android.tv.leanback_tv_input";
    private static final String TEST_APP_LINK_TEXT = "test_app_link_text";
    private static final String PARTNER_INPUT_ID = "partner";
    private static final ActivityInfo TEST_ACTIVITY_INFO = new ActivityInfo();

    private Context mMockContext;
    private Intent mInvalidIntent;
    private Intent mValidIntent;

    @Before
    public void setUp() throws NameNotFoundException {
        mInvalidIntent = new Intent(Intent.ACTION_VIEW);
        mInvalidIntent.setComponent(new ComponentName(INVALID_TV_INPUT_PACKAGE_NAME, ".test"));
        mValidIntent = new Intent(Intent.ACTION_VIEW);
        mValidIntent.setComponent(new ComponentName(LEANBACK_TV_INPUT_PACKAGE_NAME, ".test"));
        Intent liveChannelsIntent = new Intent(Intent.ACTION_VIEW);
        liveChannelsIntent.setComponent(
                new ComponentName(LIVE_CHANNELS_PACKAGE_NAME, ".MainActivity"));
        Intent leanbackTvInputIntent = new Intent(Intent.ACTION_VIEW);
        leanbackTvInputIntent.setComponent(
                new ComponentName(LEANBACK_TV_INPUT_PACKAGE_NAME, ".test"));

        PackageManager mockPackageManager = Mockito.mock(PackageManager.class);
        Mockito.when(mockPackageManager.getLeanbackLaunchIntentForPackage(
                INVALID_TV_INPUT_PACKAGE_NAME)).thenReturn(null);
        Mockito.when(mockPackageManager.getLeanbackLaunchIntentForPackage(
                LIVE_CHANNELS_PACKAGE_NAME)).thenReturn(liveChannelsIntent);
        Mockito.when(mockPackageManager.getLeanbackLaunchIntentForPackage(
                NONE_LEANBACK_TV_INPUT_PACKAGE_NAME)).thenReturn(null);
        Mockito.when(mockPackageManager.getLeanbackLaunchIntentForPackage(
                LEANBACK_TV_INPUT_PACKAGE_NAME)).thenReturn(leanbackTvInputIntent);

        // Channel.getAppLinkIntent() calls initAppLinkTypeAndIntent() which calls
        // Intent.resolveActivityInfo() which calls PackageManager.getActivityInfo().
        Mockito.doAnswer(new Answer<ActivityInfo>() {
            @Override
            public ActivityInfo answer(InvocationOnMock invocation) {
                // We only check the package name, since the class name can be changed
                // when an intent is changed to an uri and created from the uri.
                // (ex, ".className" -> "packageName.className")
                return mValidIntent.getComponent().getPackageName().equals(
                        ((ComponentName)invocation.getArguments()[0]).getPackageName())
                        ? TEST_ACTIVITY_INFO : null;
            }
        }).when(mockPackageManager).getActivityInfo(Mockito.<ComponentName>any(), Mockito.anyInt());

        mMockContext = Mockito.mock(Context.class);
        Mockito.when(mMockContext.getApplicationContext()).thenReturn(mMockContext);
        Mockito.when(mMockContext.getPackageName()).thenReturn(LIVE_CHANNELS_PACKAGE_NAME);
        Mockito.when(mMockContext.getPackageManager()).thenReturn(mockPackageManager);
    }

    @Test
    public void testGetAppLinkType_NoText_NoIntent() {
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, INVALID_TV_INPUT_PACKAGE_NAME, null, null);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, LIVE_CHANNELS_PACKAGE_NAME, null, null);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, NONE_LEANBACK_TV_INPUT_PACKAGE_NAME, null,
                null);
        assertAppLinkType(Channel.APP_LINK_TYPE_APP, LEANBACK_TV_INPUT_PACKAGE_NAME, null, null);
    }

    @Test
    public void testGetAppLinkType_NoText_InvalidIntent() {
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, INVALID_TV_INPUT_PACKAGE_NAME, null,
                mInvalidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, LIVE_CHANNELS_PACKAGE_NAME, null,
                mInvalidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, NONE_LEANBACK_TV_INPUT_PACKAGE_NAME, null,
                mInvalidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_APP, LEANBACK_TV_INPUT_PACKAGE_NAME, null,
                mInvalidIntent);
    }

    @Test
    public void testGetAppLinkType_NoText_ValidIntent() {
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, INVALID_TV_INPUT_PACKAGE_NAME, null,
                mValidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, LIVE_CHANNELS_PACKAGE_NAME, null,
                mValidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, NONE_LEANBACK_TV_INPUT_PACKAGE_NAME, null,
                mValidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_APP, LEANBACK_TV_INPUT_PACKAGE_NAME, null,
                mValidIntent);
    }

    @Test
    public void testGetAppLinkType_HasText_NoIntent() {
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, INVALID_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, null);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, LIVE_CHANNELS_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, null);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, NONE_LEANBACK_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, null);
        assertAppLinkType(Channel.APP_LINK_TYPE_APP, LEANBACK_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, null);
    }

    @Test
    public void testGetAppLinkType_HasText_InvalidIntent() {
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, INVALID_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mInvalidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, LIVE_CHANNELS_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mInvalidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_NONE, NONE_LEANBACK_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mInvalidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_APP, LEANBACK_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mInvalidIntent);
    }

    @Test
    public void testGetAppLinkType_HasText_ValidIntent() {
        assertAppLinkType(Channel.APP_LINK_TYPE_CHANNEL, INVALID_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mValidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_CHANNEL, LIVE_CHANNELS_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mValidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_CHANNEL, NONE_LEANBACK_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mValidIntent);
        assertAppLinkType(Channel.APP_LINK_TYPE_CHANNEL, LEANBACK_TV_INPUT_PACKAGE_NAME,
                TEST_APP_LINK_TEXT, mValidIntent);
    }

    private void assertAppLinkType(int expectedType, String inputPackageName, String appLinkText,
            Intent appLinkIntent) {
        // In Channel, Intent.URI_INTENT_SCHEME is used to parse the URI. So the same flag should be
        // used when the URI is created.
        Channel testChannel = new Channel.Builder()
                .setPackageName(inputPackageName)
                .setAppLinkText(appLinkText)
                .setAppLinkIntentUri(appLinkIntent == null ? null : appLinkIntent.toUri(
                        Intent.URI_INTENT_SCHEME))
                .build();
        assertEquals("Unexpected app-link type for for " + testChannel,
                expectedType, testChannel.getAppLinkType(mMockContext));
    }

    @Test
    public void testComparator() {

        TvInputManagerHelper manager = Mockito.mock(TvInputManagerHelper.class);
        Mockito.when(manager.isPartnerInput(Matchers.anyString())).thenAnswer(
                new Answer<Boolean>() {
                    @Override
                    public Boolean answer(InvocationOnMock invocation) throws Throwable {
                        String inputId = (String) invocation.getArguments()[0];
                        return PARTNER_INPUT_ID.equals(inputId);
                    }
                });
        Comparator<Channel> comparator = new TestChannelComparator(manager);
        ComparatorTester<Channel> comparatorTester =
                ComparatorTester.withoutEqualsTest(comparator);
        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId(PARTNER_INPUT_ID).build());
        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId("1").build());
        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId("1").setDisplayNumber("2").build());
        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId("2").setDisplayNumber("1.0").build());

        // display name does not affect comparator
        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId("2").setDisplayNumber("1.62")
                        .setDisplayName("test1").build(),
                new Channel.Builder().setInputId("2").setDisplayNumber("1.62")
                        .setDisplayName("test2").build(),
                new Channel.Builder().setInputId("2").setDisplayNumber("1.62")
                        .setDisplayName("test3").build());
        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId("2").setDisplayNumber("2.0").build());
        // Numeric display number sorting
        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId("2").setDisplayNumber("12.2").build());
        comparatorTester.test();
    }

    /**
     * Test Input Label handled by {@link com.android.tv.data.Channel.DefaultComparator}.
     *
     * <p>Sort partner inputs first, then sort by input label, then by input id.
     * See <a href="http://b/23031603">b/23031603</a>.
     */
    @Test
    public void testComparatorLabel() {
        TvInputManagerHelper manager = Mockito.mock(TvInputManagerHelper.class);
        Mockito.when(manager.isPartnerInput(Matchers.anyString())).thenAnswer(
                new Answer<Boolean>() {
                    @Override
                    public Boolean answer(InvocationOnMock invocation) throws Throwable {
                        String inputId = (String) invocation.getArguments()[0];
                        return PARTNER_INPUT_ID.equals(inputId);
                    }
                });
        Comparator<Channel> comparator = new ChannelComparatorWithDescriptionAsLabel(manager);
        ComparatorTester<Channel> comparatorTester =
                ComparatorTester.withoutEqualsTest(comparator);

        comparatorTester.addComparableGroup(
                new Channel.Builder().setInputId(PARTNER_INPUT_ID).setDescription("A").build());

        // The description is used as a label for this test.
        comparatorTester.addComparableGroup(
                new Channel.Builder().setDescription("A").setInputId("1").build());
        comparatorTester.addComparableGroup(
                new Channel.Builder().setDescription("A").setInputId("2").build());
        comparatorTester.addComparableGroup(
                new Channel.Builder().setDescription("B").setInputId("1").build());

        comparatorTester.test();
    }

    @Test
    public void testNormalizeChannelNumber() {
        assertNormalizedDisplayNumber(null, null);
        assertNormalizedDisplayNumber("", "");
        assertNormalizedDisplayNumber("1", "1");
        assertNormalizedDisplayNumber("abcde", "abcde");
        assertNormalizedDisplayNumber("1-1", "1-1");
        assertNormalizedDisplayNumber("1.1", "1-1");
        assertNormalizedDisplayNumber("1 1", "1-1");
        assertNormalizedDisplayNumber("1\u058a1", "1-1");
        assertNormalizedDisplayNumber("1\u05be1", "1-1");
        assertNormalizedDisplayNumber("1\u14001", "1-1");
        assertNormalizedDisplayNumber("1\u18061", "1-1");
        assertNormalizedDisplayNumber("1\u20101", "1-1");
        assertNormalizedDisplayNumber("1\u20111", "1-1");
        assertNormalizedDisplayNumber("1\u20121", "1-1");
        assertNormalizedDisplayNumber("1\u20131", "1-1");
        assertNormalizedDisplayNumber("1\u20141", "1-1");
    }

    private void assertNormalizedDisplayNumber(String displayNumber, String normalized) {
        assertEquals(normalized, Channel.normalizeDisplayNumber(displayNumber));
    }

    private class TestChannelComparator extends Channel.DefaultComparator {
        public TestChannelComparator(TvInputManagerHelper manager) {
            super(null, manager);
        }

        @Override
        public String getInputLabelForChannel(Channel channel) {
            return channel.getInputId();
        }
    }

    private static class ChannelComparatorWithDescriptionAsLabel extends Channel.DefaultComparator {
        public ChannelComparatorWithDescriptionAsLabel(TvInputManagerHelper manager) {
            super(null, manager);
        }

        @Override
        public String getInputLabelForChannel(Channel channel) {
            return channel.getDescription();
        }
    }
}