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 package com.android.tv.data;
17 
18 import static com.android.tv.data.ChannelNumber.parseChannelNumber;
19 import static com.android.tv.testing.ChannelNumberSubject.assertThat;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import com.android.tv.testing.ComparableTester;
24 import com.android.tv.testing.constants.ConfigConstants;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.robolectric.RobolectricTestRunner;
29 import org.robolectric.annotation.Config;
30 
31 /** Tests for {@link ChannelNumber}. */
32 @RunWith(RobolectricTestRunner.class)
33 @Config(sdk = ConfigConstants.SDK)
34 public class ChannelNumberTest {
35 
36     @Test
newChannelNumber()37     public void newChannelNumber() {
38         assertThat(new ChannelNumber()).isEmpty();
39     }
40 
41     @Test
parseChannelNumber_empty()42     public void parseChannelNumber_empty() {
43         assertThat(parseChannelNumber("")).isNull();
44     }
45 
46     @Test
parseChannelNumber_dash()47     public void parseChannelNumber_dash() {
48         assertThat(parseChannelNumber("-")).isNull();
49     }
50 
51     @Test
parseChannelNumber_abcd12()52     public void parseChannelNumber_abcd12() {
53         assertThat(parseChannelNumber("abcd12")).isNull();
54     }
55 
56     @Test
parseChannelNumber_12abcd()57     public void parseChannelNumber_12abcd() {
58         assertThat(parseChannelNumber("12abcd")).isNull();
59     }
60 
61     @Test
parseChannelNumber_dash12()62     public void parseChannelNumber_dash12() {
63         assertThat(parseChannelNumber("-12")).isNull();
64     }
65 
66     @Test
parseChannelNumber_1()67     public void parseChannelNumber_1() {
68         assertThat(parseChannelNumber("1")).displaysAs(1);
69     }
70 
71     @Test
parseChannelNumber_1234dash4321()72     public void parseChannelNumber_1234dash4321() {
73         assertThat(parseChannelNumber("1234-4321")).displaysAs(1234, 4321);
74     }
75 
76     @Test
parseChannelNumber_3dash4()77     public void parseChannelNumber_3dash4() {
78         assertThat(parseChannelNumber("3-4")).displaysAs(3, 4);
79     }
80 
81     @Test
parseChannelNumber_5dash6()82     public void parseChannelNumber_5dash6() {
83         assertThat(parseChannelNumber("5-6")).displaysAs(5, 6);
84     }
85 
86     @Test
compareTo()87     public void compareTo() {
88         new ComparableTester<ChannelNumber>()
89                 .addEquivalentGroup(parseChannelNumber("1"), parseChannelNumber("1"))
90                 .addEquivalentGroup(parseChannelNumber("2"))
91                 .addEquivalentGroup(parseChannelNumber("2-1"))
92                 .addEquivalentGroup(parseChannelNumber("2-2"))
93                 .addEquivalentGroup(parseChannelNumber("2-10"))
94                 .addEquivalentGroup(parseChannelNumber("3"))
95                 .addEquivalentGroup(parseChannelNumber("4"), parseChannelNumber("4-0"))
96                 .addEquivalentGroup(parseChannelNumber("10"))
97                 .addEquivalentGroup(parseChannelNumber("100"))
98                 .test();
99     }
100 
101     @Test
compare_null_null()102     public void compare_null_null() {
103         assertThat(ChannelNumber.compare(null, null)).isEqualTo(0);
104     }
105 
106     @Test
compare_1_1()107     public void compare_1_1() {
108         assertThat(ChannelNumber.compare("1", "1")).isEqualTo(0);
109         ;
110     }
111 
112     @Test
compare_null_1()113     public void compare_null_1() {
114         assertThat(ChannelNumber.compare(null, "1")).isLessThan(0);
115     }
116 
117     @Test
compare_abcd_1()118     public void compare_abcd_1() {
119         assertThat(ChannelNumber.compare("abcd", "1")).isLessThan(0);
120     }
121 
122     @Test
compare_dash1_1()123     public void compare_dash1_1() {
124         assertThat(ChannelNumber.compare(".4", "1")).isLessThan(0);
125     }
126 
127     @Test
compare_1_null()128     public void compare_1_null() {
129         assertThat(ChannelNumber.compare("1", null)).isGreaterThan(0);
130     }
131 
132     @Test
equivalent_null_to_null()133     public void equivalent_null_to_null() {
134         assertThat(ChannelNumber.equivalent(null, null)).isTrue();
135     }
136 
137     @Test
equivalent_1_to_1()138     public void equivalent_1_to_1() {
139         assertThat(ChannelNumber.equivalent("1", "1")).isTrue();
140     }
141 
142     @Test
equivalent_1d2_to_1()143     public void equivalent_1d2_to_1() {
144         assertThat(ChannelNumber.equivalent("1-2", "1")).isTrue();
145     }
146 
147     @Test
equivalent_1_to_1d2()148     public void equivalent_1_to_1d2() {
149         assertThat(ChannelNumber.equivalent("1", "1-2")).isTrue();
150     }
151 
152     @Test
equivalent_1_to_2_isFalse()153     public void equivalent_1_to_2_isFalse() {
154         assertThat(ChannelNumber.equivalent("1", "2")).isFalse();
155     }
156 
157     @Test
equivalent_1d1_to_1d1()158     public void equivalent_1d1_to_1d1() {
159         assertThat(ChannelNumber.equivalent("1-1", "1-1")).isTrue();
160     }
161 
162     @Test
equivalent_1d1_to_1d2_isFalse()163     public void equivalent_1d1_to_1d2_isFalse() {
164         assertThat(ChannelNumber.equivalent("1-1", "1-2")).isFalse();
165     }
166 
167     @Test
equivalent_1_to_null_isFalse()168     public void equivalent_1_to_null_isFalse() {
169         assertThat(ChannelNumber.equivalent("1", null)).isFalse();
170     }
171 
172     @Test
equivalent_null_to_1_isFalse()173     public void equivalent_null_to_1_isFalse() {
174         assertThat(ChannelNumber.equivalent(null, "1")).isFalse();
175     }
176 }
177