Fix InputConnectionBlockingMethodTest flakiness

This is a follow up CL to my prior CLs [1][2][3], which added
InputConnectionBlockingMethodTest.

Suppose we are doing something like this.

  TestActivity.startSync(activity -> {
      final LinearLayout layout = new LinearLayout(activity);
      layout.setOrientation(LinearLayout.VERTICAL);
      final EditText editText = new EditText(activity) {
          @Override
          public InputConnection onCreateInputConnection(
                  EditorInfo outAttrs) {
              final InputConnection ic =
                      super.onCreateInputConnection(outAttrs);
              return new InputConnectionWrapper(ic, false) {
                  @Override
                  public CharSequence getTextAfterCursor(
                          int n, int flags) {
                      assertEquals(expectedN, n);
                      assertEquals(expectedFlags, flags);
                      return expectedResult;
                  }
              }
          }
      };
      editText.setPrivateImeOptions(marker);
      editText.setHint("editText");
      editText.requestFocus();

      layout.addView(editText);
      activity.getWindow().setSoftInputMode(
              SOFT_INPUT_STATE_ALWAYS_VISIBLE);
      return layout;
  });

One may think the above logic is OK because with MockIme we can fully
control what InputConnection method will get called in a test
scenario, but the TestActivity might be working or re-launched even
after the MockImeSession is done, meaning that the system may allow
the default IME to interact with the TestActivity for a short period
while or after the test case is torn down, where "assertEquals" can
fail depending on how the default IME behaves.

The key idea of this CL is to return our instrumented InputConnection
from onCreateInputConnection() only while tests are actually running
throughout InputConnectionBlockingMethodTest, that is:

  TestActivity.startSync(activity -> {
      final LinearLayout layout = new LinearLayout(activity);
      layout.setOrientation(LinearLayout.VERTICAL);
      final EditText editText = new EditText(activity) {
          @Override
          public InputConnection onCreateInputConnection(
                  EditorInfo outAttrs) {
              final InputConnection ic =
                      super.onCreateInputConnection(outAttrs);
              // Fall back to the original InputConnection once the
              // test is done.
              if (!isTestRunning.get()) {
                  return ic;
              }
              return new InputConnectionWrapper(ic, false) {
                  @Override
                  public CharSequence getTextAfterCursor(
                          int n, int flags) {
                      assertEquals(expectedN, n);
                      assertEquals(expectedFlags, flags);
                      return expectedResult;
                  }
              }
          }
      };
      ....

This should be sufficient for us because the OS always re-creates a
new InputConnection upon IME switching. We need to care about only
InputConnection instances that are created while MockIme is in use.

Othar than that, there is no change in
InputConnectionBlockingMethodTest.

 [1]: Id61e4a7476cf99bea2db3d6488c1c6eb17e88782
      045653e0561ac0dab548c91674ff3e6bfe3cf818
 [2]: I7e0f8baf6c9662d486cd189d0731e65eeae38a15
      399cc0a253b5d7a6669d1c65f1b3d614f1cee1a7
 [3]: I43340a4172c83100d968612ccb0eced4479b81f8
      3d4e3b822e71aeeb856f4869b029069f154306ab

Bug: 36897707
Fix: 154990192
Test: atest CtsInputMethodTestCases:InputConnectionBlockingMethodTest
Change-Id: I8ae3b1b1c868dbd701d2ba7fae281aafc8a0041d
1 file changed