1 /* 2 * Copyright (C) 2009 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.inputmethod.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 22 import android.os.Parcel; 23 import android.platform.test.annotations.AppModeSdkSandbox; 24 import android.view.inputmethod.ExtractedTextRequest; 25 import android.view.inputmethod.InputConnection; 26 27 import androidx.annotation.AnyThread; 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.test.filters.SmallTest; 31 import androidx.test.runner.AndroidJUnit4; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @SmallTest 37 @RunWith(AndroidJUnit4.class) 38 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 39 public class ExtractedTextRequestTest { 40 41 private static final int EXPECTED_FLAGS = InputConnection.GET_TEXT_WITH_STYLES; 42 private static final int EXPECTED_HINT_MAX_CHARS = 100; 43 private static final int EXPECTED_HINT_MAX_LINES = 10; 44 private static final int EXPECTED_TOKEN = 2; 45 46 /** 47 * @return An instance of {@link ExtractedTextRequest} for test. 48 */ 49 @AnyThread 50 @NonNull createForTest()51 static ExtractedTextRequest createForTest() { 52 final ExtractedTextRequest request = new ExtractedTextRequest(); 53 request.flags = EXPECTED_FLAGS; 54 request.hintMaxChars = EXPECTED_HINT_MAX_CHARS; 55 request.hintMaxLines = EXPECTED_HINT_MAX_LINES; 56 request.token = EXPECTED_TOKEN; 57 return request; 58 } 59 60 /** 61 * Ensures that the specified object is equivalent to the one returned from 62 * {@link #createForTest()}. 63 * 64 * @param request {@link ExtractedTextRequest} to be tested. 65 */ assertTestInstance(@ullable ExtractedTextRequest request)66 static void assertTestInstance(@Nullable ExtractedTextRequest request) { 67 assertNotNull(request); 68 69 assertEquals(EXPECTED_FLAGS, request.flags); 70 assertEquals(EXPECTED_HINT_MAX_CHARS, request.hintMaxChars); 71 assertEquals(EXPECTED_HINT_MAX_LINES, request.hintMaxLines); 72 assertEquals(EXPECTED_TOKEN, request.token); 73 assertEquals(0, request.describeContents()); 74 } 75 76 /** 77 * Ensures that {@link ExtractedTextRequest} can be serialized and deserialized via 78 * {@link Parcel}. 79 */ 80 @Test testWriteToParcel()81 public void testWriteToParcel() { 82 final ExtractedTextRequest original = createForTest(); 83 assertTestInstance(original); 84 final ExtractedTextRequest copied = cloneViaParcel(original); 85 assertTestInstance(copied); 86 } 87 88 @NonNull cloneViaParcel(@onNull ExtractedTextRequest src)89 private static ExtractedTextRequest cloneViaParcel(@NonNull ExtractedTextRequest src) { 90 final Parcel parcel = Parcel.obtain(); 91 try { 92 src.writeToParcel(parcel, 0); 93 parcel.setDataPosition(0); 94 return ExtractedTextRequest.CREATOR.createFromParcel(parcel); 95 } finally { 96 parcel.recycle(); 97 } 98 } 99 } 100