Updating the behaviour of accessibility text iterators - CTS

1. Iterators were skipping content on reversing direction.

2. The cursor was positioned at the beginning of the next text segment
   when moving forward and at end of the previous text segment when moving
   backwards. This is incorrect and now the cursor is positioned at the
   end of the segment when moving forward and at the beginning when moving
   backward.

3. The cursor position was not properly set when reaching the end/start
   of the text.

4. The iterators were reporting strictly the next/previous segment even
   if the cursor is within such a segment. Thus, when traversing some
   content may be skipped. Now moving forward moves the selection to
   the next segment end and the start position is either the old index
   if it was within a segment or the start of the segment. Same in
   reverse.

bug:6575099

Change-Id: I170ee77b34233e56a47e4567da122291709da179
diff --git a/tests/tests/accessibilityservice/res/values/strings.xml b/tests/tests/accessibilityservice/res/values/strings.xml
index 16ad575..d022ce3 100644
--- a/tests/tests/accessibilityservice/res/values/strings.xml
+++ b/tests/tests/accessibilityservice/res/values/strings.xml
@@ -136,8 +136,7 @@
         computers. It is developed by the Open Handset Alliance, led by Google, and other companies.
         Google purchased the initial developer of the software, Android Inc., in 2005. The
         unveiling of the Android distribution in 2007 was announced with the founding of the Open
-        Handset Alliance, a consortium of 86 hardware, software, and telecommunication companies
-        devoted to advancing open </string>
+        Handset Alliance, a consortium of 86 hardware, software, and telecommunication </string>
 
     <string name="a_b">A B</string>
 
diff --git a/tests/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityTextTraversalTest.java b/tests/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityTextTraversalTest.java
index 0bc59f8..21c0271 100644
--- a/tests/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityTextTraversalTest.java
+++ b/tests/tests/accessibilityservice/src/android/accessibilityservice/cts/AccessibilityTextTraversalTest.java
@@ -50,7 +50,6 @@
     @MediumTest
     public void testActionNextAndPreviousAtGranularityCharacterOverContentDescription()
             throws Exception {
-
         final View view = getActivity().findViewById(R.id.view);
 
         getInstrumentation().runOnMainSync(new Runnable() {
@@ -167,36 +166,6 @@
                 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Move to the next character and wait for an event.
-        AccessibilityEvent fourthExpected = getInteractionBridge()
-                .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
-            @Override
-            public void run() {
-                getInteractionBridge().performAction(text,
-                        AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, arguments);
-            }
-        }, new AccessibilityEventFilter() {
-            @Override
-            public boolean accept(AccessibilityEvent event) {
-                return
-                (event.getEventType() ==
-                    AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY
-                        && event.getAction() ==
-                            AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
-                        && event.getPackageName().equals(getActivity().getPackageName())
-                        && event.getClassName().equals(View.class.getName())
-                        && event.getContentDescription().toString().equals(
-                                getString(R.string.a_b))
-                        && event.getFromIndex() == 2
-                        && event.getToIndex() == 3
-                        && event.getMovementGranularity() ==
-                            AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER);
-            }
-        }, TIMEOUT_ASYNC_PROCESSING);
-
-        // Make sure we got the expected event.
-        assertNotNull(fourthExpected);
-
-        // Move to the next character and wait for an event.
         AccessibilityEvent fifthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -531,8 +500,8 @@
         assertNotNull(firstExpected);
 
         // Verify the selection position.
-        assertEquals(0, Selection.getSelectionStart(textView.getText()));
-        assertEquals(0, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(1, Selection.getSelectionStart(textView.getText()));
+        assertEquals(1, Selection.getSelectionEnd(textView.getText()));
 
         // Move to the next character and wait for an event.
         AccessibilityEvent secondExpected = getInteractionBridge()
@@ -565,8 +534,8 @@
         assertNotNull(secondExpected);
 
         // Verify the selection position.
-        assertEquals(1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(2, Selection.getSelectionStart(textView.getText()));
+        assertEquals(2, Selection.getSelectionEnd(textView.getText()));
 
         // Move to the next character and wait for an event.
         AccessibilityEvent thirdExpected = getInteractionBridge()
@@ -599,48 +568,14 @@
         assertNotNull(thirdExpected);
 
         // Verify the selection position.
-        assertEquals(2, Selection.getSelectionStart(textView.getText()));
-        assertEquals(2, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(3, Selection.getSelectionStart(textView.getText()));
+        assertEquals(3, Selection.getSelectionEnd(textView.getText()));
 
         // Make sure there is no next.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
-
-        // Move to the next character and wait for an event.
-        AccessibilityEvent fourthExpected = getInteractionBridge()
-                .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
-            @Override
-            public void run() {
-                getInteractionBridge().performAction(text,
-                        AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, arguments);
-            }
-        }, new AccessibilityEventFilter() {
-            @Override
-            public boolean accept(AccessibilityEvent event) {
-                return
-                (event.getEventType() ==
-                    AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY
-                        && event.getAction() ==
-                            AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
-                        && event.getPackageName().equals(getActivity().getPackageName())
-                        && event.getClassName().equals(TextView.class.getName())
-                        && event.getText().size() > 0
-                        && event.getText().get(0).toString().equals(getString(R.string.a_b))
-                        && event.getFromIndex() == 2
-                        && event.getToIndex() == 3
-                        && event.getMovementGranularity() ==
-                            AccessibilityNodeInfo.MOVEMENT_GRANULARITY_CHARACTER);
-            }
-        }, TIMEOUT_ASYNC_PROCESSING);
-
-        // Make sure we got the expected event.
-        assertNotNull(fourthExpected);
-
-        // Verify the selection position.
         assertEquals(3, Selection.getSelectionStart(textView.getText()));
         assertEquals(3, Selection.getSelectionEnd(textView.getText()));
 
@@ -717,8 +652,8 @@
                 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(1, Selection.getSelectionStart(textView.getText()));
+        assertEquals(1, Selection.getSelectionEnd(textView.getText()));
     }
 
     @MediumTest
@@ -746,7 +681,7 @@
         arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                 AccessibilityNodeInfo.MOVEMENT_GRANULARITY_WORD);
 
