1 /*
2  * Copyright (C) 2012 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.dialer.dialpad;
18 
19 import android.test.suitebuilder.annotation.SmallTest;
20 
21 import junit.framework.TestCase;
22 
23 /** Unit tests for {@link DialpadFragment}. */
24 @SmallTest
25 public class DialpadFragmentTest extends TestCase {
26 
testCanAddDigit_Valid()27     public void testCanAddDigit_Valid() {
28         // end, middle, selection to end, middle selection
29         assertTrue(DialpadFragment.canAddDigit("123", 3, 3, ';'));
30         assertTrue(DialpadFragment.canAddDigit("123", 1, 1, ','));
31         assertTrue(DialpadFragment.canAddDigit("123", 1, 3, ';'));
32         assertTrue(DialpadFragment.canAddDigit("123", 1, 2, ','));
33     }
34 
testCanAddDigit_InvalidCharacter()35     public void testCanAddDigit_InvalidCharacter() {
36         // only handles wait/pause
37         try {
38             DialpadFragment.canAddDigit("123", 1, 1, '5');
39             fail("Calling canAddDigit with invalid character should throw an exception");
40         } catch (IllegalArgumentException e) {
41         }
42     }
43 
testCanAddDigit_BadOrNoSelection()44     public void testCanAddDigit_BadOrNoSelection() {
45         // no selection
46         assertFalse(DialpadFragment.canAddDigit("123", -1, -1, ';'));
47         assertFalse(DialpadFragment.canAddDigit("123", -1, 1, ','));
48 
49         // start > end
50         assertFalse(DialpadFragment.canAddDigit("123", 2, 1, ','));
51     }
52 
testCanAddDigit_OutOfBounds()53     public void testCanAddDigit_OutOfBounds() {
54         // start or end is > digits.length()
55         assertFalse(DialpadFragment.canAddDigit("123", 1, 4, ';'));
56         assertFalse(DialpadFragment.canAddDigit("123", 4, 4, ','));
57     }
58 
testCanAddDigit_AsFirstCharacter()59     public void testCanAddDigit_AsFirstCharacter() {
60         assertFalse(DialpadFragment.canAddDigit("", 0, 0, ','));
61         assertFalse(DialpadFragment.canAddDigit("123", 0, 0, ';'));
62         assertFalse(DialpadFragment.canAddDigit("123", 0, 2, ','));
63         assertFalse(DialpadFragment.canAddDigit("123", 0, 3, ','));
64     }
65 
testCanAddDigit_AdjacentCharacters_Before()66     public void testCanAddDigit_AdjacentCharacters_Before() {
67         // before
68         assertFalse(DialpadFragment.canAddDigit("55;55", 2, 2, ';')); // WAIT
69         assertFalse(DialpadFragment.canAddDigit("55;55", 1, 2, ';'));
70         assertTrue(DialpadFragment.canAddDigit("55,55", 2, 2, ',')); // PAUSE
71         assertTrue(DialpadFragment.canAddDigit("55,55", 1, 2, ','));
72         assertTrue(DialpadFragment.canAddDigit("55;55", 2, 2, ',')); // WAIT & PAUSE
73         assertTrue(DialpadFragment.canAddDigit("55,55", 1, 2, ';'));
74     }
75 
testCanAddDigit_AdjacentCharacters_After()76     public void testCanAddDigit_AdjacentCharacters_After() {
77         // after
78         assertFalse(DialpadFragment.canAddDigit("55;55", 3, 3, ';')); // WAIT
79         assertFalse(DialpadFragment.canAddDigit("55;55", 3, 4, ';'));
80         assertTrue(DialpadFragment.canAddDigit("55,55", 3, 3, ',')); // PAUSE
81         assertTrue(DialpadFragment.canAddDigit("55,55", 3, 4, ','));
82         assertTrue(DialpadFragment.canAddDigit("55;55", 3, 3, ',')); // WAIT & PAUSE
83         assertTrue(DialpadFragment.canAddDigit("55,55", 3, 4, ';'));
84     }
85 }
86