Home | History | Annotate | Download | only in dialpad
      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 
     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 
     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 
     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 
     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 
     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 
     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 
     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     public void testGetFormattedDigits_NoPostDialString() {
     87         assertEquals("(510) 333-7596",
     88                 DialpadFragment.getFormattedDigits("5103337596", null, "US"));
     89         assertEquals("(510) 333-7596",
     90                 DialpadFragment.getFormattedDigits("5103337596", "+15103337596", "US"));
     91     }
     92 
     93     public void testGetFormattedDigits_WithPostDialString() {
     94         assertEquals("(510) 333-7596,1234",
     95                 DialpadFragment.getFormattedDigits("5103337596,1234", null, "US"));
     96         assertEquals("(510) 333-7596;;1234",
     97                 DialpadFragment.getFormattedDigits("5103337596;;1234", null, "US"));
     98         assertEquals("(510) 333-7596;123,,4",
     99                 DialpadFragment.getFormattedDigits("(510)3337596;123,,4", "+15103337596", "US"));
    100     }
    101 
    102     public void testGetFormattedDigits_PostDialStringOnly() {
    103         assertEquals(",1234567", DialpadFragment.getFormattedDigits(",1234567", null, "US"));
    104         assertEquals(";4321", DialpadFragment.getFormattedDigits(";4321", null, "US"));
    105     }
    106 
    107     public void testGetFormattedDigits_Invalid() {
    108         assertEquals(null, DialpadFragment.getFormattedDigits(null, null, "US"));
    109         assertEquals("", DialpadFragment.getFormattedDigits("", "+15104233335", "US"));
    110     }
    111 }
    112