-        // Move to the next character and wait for an event.
+        // Move to the next word and wait for an event.
         AccessibilityEvent firstExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -777,10 +712,10 @@
         assertNotNull(firstExpected);
 
         // Verify the selection position.
-        assertEquals(0, Selection.getSelectionStart(textView.getText()));
-        assertEquals(0, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(3, Selection.getSelectionStart(textView.getText()));
+        assertEquals(3, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next word and wait for an event.
         AccessibilityEvent secondExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -811,10 +746,10 @@
         assertNotNull(secondExpected);
 
         // Verify the selection position.
-        assertEquals(4, Selection.getSelectionStart(textView.getText()));
-        assertEquals(4, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(7, Selection.getSelectionStart(textView.getText()));
+        assertEquals(7, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next word and wait for an event.
         AccessibilityEvent thirdExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -845,18 +780,18 @@
         assertNotNull(thirdExpected);
 
         // Verify the selection position.
-        assertEquals(8, Selection.getSelectionStart(textView.getText()));
-        assertEquals(8, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(11, Selection.getSelectionStart(textView.getText()));
+        assertEquals(11, Selection.getSelectionEnd(textView.getText()));
 
         // Make sure there is no next.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(11, Selection.getSelectionStart(textView.getText()));
+        assertEquals(11, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next word and wait for an event.
         AccessibilityEvent fourthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -887,10 +822,10 @@
         assertNotNull(fourthExpected);
 
         // Verify the selection position.
-        assertEquals(11, Selection.getSelectionStart(textView.getText()));
-        assertEquals(11, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(8, Selection.getSelectionStart(textView.getText()));
+        assertEquals(8, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next word and wait for an event.
         AccessibilityEvent fifthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -921,8 +856,8 @@
         assertNotNull(fifthExpected);
 
         // Verify the selection position.
-        assertEquals(7, Selection.getSelectionStart(textView.getText()));
-        assertEquals(7, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(4, Selection.getSelectionStart(textView.getText()));
+        assertEquals(4, Selection.getSelectionEnd(textView.getText()));
 
         // Move to the next character and wait for an event.
         AccessibilityEvent sixthExpected = getInteractionBridge()
@@ -955,16 +890,16 @@
         assertNotNull(sixthExpected);
 
         // Verify the selection position.
-        assertEquals(3, Selection.getSelectionStart(textView.getText()));
-        assertEquals(3, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(0, Selection.getSelectionStart(textView.getText()));
+        assertEquals(0, Selection.getSelectionEnd(textView.getText()));
 
         // Make sure there is no previous.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(0, Selection.getSelectionStart(textView.getText()));
+        assertEquals(0, Selection.getSelectionEnd(textView.getText()));
     }
 
     @MediumTest
@@ -992,7 +927,7 @@
         arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                 AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE);
 
-        // Move to the next character and wait for an event.
+        // Move to the next line and wait for an event.
         AccessibilityEvent firstExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1024,10 +959,10 @@
         assertNotNull(firstExpected);
 
         // Verify the selection position.
-        assertEquals(0, Selection.getSelectionStart(textView.getText()));
-        assertEquals(0, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(25, Selection.getSelectionStart(textView.getText()));
+        assertEquals(25, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next line and wait for an event.
         AccessibilityEvent secondExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1059,10 +994,10 @@
         assertNotNull(secondExpected);
 
         // Verify the selection position.
-        assertEquals(25, Selection.getSelectionStart(textView.getText()));
-        assertEquals(25, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(53, Selection.getSelectionStart(textView.getText()));
+        assertEquals(53, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next line and wait for an event.
         AccessibilityEvent thirdExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1094,18 +1029,18 @@
         assertNotNull(thirdExpected);
 
         // Verify the selection position.
-        assertEquals(53, Selection.getSelectionStart(textView.getText()));
-        assertEquals(53, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(60, Selection.getSelectionStart(textView.getText()));
+        assertEquals(60, Selection.getSelectionEnd(textView.getText()));
 
         // Make sure there is no next.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(60, Selection.getSelectionStart(textView.getText()));
+        assertEquals(60, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous line and wait for an event.
         AccessibilityEvent fourthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1137,10 +1072,10 @@
         assertNotNull(fourthExpected);
 
         // Verify the selection position.
-        assertEquals(60, Selection.getSelectionStart(textView.getText()));
-        assertEquals(60, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(53, Selection.getSelectionStart(textView.getText()));
+        assertEquals(53, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous line and wait for an event.
         AccessibilityEvent fifthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1172,10 +1107,10 @@
         assertNotNull(fifthExpected);
 
         // Verify the selection position.
-        assertEquals(53, Selection.getSelectionStart(textView.getText()));
-        assertEquals(53, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(25, Selection.getSelectionStart(textView.getText()));
+        assertEquals(25, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous line and wait for an event.
         AccessibilityEvent sixthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1207,16 +1142,16 @@
         assertNotNull(sixthExpected);
 
         // Verify the selection position.
-        assertEquals(25, Selection.getSelectionStart(textView.getText()));
-        assertEquals(25, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(0, Selection.getSelectionStart(textView.getText()));
+        assertEquals(0, Selection.getSelectionEnd(textView.getText()));
 
         // Make sure there is no previous.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(0, Selection.getSelectionStart(textView.getText()));
+        assertEquals(0, Selection.getSelectionEnd(textView.getText()));
     }
 
     @MediumTest
@@ -1245,7 +1180,7 @@
         arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                 AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
 
-        // Move to the next character and wait for an event.
+        // Move to the next page and wait for an event.
         AccessibilityEvent firstExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1267,7 +1202,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki))
                         && event.getFromIndex() == 0
-                        && event.getToIndex() == 156
+                        && event.getToIndex() == 139
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
             }
@@ -1277,10 +1212,10 @@
         assertNotNull(firstExpected);
 
         // Verify the selection position.
-        assertEquals(0, Selection.getSelectionStart(editText.getText()));
-        assertEquals(0, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(139, Selection.getSelectionStart(editText.getText()));
+        assertEquals(139, Selection.getSelectionEnd(editText.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next page and wait for an event.
         AccessibilityEvent secondExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1301,8 +1236,8 @@
                         && event.getText().size() > 0
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki))
-                        && event.getFromIndex() == 156
-                        && event.getToIndex() == 318
+                        && event.getFromIndex() == 139
+                        && event.getToIndex() == 285
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
             }
@@ -1312,10 +1247,10 @@
         assertNotNull(secondExpected);
 
         // Verify the selection position.
-        assertEquals(156, Selection.getSelectionStart(editText.getText()));
-        assertEquals(156, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(285, Selection.getSelectionStart(editText.getText()));
+        assertEquals(285, Selection.getSelectionEnd(editText.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next page and wait for an event.
         AccessibilityEvent thirdExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1336,8 +1271,8 @@
                         && event.getText().size() > 0
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki))
-                        && event.getFromIndex() == 318
-                        && event.getToIndex() == 472
+                        && event.getFromIndex() == 285
+                        && event.getToIndex() == 436
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
             }
@@ -1347,18 +1282,18 @@
         assertNotNull(thirdExpected);
 
         // Verify the selection position.
-        assertEquals(318, Selection.getSelectionStart(editText.getText()));
-        assertEquals(318, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(436, Selection.getSelectionStart(editText.getText()));
+        assertEquals(436, Selection.getSelectionEnd(editText.getText()));
 
         // Make sure there is no next.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(editText.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(436, Selection.getSelectionStart(editText.getText()));
+        assertEquals(436, Selection.getSelectionEnd(editText.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous page and wait for an event.
         AccessibilityEvent fourthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1379,8 +1314,8 @@
                         && event.getText().size() > 0
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki))
-                        && event.getFromIndex() == 318
-                        && event.getToIndex() == 472
+                        && event.getFromIndex() == 285
+                        && event.getToIndex() == 436
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
             }
@@ -1390,10 +1325,10 @@
         assertNotNull(fourthExpected);
 
         // Verify the selection position.
-        assertEquals(472, Selection.getSelectionStart(editText.getText()));
-        assertEquals(472, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(285, Selection.getSelectionStart(editText.getText()));
+        assertEquals(285, Selection.getSelectionEnd(editText.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous page and wait for an event.
         AccessibilityEvent fifthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1414,8 +1349,8 @@
                         && event.getText().size() > 0
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki))
-                        && event.getFromIndex() == 156
-                        && event.getToIndex() == 318
+                        && event.getFromIndex() == 139
+                        && event.getToIndex() == 285
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
             }
@@ -1425,10 +1360,10 @@
         assertNotNull(fifthExpected);
 
         // Verify the selection position.
-        assertEquals(318, Selection.getSelectionStart(editText.getText()));
-        assertEquals(318, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(139, Selection.getSelectionStart(editText.getText()));
+        assertEquals(139, Selection.getSelectionEnd(editText.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous page and wait for an event.
         AccessibilityEvent sixthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1450,7 +1385,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki))
                         && event.getFromIndex() == 0
-                        && event.getToIndex() == 156
+                        && event.getToIndex() == 139
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PAGE);
             }
@@ -1460,16 +1395,16 @@
         assertNotNull(sixthExpected);
 
         // Verify the selection position.
-        assertEquals(156, Selection.getSelectionStart(editText.getText()));
-        assertEquals(156, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(0, Selection.getSelectionStart(editText.getText()));
+        assertEquals(0, Selection.getSelectionEnd(editText.getText()));
 
         // Make sure there is no previous.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(editText.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(editText.getText()));
+        assertEquals(0, Selection.getSelectionStart(editText.getText()));
+        assertEquals(0, Selection.getSelectionEnd(editText.getText()));
     }
 
     @MediumTest
@@ -1497,7 +1432,7 @@
         arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                 AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
 
-        // Move to the next character and wait for an event.
+        // Move to the next paragraph and wait for an event.
         AccessibilityEvent firstExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1519,7 +1454,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki_paragraphs))
                         && event.getFromIndex() == 2
-                        && event.getToIndex() == 106
+                        && event.getToIndex() == 104
                         && event.getMovementGranularity()
                             == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
             }
@@ -1529,10 +1464,10 @@
         assertNotNull(firstExpected);
 
         // Verify the selection position.
-        assertEquals(2, Selection.getSelectionStart(textView.getText()));
-        assertEquals(2, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(104, Selection.getSelectionStart(textView.getText()));
+        assertEquals(104, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next paragraph and wait for an event.
         AccessibilityEvent secondExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1554,7 +1489,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki_paragraphs))
                         && event.getFromIndex() == 106
-                        && event.getToIndex() == 268
+                        && event.getToIndex() == 267
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
             }
@@ -1564,10 +1499,10 @@
         assertNotNull(secondExpected);
 
         // Verify the selection position.
-        assertEquals(106, Selection.getSelectionStart(textView.getText()));
-        assertEquals(106, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(267, Selection.getSelectionStart(textView.getText()));
+        assertEquals(267, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the next paragraph and wait for an event.
         AccessibilityEvent thirdExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1589,7 +1524,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki_paragraphs))
                         && event.getFromIndex() == 268
-                        && event.getToIndex() == 584
+                        && event.getToIndex() == 582
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
             }
@@ -1599,18 +1534,18 @@
         assertNotNull(thirdExpected);
 
         // Verify the selection position.
-        assertEquals(268, Selection.getSelectionStart(textView.getText()));
-        assertEquals(268, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(582, Selection.getSelectionStart(textView.getText()));
+        assertEquals(582, Selection.getSelectionEnd(textView.getText()));
 
         // Make sure there is no next.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(582, Selection.getSelectionStart(textView.getText()));
+        assertEquals(582, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous paragraph and wait for an event.
         AccessibilityEvent fourthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1632,7 +1567,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki_paragraphs))
                         && event.getFromIndex() == 268
-                        && event.getToIndex() == 584
+                        && event.getToIndex() == 582
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
             }
@@ -1642,10 +1577,10 @@
         assertNotNull(fourthExpected);
 
         // Verify the selection position.
-        assertEquals(584, Selection.getSelectionStart(textView.getText()));
-        assertEquals(584, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(268, Selection.getSelectionStart(textView.getText()));
+        assertEquals(268, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous paragraph and wait for an event.
         AccessibilityEvent fifthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1667,7 +1602,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki_paragraphs))
                         && event.getFromIndex() == 106
-                        && event.getToIndex() == 268
+                        && event.getToIndex() == 267
                         && event.getMovementGranularity() ==
                             AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
             }
@@ -1677,10 +1612,10 @@
         assertNotNull(fifthExpected);
 
         // Verify the selection position.
-        assertEquals(268, Selection.getSelectionStart(textView.getText()));
-        assertEquals(268, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(106, Selection.getSelectionStart(textView.getText()));
+        assertEquals(106, Selection.getSelectionEnd(textView.getText()));
 
-        // Move to the next character and wait for an event.
+        // Move to the previous paragraph and wait for an event.
         AccessibilityEvent sixthExpected = getInteractionBridge()
                 .executeCommandAndWaitForAccessibilityEvent(new Runnable() {
             @Override
@@ -1702,7 +1637,7 @@
                         && event.getText().get(0).toString().equals(getString(
                                 R.string.android_wiki_paragraphs))
                         && event.getFromIndex() == 2
-                        && event.getToIndex() == 106
+                        && event.getToIndex() == 104
                         && event.getMovementGranularity()
                                 == AccessibilityNodeInfo.MOVEMENT_GRANULARITY_PARAGRAPH);
             }
@@ -1712,15 +1647,15 @@
         assertNotNull(sixthExpected);
 
         // Verify the selection position.
-        assertEquals(106, Selection.getSelectionStart(textView.getText()));
-        assertEquals(106, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(2, Selection.getSelectionStart(textView.getText()));
+        assertEquals(2, Selection.getSelectionEnd(textView.getText()));
 
         // Make sure there is no previous.
         assertFalse(getInteractionBridge().performAction(text,
                 AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY, arguments));
 
         // Verify the selection position.
-        assertEquals(-1, Selection.getSelectionStart(textView.getText()));
-        assertEquals(-1, Selection.getSelectionEnd(textView.getText()));
+        assertEquals(2, Selection.getSelectionStart(textView.getText()));
+        assertEquals(2, Selection.getSelectionEnd(textView.getText()));
     }
 }