1 /*
2  * Copyright (C) 2015 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 
20 import android.support.test.filters.SmallTest;
21 
22 import com.android.tv.testing.ComparableTester;
23 
24 import junit.framework.TestCase;
25 
26 /**
27  * Tests for {@link ChannelNumber}.
28  */
29 @SmallTest
30 public class ChannelNumberTest extends TestCase {
31 
32     /**
33      * Test method for {@link ChannelNumber#ChannelNumber()}.
34      */
testChannelNumber()35     public void testChannelNumber() {
36         assertChannelEquals(new ChannelNumber(), "", false, "");
37     }
38 
39     /**
40      * Test method for
41      * {@link com.android.tv.data.ChannelNumber#parseChannelNumber(java.lang.String)}.
42      */
testParseChannelNumber()43     public void testParseChannelNumber() {
44         assertNull(parseChannelNumber(""));
45         assertNull(parseChannelNumber(" "));
46         assertNull(parseChannelNumber("abcd12"));
47         assertNull(parseChannelNumber("12abcd"));
48         assertNull(parseChannelNumber("-12"));
49         assertChannelEquals(parseChannelNumber("1"), "1", false, "");
50         assertChannelEquals(parseChannelNumber("1234 4321"), "1234", true, "4321");
51         assertChannelEquals(parseChannelNumber("3-4"), "3", true, "4");
52         assertChannelEquals(parseChannelNumber("5.6"), "5", true, "6");
53     }
54 
55     /**
56      * Test method for {@link ChannelNumber#compareTo(com.android.tv.data.ChannelNumber)}.
57      */
testCompareTo()58     public void testCompareTo() {
59         new ComparableTester<ChannelNumber>()
60                 .addEquivalentGroup(parseChannelNumber("1"), parseChannelNumber("1"))
61                 .addEquivalentGroup(parseChannelNumber("2"))
62                 .addEquivalentGroup(parseChannelNumber("2 1"), parseChannelNumber("2.1"),
63                         parseChannelNumber("2-1"))
64                 .addEquivalentGroup(parseChannelNumber("2-2"))
65                 .addEquivalentGroup(parseChannelNumber("2-10"))
66                 .addEquivalentGroup(parseChannelNumber("3"))
67                 .addEquivalentGroup(parseChannelNumber("4"), parseChannelNumber("4 0"),
68                         parseChannelNumber("4.0"), parseChannelNumber("4-0"))
69                 .addEquivalentGroup(parseChannelNumber("10"))
70                 .addEquivalentGroup(parseChannelNumber("100"))
71                 .test();
72     }
73 
74     /**
75      * Test method for {@link ChannelNumber#compare(java.lang.String, java.lang.String)}.
76      */
testCompare()77     public void testCompare() {
78         // Only need to test nulls, the reset is tested by testCompareTo
79         assertEquals("compareTo(null,null)", 0, ChannelNumber.compare(null, null));
80         assertEquals("compareTo(1,1)", 0, ChannelNumber.compare("1", "1"));
81         assertEquals("compareTo(null,1)<0", true, ChannelNumber.compare(null, "1") < 0);
82         assertEquals("compareTo(mal-formatted,1)<0", true, ChannelNumber.compare("abcd", "1") < 0);
83         assertEquals("compareTo(mal-formatted,1)<0", true, ChannelNumber.compare(".4", "1") < 0);
84         assertEquals("compareTo(1,null)>0", true, ChannelNumber.compare("1", null) > 0);
85     }
86 
assertChannelEquals(ChannelNumber actual, String expectedMajor, boolean expectedHasDelimiter, String expectedMinor)87     private void assertChannelEquals(ChannelNumber actual, String expectedMajor,
88             boolean expectedHasDelimiter, String expectedMinor) {
89         assertEquals(actual + " major", actual.majorNumber, expectedMajor);
90         assertEquals(actual + " hasDelimiter", actual.hasDelimiter, expectedHasDelimiter);
91         assertEquals(actual + " minor", actual.minorNumber, expectedMinor);
92     }
93 
94 }
95