Merge "Adds CTS test for AccessibilityEvent constructors" into rvc-dev am: 1215d36f73 am: d20929e5f5 am: 8dcf790121 am: 73b90c5927

Change-Id: Ic008315c33d5cb2fe64ac5d7f4ec2ae12296daf5
diff --git a/tests/accessibility/src/android/view/accessibility/cts/AccessibilityEventTest.java b/tests/accessibility/src/android/view/accessibility/cts/AccessibilityEventTest.java
index 8143a82..9b7951f 100644
--- a/tests/accessibility/src/android/view/accessibility/cts/AccessibilityEventTest.java
+++ b/tests/accessibility/src/android/view/accessibility/cts/AccessibilityEventTest.java
@@ -33,12 +33,14 @@
 import android.view.accessibility.AccessibilityManager;
 import android.view.accessibility.AccessibilityNodeInfo;
 import android.view.accessibility.AccessibilityRecord;
-import android.view.accessibility.cts.SpeakingAccessibilityService;
 import android.widget.LinearLayout;
+
 import androidx.test.filters.SmallTest;
 import androidx.test.rule.ActivityTestRule;
 import androidx.test.runner.AndroidJUnit4;
+
 import junit.framework.TestCase;
+
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -46,11 +48,8 @@
 import org.junit.runner.RunWith;
 
 import java.util.ArrayList;
-import java.util.concurrent.Callable;
 import java.util.List;
 
-import static org.junit.Assert.*;
-
 /**
  * Class for testing {@link AccessibilityEvent}.
  */
@@ -107,7 +106,7 @@
 
     @Test
     public void testScrollEvent() throws Exception {
-        mChildView.scrollTo(0,100);
+        mChildView.scrollTo(0, 100);
         Thread.sleep(1000);
         scrollEventFilter.assertReceivedEventCount(1);
     }
@@ -225,7 +224,8 @@
 
     private AccessibilityEventFilter stateDescriptionEventFilter = new AccessibilityEventFilter() {
         public boolean pass(AccessibilityEvent event) {
-            return event.getContentChangeTypes() == AccessibilityEvent.CONTENT_CHANGE_TYPE_STATE_DESCRIPTION;
+            return event.getContentChangeTypes()
+                    == AccessibilityEvent.CONTENT_CHANGE_TYPE_STATE_DESCRIPTION;
         }
     };
 
@@ -261,7 +261,8 @@
     }
 
     private void sendStateDescriptionChangedEvent(View view, CharSequence text) {
-        AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
+        AccessibilityEvent event = AccessibilityEvent.obtain(
+                AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
         event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_STATE_DESCRIPTION);
         event.getText().add(text);
         view.sendAccessibilityEventUnchecked(event);
@@ -285,7 +286,7 @@
         AccessibilityEvent receivedEvent = AccessibilityEvent.CREATOR.createFromParcel(parcel);
 
         // make sure all fields properly marshaled
-        assertEqualsAccessiblityEvent(sentEvent, receivedEvent);
+        assertEqualsAccessibilityEvent(sentEvent, receivedEvent);
 
         parcel.recycle();
     }
