Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 android.text.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertNull;
     22 import static org.junit.Assert.fail;
     23 import static org.mockito.Matchers.anyChar;
     24 import static org.mockito.Mockito.never;
     25 import static org.mockito.Mockito.reset;
     26 import static org.mockito.Mockito.spy;
     27 import static org.mockito.Mockito.times;
     28 import static org.mockito.Mockito.verify;
     29 
     30 import android.support.test.filters.SmallTest;
     31 import android.support.test.runner.AndroidJUnit4;
     32 import android.text.LoginFilter;
     33 import android.text.LoginFilter.UsernameFilterGeneric;
     34 import android.text.SpannableString;
     35 import android.text.Spanned;
     36 import android.text.SpannedString;
     37 
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 
     41 @SmallTest
     42 @RunWith(AndroidJUnit4.class)
     43 public class LoginFilterTest {
     44     @Test
     45     public void testFilter() {
     46         CharSequence result;
     47         LoginFilter loginFilter = spy(new UsernameFilterGeneric());
     48         Spanned dest1 = new SpannedString("dest_without_invalid_char");
     49         Spanned dest2 = new SpannedString("&*dest_with_invalid_char#$");
     50         String source1 = "source_without_invalid_char";
     51         String source2 = "+=source_with_invalid_char%!";
     52         Spanned spannedSource = new SpannedString("&*spanned_source_with_invalid_char#$");
     53 
     54         verify(loginFilter, never()).onStart();
     55         verify(loginFilter, never()).onStop();
     56         verify(loginFilter, never()).onInvalidCharacter(anyChar());
     57 
     58         assertNull(loginFilter.filter(source1, 0, source1.length(), dest1, 0, dest1.length()));
     59         verify(loginFilter, times(1)).onStart();
     60         verify(loginFilter, times(1)).onStop();
     61         verify(loginFilter, never()).onInvalidCharacter(anyChar());
     62 
     63         reset(loginFilter);
     64         assertNull(loginFilter.filter(source1, 0, source1.length(), dest2, 5, 6));
     65         verify(loginFilter, times(1)).onStart();
     66         verify(loginFilter, times(1)).onStop();
     67         verify(loginFilter, times(4)).onInvalidCharacter(anyChar());
     68 
     69         loginFilter = spy(new UsernameFilterGeneric(true));
     70         assertNull(loginFilter.filter(source2, 0, source2.length(),
     71                 dest1, 0, dest1.length()));
     72         verify(loginFilter, times(1)).onStart();
     73         verify(loginFilter, times(1)).onStop();
     74         verify(loginFilter, times(3)).onInvalidCharacter(anyChar());
     75 
     76         reset(loginFilter);
     77         assertNull(loginFilter.filter(spannedSource, 0, spannedSource.length(),
     78                 dest1, 0, dest1.length()));
     79         verify(loginFilter, times(1)).onStart();
     80         verify(loginFilter, times(1)).onStop();
     81         verify(loginFilter, times(4)).onInvalidCharacter(anyChar());
     82 
     83         loginFilter = spy(new UsernameFilterGeneric(false));
     84         result = loginFilter.filter(source2, 0, source2.length(), dest1, 0, dest1.length());
     85         assertFalse(result instanceof SpannableString);
     86         assertEquals("+source_with_invalid_char", result.toString());
     87         verify(loginFilter, times(1)).onStart();
     88         verify(loginFilter, times(1)).onStop();
     89         verify(loginFilter, times(3)).onInvalidCharacter(anyChar());
     90 
     91         reset(loginFilter);
     92         result = loginFilter.filter(spannedSource, 0, spannedSource.length(),
     93                 dest1, 0, dest1.length());
     94         assertEquals("spanned_source_with_invalid_char", result.toString());
     95         verify(loginFilter, times(1)).onStart();
     96         verify(loginFilter, times(1)).onStop();
     97         verify(loginFilter, times(4)).onInvalidCharacter(anyChar());
     98 
     99         try {
    100             loginFilter.filter(null, 0, source1.length(), dest1, 0, dest1.length());
    101             fail("should throw NullPointerException when source is null");
    102         } catch (NullPointerException e) {
    103         }
    104 
    105         try {
    106             // start and end are out of bound.
    107             loginFilter.filter(source1, -1, source1.length() + 1, dest1, 0, dest1.length());
    108             fail("should throw StringIndexOutOfBoundsException" +
    109                     " when start and end are out of bound");
    110         } catch (StringIndexOutOfBoundsException e) {
    111         }
    112 
    113         // start is larger than end.
    114         assertNull("should return null when start is larger than end",
    115                 loginFilter.filter(source1, source1.length(), 0, dest1, 0, dest1.length()));
    116 
    117         try {
    118             loginFilter.filter(source1, 0, source1.length(), null, 2, dest1.length());
    119             fail("should throw NullPointerException when dest is null");
    120         } catch (NullPointerException e) {
    121         }
    122 
    123         // dstart and dend are out of bound.
    124         loginFilter.filter(source1, 0, source1.length(), dest1, -1, dest1.length() + 1);
    125 
    126         // dstart is larger than dend.
    127         loginFilter.filter(source1, 0, source1.length(), dest1, dest1.length(),  0);
    128     }
    129 
    130     // This method does nothing. we only test onInvalidCharacter function here,
    131     // the callback should be tested in testFilter()
    132     @Test
    133     public void testOnInvalidCharacter() {
    134         LoginFilter loginFilter = new UsernameFilterGeneric();
    135         loginFilter.onInvalidCharacter('a');
    136     }
    137 
    138     // This method does nothing. we only test onStop function here,
    139     // the callback should be tested in testFilter()
    140     @Test
    141     public void testOnStop() {
    142         LoginFilter loginFilter = new UsernameFilterGeneric();
    143         loginFilter.onStop();
    144     }
    145 
    146     // This method does nothing. we only test onStart function here,
    147     // the callback should be tested in testFilter()
    148     @Test
    149     public void testOnStart() {
    150         LoginFilter loginFilter = new UsernameFilterGeneric();
    151         loginFilter.onStart();
    152     }
    153 }
    154