Home | History | Annotate | Download | only in latin
      1 /*
      2  * Copyright (C) 2013 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.inputmethod.latin;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 /**
     23  * Unit tests for WordComposer.
     24  */
     25 @SmallTest
     26 public class WordComposerTests extends AndroidTestCase {
     27     public void testMoveCursor() {
     28         final WordComposer wc = new WordComposer();
     29         final String STR_WITHIN_BMP = "abcdef";
     30         wc.setComposingWord(STR_WITHIN_BMP, null);
     31         assertEquals(wc.size(),
     32                 STR_WITHIN_BMP.codePointCount(0, STR_WITHIN_BMP.length()));
     33         assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
     34         wc.setCursorPositionWithinWord(2);
     35         assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
     36         // Move the cursor to after the 'd'
     37         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(2));
     38         assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
     39         // Move the cursor to after the 'e'
     40         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(1));
     41         assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
     42         assertEquals(wc.size(), 6);
     43         // Move the cursor to after the 'f'
     44         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(1));
     45         assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
     46         // Move the cursor past the end of the word
     47         assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(1));
     48         assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(15));
     49 
     50         // \uD861\uDED7 is , a character outside the BMP
     51         final String STR_WITH_SUPPLEMENTARY_CHAR = "abcde\uD861\uDED7fgh";
     52         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     53         assertEquals(wc.size(), STR_WITH_SUPPLEMENTARY_CHAR.codePointCount(0,
     54                         STR_WITH_SUPPLEMENTARY_CHAR.length()));
     55         assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
     56         wc.setCursorPositionWithinWord(3);
     57         assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
     58         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(6));
     59         assertTrue(wc.isCursorFrontOrMiddleOfComposingWord());
     60         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(1));
     61         assertFalse(wc.isCursorFrontOrMiddleOfComposingWord());
     62 
     63         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     64         wc.setCursorPositionWithinWord(3);
     65         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(7));
     66 
     67         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     68         wc.setCursorPositionWithinWord(3);
     69         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(7));
     70 
     71         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     72         wc.setCursorPositionWithinWord(3);
     73         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(-3));
     74         assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(-1));
     75 
     76         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     77         wc.setCursorPositionWithinWord(3);
     78         assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(-9));
     79 
     80         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     81         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(-10));
     82 
     83         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     84         assertFalse(wc.moveCursorByAndReturnIfInsideComposingWord(-11));
     85 
     86         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     87         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(0));
     88 
     89         wc.setComposingWord(STR_WITH_SUPPLEMENTARY_CHAR, null);
     90         wc.setCursorPositionWithinWord(2);
     91         assertTrue(wc.moveCursorByAndReturnIfInsideComposingWord(0));
     92     }
     93 }
     94