1 /*
2  * Copyright (C) 2022 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 com.android.apps.inputmethod.simpleime.ims;
18 
19 import android.content.res.Configuration;
20 import android.inputmethodservice.InputMethodService;
21 import android.util.Log;
22 import android.view.inputmethod.EditorInfo;
23 
24 import java.util.concurrent.CountDownLatch;
25 
26 /** Wrapper of {@link InputMethodService} to expose interfaces for testing purpose. */
27 public class InputMethodServiceWrapper extends InputMethodService {
28     private static final String TAG = "InputMethodServiceWrapper";
29 
30     private static InputMethodServiceWrapper sInputMethodServiceWrapper;
31 
getInputMethodServiceWrapperForTesting()32     public static InputMethodServiceWrapper getInputMethodServiceWrapperForTesting() {
33         return sInputMethodServiceWrapper;
34     }
35 
36     private boolean mInputViewStarted;
37     private CountDownLatch mCountDownLatchForTesting;
38 
getCurrentInputViewStarted()39     public boolean getCurrentInputViewStarted() {
40         return mInputViewStarted;
41     }
42 
setCountDownLatchForTesting(CountDownLatch countDownLatchForTesting)43     public void setCountDownLatchForTesting(CountDownLatch countDownLatchForTesting) {
44         mCountDownLatchForTesting = countDownLatchForTesting;
45     }
46 
47     @Override
onCreate()48     public void onCreate() {
49         Log.i(TAG, "onCreate()");
50         super.onCreate();
51         sInputMethodServiceWrapper = this;
52     }
53 
54     @Override
onStartInput(EditorInfo info, boolean restarting)55     public void onStartInput(EditorInfo info, boolean restarting) {
56         Log.i(TAG, "onStartInput() editor=" + info + ", restarting=" + restarting);
57         super.onStartInput(info, restarting);
58     }
59 
60     @Override
onStartInputView(EditorInfo info, boolean restarting)61     public void onStartInputView(EditorInfo info, boolean restarting) {
62         Log.i(TAG, "onStartInputView() editor=" + info + ", restarting=" + restarting);
63         super.onStartInputView(info, restarting);
64         mInputViewStarted = true;
65         if (mCountDownLatchForTesting != null) {
66             mCountDownLatchForTesting.countDown();
67         }
68     }
69 
70     @Override
onFinishInput()71     public void onFinishInput() {
72         Log.i(TAG, "onFinishInput()");
73         super.onFinishInput();
74     }
75 
76     @Override
onFinishInputView(boolean finishingInput)77     public void onFinishInputView(boolean finishingInput) {
78         Log.i(TAG, "onFinishInputView()");
79         super.onFinishInputView(finishingInput);
80         mInputViewStarted = false;
81 
82         if (mCountDownLatchForTesting != null) {
83             mCountDownLatchForTesting.countDown();
84         }
85     }
86 
87     @Override
requestHideSelf(int flags)88     public void requestHideSelf(int flags) {
89         Log.i(TAG, "requestHideSelf() " + flags);
90         super.requestHideSelf(flags);
91     }
92 
93     @Override
onConfigurationChanged(Configuration newConfig)94     public void onConfigurationChanged(Configuration newConfig) {
95         Log.i(TAG, "onConfigurationChanged() " + newConfig);
96         super.onConfigurationChanged(newConfig);
97 
98         if (mCountDownLatchForTesting != null) {
99             mCountDownLatchForTesting.countDown();
100         }
101     }
102 }
103