Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 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.assertFalse;
     20 import static org.junit.Assert.assertNotNull;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.inputmethodservice.Keyboard;
     24 import android.inputmethodservice.Keyboard.Key;
     25 
     26 import androidx.test.InstrumentationRegistry;
     27 import androidx.test.filters.SmallTest;
     28 import androidx.test.runner.AndroidJUnit4;
     29 
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 
     33 @SmallTest
     34 @RunWith(AndroidJUnit4.class)
     35 public class KeyboardTest {
     36     @Test
     37     public void testKeyOnPressedAndReleased() {
     38         Key nonStickyKey = null;
     39         Key stickyKey = null;
     40         // Indirectly instantiate Keyboard.Key with XML resources.
     41         final Keyboard keyboard =
     42                 new Keyboard(InstrumentationRegistry.getTargetContext(), R.xml.keyboard);
     43         for (final Key key : keyboard.getKeys()) {
     44             if (!key.sticky) {
     45                 nonStickyKey = key;
     46                 break;
     47             }
     48         }
     49         for (final Key key : keyboard.getModifierKeys()) {
     50             if (key.sticky) {
     51                 stickyKey = key;
     52                 break;
     53             }
     54         }
     55 
     56         // Asserting existences of following keys is not the goal of this test, but this should work
     57         // anyway.
     58         assertNotNull(nonStickyKey);
     59         assertNotNull(stickyKey);
     60 
     61         // At first, both "pressed" and "on" must be false.
     62         assertFalse(nonStickyKey.pressed);
     63         assertFalse(stickyKey.pressed);
     64         assertFalse(nonStickyKey.on);
     65         assertFalse(stickyKey.on);
     66 
     67         // Pressing the key must flip the "pressed" state only.
     68         nonStickyKey.onPressed();
     69         stickyKey.onPressed();
     70         assertTrue(nonStickyKey.pressed);
     71         assertTrue(stickyKey.pressed);
     72         assertFalse(nonStickyKey.on);
     73         assertFalse(stickyKey.on);
     74 
     75         // Releasing the key inside the key area must flip the "pressed" state and toggle the "on"
     76         // state if the key is marked as sticky.
     77         nonStickyKey.onReleased(true /* inside */);
     78         stickyKey.onReleased(true /* inside */);
     79         assertFalse(nonStickyKey.pressed);
     80         assertFalse(stickyKey.pressed);
     81         assertFalse(nonStickyKey.on);
     82         assertTrue(stickyKey.on);   // The key state is toggled.
     83 
     84         // Pressing the key again must flip the "pressed" state only.
     85         nonStickyKey.onPressed();
     86         stickyKey.onPressed();
     87         assertTrue(nonStickyKey.pressed);
     88         assertTrue(stickyKey.pressed);
     89         assertFalse(nonStickyKey.on);
     90         assertTrue(stickyKey.on);
     91 
     92         // Releasing the key inside the key area must flip the "pressed" state and toggle the "on"
     93         // state if the key is marked as sticky hence we will be back to the initial state.
     94         nonStickyKey.onReleased(true /* inside */);
     95         stickyKey.onReleased(true /* inside */);
     96         assertFalse(nonStickyKey.pressed);
     97         assertFalse(stickyKey.pressed);
     98         assertFalse(nonStickyKey.on);
     99         assertFalse(stickyKey.on);
    100 
    101         // Pressing then releasing the key outside the key area must not affect the "on" state.
    102         nonStickyKey.onPressed();
    103         stickyKey.onPressed();
    104         nonStickyKey.onReleased(false /* inside */);
    105         stickyKey.onReleased(false /* inside */);
    106         assertFalse(nonStickyKey.pressed);
    107         assertFalse(stickyKey.pressed);
    108         assertFalse(nonStickyKey.on);
    109         assertFalse(stickyKey.on);
    110     }
    111 }
    112