@@ -349,7 +350,7 @@
         assertEquals("TYPE_VIEW_SELECTED", AccessibilityEvent.eventTypeToString(
                 AccessibilityEvent.TYPE_VIEW_SELECTED));
         assertEquals("TYPE_VIEW_TEXT_CHANGED", AccessibilityEvent.eventTypeToString(
-                AccessibilityEvent .TYPE_VIEW_TEXT_CHANGED));
+                AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED));
         assertEquals("TYPE_VIEW_TEXT_SELECTION_CHANGED", AccessibilityEvent.eventTypeToString(
                 AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED));
         assertEquals("TYPE_WINDOW_CONTENT_CHANGED", AccessibilityEvent.eventTypeToString(
@@ -379,14 +380,19 @@
     @SmallTest
     @Test
     public void testMarshaling2() {
+        // fully populate the event to marshal
         AccessibilityEvent marshaledEvent = AccessibilityEvent.obtain();
         fullyPopulateAccessibilityEvent(marshaledEvent);
+
+        // marshal and unmarshal the event
         Parcel parcel = Parcel.obtain();
         marshaledEvent.writeToParcel(parcel, 0);
         parcel.setDataPosition(0);
         AccessibilityEvent unmarshaledEvent = AccessibilityEvent.obtain();
         unmarshaledEvent.initFromParcel(parcel);
-        assertEqualsAccessiblityEvent(marshaledEvent, unmarshaledEvent);
+
+        // make sure all fields properly marshaled
+        assertEqualsAccessibilityEvent(marshaledEvent, unmarshaledEvent);
 
         parcel.recycle();
     }
@@ -412,6 +418,23 @@
         assertTrue(TextUtils.equals(originalText, event.getContentDescription()));
     }
 
+    @SmallTest
+    @Test
+    public void testConstructors() {
+        final AccessibilityEvent populatedEvent = new AccessibilityEvent();
+        fullyPopulateAccessibilityEvent(populatedEvent);
+        final AccessibilityEvent event = new AccessibilityEvent(populatedEvent);
+
+        assertEqualsAccessibilityEvent(event, populatedEvent);
+
+        final AccessibilityEvent firstEvent = new AccessibilityEvent();
+        firstEvent.setEventType(AccessibilityEvent.TYPE_VIEW_FOCUSED);
+        final AccessibilityEvent secondEvent = new AccessibilityEvent(
+                AccessibilityEvent.TYPE_VIEW_FOCUSED);
+
+        assertEqualsAccessibilityEvent(firstEvent, secondEvent);
+    }
+
     /**
      * Fully populates the {@link AccessibilityEvent} to marshal.
      *
@@ -451,14 +474,13 @@
         sentEvent.appendRecord(record);
     }
 
-
     /**
      * Compares all properties of the <code>expectedEvent</code> and the
-     * <code>receviedEvent</code> to verify that the received event is the one
+     * <code>receivedEvent</code> to verify that the received event is the one
      * that is expected.
      */
-    private static void assertEqualsAccessiblityEvent(AccessibilityEvent expectedEvent,
-                                                      AccessibilityEvent receivedEvent) {
+    private static void assertEqualsAccessibilityEvent(AccessibilityEvent expectedEvent,
+            AccessibilityEvent receivedEvent) {
         assertEquals("addedCount has incorrect value", expectedEvent.getAddedCount(), receivedEvent
                 .getAddedCount());
         assertEquals("beforeText has incorrect value", expectedEvent.getBeforeText(), receivedEvent
@@ -485,38 +507,44 @@
                 .isPassword());
         assertEquals("removedCount has incorrect value", expectedEvent.getRemovedCount(),
                 receivedEvent.getRemovedCount());
-        AccessibilityRecordTest.assertEqualsText(expectedEvent.getText(), receivedEvent.getText());
-        assertEquals("must have one record", expectedEvent.getRecordCount(),
-                receivedEvent.getRecordCount());
-        assertSame("maxScrollX has incorect value", expectedEvent.getMaxScrollX(),
+        assertSame("maxScrollX has incorrect value", expectedEvent.getMaxScrollX(),
                 receivedEvent.getMaxScrollX());
-        assertSame("maxScrollY has incorect value", expectedEvent.getMaxScrollY(),
+        assertSame("maxScrollY has incorrect value", expectedEvent.getMaxScrollY(),
                 receivedEvent.getMaxScrollY());
-        assertSame("scrollX has incorect value", expectedEvent.getScrollX(),
+        assertSame("scrollX has incorrect value", expectedEvent.getScrollX(),
                 receivedEvent.getScrollX());
-        assertSame("scrollY has incorect value", expectedEvent.getScrollY(),
+        assertSame("scrollY has incorrect value", expectedEvent.getScrollY(),
                 receivedEvent.getScrollY());
-        assertSame("scrollDeltaX has incorect value", expectedEvent.getScrollDeltaX(),
+        assertSame("scrollDeltaX has incorrect value", expectedEvent.getScrollDeltaX(),
                 receivedEvent.getScrollDeltaX());
-        assertSame("scrollDeltaY has incorect value", expectedEvent.getScrollDeltaY(),
+        assertSame("scrollDeltaY has incorrect value", expectedEvent.getScrollDeltaY(),
                 receivedEvent.getScrollDeltaY());
-        assertSame("toIndex has incorect value", expectedEvent.getToIndex(),
+        assertSame("toIndex has incorrect value", expectedEvent.getToIndex(),
                 receivedEvent.getToIndex());
-        assertSame("scrollable has incorect value", expectedEvent.isScrollable(),
+        assertSame("scrollable has incorrect value", expectedEvent.isScrollable(),
                 receivedEvent.isScrollable());
-        assertSame("granularity has incorect value", expectedEvent.getMovementGranularity(),
+        assertSame("granularity has incorrect value", expectedEvent.getMovementGranularity(),
                 receivedEvent.getMovementGranularity());
-        assertSame("action has incorect value", expectedEvent.getAction(),
+        assertSame("action has incorrect value", expectedEvent.getAction(),
                 receivedEvent.getAction());
-        assertSame("windowChangeTypes has incorect value", expectedEvent.getWindowChanges(),
+        assertSame("windowChangeTypes has incorrect value", expectedEvent.getWindowChanges(),
                 receivedEvent.getWindowChanges());
 
-        assertSame("parcelableData has incorect value",
-                ((Message) expectedEvent.getParcelableData()).what,
-                ((Message) receivedEvent.getParcelableData()).what);
+        AccessibilityRecordTest.assertEqualsText(expectedEvent.getText(), receivedEvent.getText());
+        AccessibilityRecordTest.assertEqualAccessibilityRecord(expectedEvent, receivedEvent);
 
-        AccessibilityRecord receivedRecord = receivedEvent.getRecord(0);
-        AccessibilityRecordTest.assertEqualAccessibilityRecord(expectedEvent, receivedRecord);
+        assertEqualAppendedRecord(expectedEvent, receivedEvent);
+    }
+
+    private static void assertEqualAppendedRecord(AccessibilityEvent expectedEvent,
+            AccessibilityEvent receivedEvent) {
+        assertEquals("recordCount has incorrect value", expectedEvent.getRecordCount(),
+                receivedEvent.getRecordCount());
+        if (expectedEvent.getRecordCount() != 0 && receivedEvent.getRecordCount() != 0) {
+            AccessibilityRecord expectedRecord =  expectedEvent.getRecord(0);
+            AccessibilityRecord receivedRecord = receivedEvent.getRecord(0);
+            AccessibilityRecordTest.assertEqualAccessibilityRecord(expectedRecord, receivedRecord);
+        }
     }
 
     /**
diff --git a/tests/accessibility/src/android/view/accessibility/cts/AccessibilityRecordTest.java b/tests/accessibility/src/android/view/accessibility/cts/AccessibilityRecordTest.java
index 119de9f..08d6637 100644
--- a/tests/accessibility/src/android/view/accessibility/cts/AccessibilityRecordTest.java
+++ b/tests/accessibility/src/android/view/accessibility/cts/AccessibilityRecordTest.java
@@ -17,6 +17,7 @@
 package android.view.accessibility.cts;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
 
 import android.accessibility.cts.common.AccessibilityDumpOnFailureRule;
@@ -160,21 +161,28 @@
         assertEquals("removedCount has incorrect value", expectedRecord.getRemovedCount(),
                 receivedRecord.getRemovedCount());
         assertEqualsText(expectedRecord.getText(), receivedRecord.getText());
-        assertSame("maxScrollX has incorect value", expectedRecord.getMaxScrollX(),
+        assertSame("maxScrollX has incorrect value", expectedRecord.getMaxScrollX(),
                 receivedRecord.getMaxScrollX());
-        assertSame("maxScrollY has incorect value", expectedRecord.getMaxScrollY(),
+        assertSame("maxScrollY has incorrect value", expectedRecord.getMaxScrollY(),
                 receivedRecord.getMaxScrollY());
-        assertSame("scrollX has incorect value", expectedRecord.getScrollX(),
+        assertSame("scrollX has incorrect value", expectedRecord.getScrollX(),
                 receivedRecord.getScrollX());
-        assertSame("scrollY has incorect value", expectedRecord.getScrollY(),
+        assertSame("scrollY has incorrect value", expectedRecord.getScrollY(),
                 receivedRecord.getScrollY());
-        assertSame("toIndex has incorect value", expectedRecord.getToIndex(),
+        assertSame("toIndex has incorrect value", expectedRecord.getToIndex(),
                 receivedRecord.getToIndex());
-        assertSame("scrollable has incorect value", expectedRecord.isScrollable(),
+        assertSame("scrollable has incorrect value", expectedRecord.isScrollable(),
                 receivedRecord.isScrollable());
-        assertSame("parcelableData has incorect value",
-                ((Message) expectedRecord.getParcelableData()).what,
-                ((Message) receivedRecord.getParcelableData()).what);
+
+        assertFalse("one of the parcelableData is null",
+                expectedRecord.getParcelableData() == null
+                        ^ receivedRecord.getParcelableData() == null);
+        if (expectedRecord.getParcelableData() != null
+                && receivedRecord.getParcelableData() != null) {
+            assertSame("parcelableData has incorrect value",
+                    ((Message) expectedRecord.getParcelableData()).what,
+                    ((Message) receivedRecord.getParcelableData()).what);
+        }
     }
 
     /**