Home | History | Annotate | Download | only in cts
      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 android.view.accessibility.cts;
     18 
     19 import android.os.Message;
     20 import android.platform.test.annotations.Presubmit;
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 import android.view.accessibility.AccessibilityEvent;
     24 import android.view.accessibility.AccessibilityRecord;
     25 
     26 import junit.framework.TestCase;
     27 
     28 import java.lang.reflect.Field;
     29 import java.lang.reflect.Modifier;
     30 import java.util.Iterator;
     31 import java.util.List;
     32 
     33 /**
     34  * Class for testing {@link AccessibilityRecord}.
     35  */
     36 @Presubmit
     37 public class AccessibilityRecordTest extends AndroidTestCase {
     38     /**
     39      * Tests the cloning obtain method.
     40      */
     41     @SmallTest
     42     public void testObtain() {
     43         AccessibilityRecord originalRecord = AccessibilityRecord.obtain();
     44         fullyPopulateAccessibilityRecord(originalRecord);
     45         AccessibilityRecord cloneRecord = AccessibilityRecord.obtain(originalRecord);
     46         assertEqualAccessibilityRecord(originalRecord, cloneRecord);
     47     }
     48 
     49    /**
     50     * Tests if {@link AccessibilityRecord}s are properly recycled.
     51     */
     52    @SmallTest
     53    public void testRecycle() {
     54        // obtain and populate an event
     55        AccessibilityRecord populatedRecord = AccessibilityRecord.obtain();
     56        fullyPopulateAccessibilityRecord(populatedRecord);
     57 
     58        // recycle and obtain the same recycled instance
     59        populatedRecord.recycle();
     60        AccessibilityRecord recycledRecord = AccessibilityRecord.obtain();
     61 
     62        // check expectations
     63        assertAccessibilityRecordCleared(recycledRecord);
     64    }
     65 
     66    /**
     67     * Asserts that an {@link AccessibilityRecord} is cleared.
     68     */
     69    static void assertAccessibilityRecordCleared(AccessibilityRecord record) {
     70        TestCase.assertEquals("addedCount not properly recycled", -1, record.getAddedCount());
     71        TestCase.assertNull("beforeText not properly recycled", record.getBeforeText());
     72        TestCase.assertFalse("checked not properly recycled", record.isChecked());
     73        TestCase.assertNull("className not properly recycled", record.getClassName());
     74        TestCase.assertNull("contentDescription not properly recycled",
     75                record.getContentDescription());
     76        TestCase.assertEquals("currentItemIndex not properly recycled", -1,
     77                record.getCurrentItemIndex());
     78        TestCase.assertFalse("enabled not properly recycled", record.isEnabled());
     79        TestCase.assertEquals("fromIndex not properly recycled", -1, record.getFromIndex());
     80        TestCase.assertFalse("fullScreen not properly recycled", record.isFullScreen());
     81        TestCase.assertEquals("itemCount not properly recycled", -1, record.getItemCount());
     82        TestCase.assertNull("parcelableData not properly recycled", record.getParcelableData());
     83        TestCase.assertFalse("password not properly recycled", record.isPassword());
     84        TestCase.assertEquals("removedCount not properly recycled", -1, record.getRemovedCount());
     85        TestCase.assertTrue("text not properly recycled", record.getText().isEmpty());
     86        TestCase.assertFalse("scrollable not properly recycled", record.isScrollable());
     87        TestCase.assertSame("maxScrollX not properly recycled", -1, record.getMaxScrollX());
     88        TestCase.assertSame("maxScrollY not properly recycled", -1, record.getMaxScrollY());
     89        TestCase.assertSame("scrollX not properly recycled", -1, record.getScrollX());
     90        TestCase.assertSame("scrollY not properly recycled", -1, record.getScrollY());
     91        TestCase.assertSame("toIndex not properly recycled", -1, record.getToIndex());
     92    }
     93 
     94     /**
     95      * Fully populates the {@link AccessibilityRecord}.
     96      *
     97      * @param record The record to populate.
     98      */
     99     static void fullyPopulateAccessibilityRecord(AccessibilityRecord record) {
    100         record.setAddedCount(1);
    101         record.setBeforeText("BeforeText");
    102         record.setChecked(true);
    103         record.setClassName("foo.bar.baz.Class");
    104         record.setContentDescription("ContentDescription");
    105         record.setCurrentItemIndex(1);
    106         record.setEnabled(true);
    107         record.setFromIndex(1);
    108         record.setFullScreen(true);
    109         record.setItemCount(1);
    110         record.setParcelableData(Message.obtain(null, 1, 2, 3));
    111         record.setPassword(true);
    112         record.setRemovedCount(1);
    113         record.getText().add("Foo");
    114         record.setMaxScrollX(1);
    115         record.setMaxScrollY(1);
    116         record.setScrollX(1);
    117         record.setScrollY(1);
    118         record.setToIndex(1);
    119         record.setScrollable(true);
    120     }
    121 
    122     static void assertEqualAccessibilityRecord(AccessibilityRecord expectedRecord,
    123             AccessibilityRecord receivedRecord) {
    124         assertEquals("addedCount has incorrect value", expectedRecord.getAddedCount(),
    125                 receivedRecord.getAddedCount());
    126         assertEquals("beforeText has incorrect value", expectedRecord.getBeforeText(),
    127                 receivedRecord.getBeforeText());
    128         assertEquals("checked has incorrect value", expectedRecord.isChecked(),
    129                 receivedRecord.isChecked());
    130         assertEquals("className has incorrect value", expectedRecord.getClassName(),
    131                 receivedRecord.getClassName());
    132         assertEquals("contentDescription has incorrect value",
    133                 expectedRecord.getContentDescription(), receivedRecord.getContentDescription());
    134         assertEquals("currentItemIndex has incorrect value", expectedRecord.getCurrentItemIndex(),
    135                 receivedRecord.getCurrentItemIndex());
    136         assertEquals("enabled has incorrect value", expectedRecord.isEnabled(),
    137                 receivedRecord.isEnabled());
    138         assertEquals("fromIndex has incorrect value", expectedRecord.getFromIndex(),
    139                 receivedRecord.getFromIndex());
    140         assertEquals("fullScreen has incorrect value", expectedRecord.isFullScreen(),
    141                 receivedRecord.isFullScreen());
    142         assertEquals("itemCount has incorrect value", expectedRecord.getItemCount(),
    143                 receivedRecord.getItemCount());
    144         assertEquals("password has incorrect value", expectedRecord.isPassword(),
    145                 receivedRecord.isPassword());
    146         assertEquals("removedCount has incorrect value", expectedRecord.getRemovedCount(),
    147                 receivedRecord.getRemovedCount());
    148         assertEqualsText(expectedRecord.getText(), receivedRecord.getText());
    149         assertSame("maxScrollX has incorect value", expectedRecord.getMaxScrollX(),
    150                 receivedRecord.getMaxScrollX());
    151         assertSame("maxScrollY has incorect value", expectedRecord.getMaxScrollY(),
    152                 receivedRecord.getMaxScrollY());
    153         assertSame("scrollX has incorect value", expectedRecord.getScrollX(),
    154                 receivedRecord.getScrollX());
    155         assertSame("scrollY has incorect value", expectedRecord.getScrollY(),
    156                 receivedRecord.getScrollY());
    157         assertSame("toIndex has incorect value", expectedRecord.getToIndex(),
    158                 receivedRecord.getToIndex());
    159         assertSame("scrollable has incorect value", expectedRecord.isScrollable(),
    160                 receivedRecord.isScrollable());
    161         assertSame("parcelableData has incorect value",
    162                 ((Message) expectedRecord.getParcelableData()).what,
    163                 ((Message) receivedRecord.getParcelableData()).what);
    164     }
    165 
    166     /**
    167      * Compares the text of the <code>expectedEvent</code> and
    168      * <code>receivedEvent</code> by comparing the string representation of the
    169      * corresponding {@link CharSequence}s.
    170      */
    171     static void assertEqualsText(List<CharSequence> expectedText,
    172             List<CharSequence> receivedText ) {
    173         String message = "text has incorrect value";
    174 
    175         TestCase.assertEquals(message, expectedText.size(), receivedText.size());
    176 
    177         Iterator<CharSequence> expectedTextIterator = expectedText.iterator();
    178         Iterator<CharSequence> receivedTextIterator = receivedText.iterator();
    179 
    180         for (int i = 0; i < expectedText.size(); i++) {
    181             // compare the string representation
    182             TestCase.assertEquals(message, expectedTextIterator.next().toString(),
    183                     receivedTextIterator.next().toString());
    184         }
    185     }
    186 }
    187