1 /* 2 * Copyright (C) 2008 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 import static org.junit.Assert.assertSame; 22 import static org.mockito.Mockito.mock; 23 24 import android.os.Binder; 25 import android.os.Parcel; 26 import android.platform.test.annotations.AppModeSdkSandbox; 27 import android.platform.test.annotations.DisabledOnRavenwood; 28 import android.platform.test.ravenwood.RavenwoodRule; 29 import android.view.inputmethod.InputBinding; 30 import android.view.inputmethod.InputConnection; 31 32 import androidx.test.filters.SmallTest; 33 import androidx.test.runner.AndroidJUnit4; 34 35 import org.junit.Rule; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 39 // This is a bivalent test, it is expected to run on both device and host (Ravenwood) sides. 40 @SmallTest 41 @RunWith(AndroidJUnit4.class) 42 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 43 public final class InputBindingTest { 44 45 private static final int ANY_UID = 1; 46 private static final int ANY_PID = 2; 47 48 @Rule 49 public final RavenwoodRule mRavenwood = new RavenwoodRule(); 50 51 @Test 52 @DisabledOnRavenwood(blockedBy = {Parcel.class}) testInputBinding()53 public void testInputBinding() { 54 InputConnection conn = mock(InputConnection.class); 55 Binder binder = new Binder(); 56 InputBinding inputBinding = new InputBinding(conn, binder, ANY_UID, ANY_PID); 57 assertSame(conn, inputBinding.getConnection()); 58 assertSame(binder, inputBinding.getConnectionToken()); 59 assertEquals(ANY_UID, inputBinding.getUid()); 60 assertEquals(ANY_PID, inputBinding.getPid()); 61 62 assertNotNull(inputBinding.toString()); 63 assertEquals(0, inputBinding.describeContents()); 64 65 Parcel p = Parcel.obtain(); 66 inputBinding.writeToParcel(p, 0); 67 p.setDataPosition(0); 68 InputBinding target = InputBinding.CREATOR.createFromParcel(p); 69 assertEquals(ANY_UID, target.getUid()); 70 assertEquals(ANY_PID, target.getPid()); 71 assertSame(binder, target.getConnectionToken()); 72 73 p.recycle(); 74 } 75 76 @Test testInputBindingConstructor()77 public void testInputBindingConstructor() { 78 InputConnection conn = mock(InputConnection.class); 79 Binder connectionToken = mock(Binder.class); 80 81 InputBinding binding = new InputBinding(conn, connectionToken, ANY_UID, ANY_PID); 82 83 assertEquals(ANY_UID, binding.getUid()); 84 assertEquals(ANY_PID, binding.getPid()); 85 assertSame(conn, binding.getConnection()); 86 assertSame(connectionToken, binding.getConnectionToken()); 87 } 88 89 @Test testInputBindingCopyConstructor()90 public void testInputBindingCopyConstructor() { 91 InputConnection conn = mock(InputConnection.class); 92 Binder connectionToken = mock(Binder.class); 93 InputBinding source = new InputBinding(conn, connectionToken, ANY_UID, ANY_PID); 94 95 InputBinding copy = new InputBinding(conn, source); 96 assertEquals(ANY_UID, copy.getUid()); 97 assertEquals(ANY_PID, copy.getPid()); 98 assertSame(conn, copy.getConnection()); 99 assertSame(connectionToken, copy.getConnectionToken()); 100 } 101 } 102