1 /* 2 * Copyright 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 androidx.test.uiautomator.testapp; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.os.Handler; 22 import android.widget.TextView; 23 24 import org.jspecify.annotations.Nullable; 25 26 public class WaitTestActivity extends Activity { 27 @Override onCreate(@ullable Bundle savedInstanceState)28 public void onCreate(@Nullable Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 31 setContentView(R.layout.wait_test_activity); 32 33 TextView text1 = findViewById(R.id.text_1); 34 35 text1.setOnClickListener(view -> { 36 // Unlike in `UiObject2`, the {@link UiObject#click()} will block the process until 37 // either accessibility event `TYPE_WINDOW_CONTENT_CHANGED` or `TYPE_VIEW_SELECTED` 38 // is received (as in {@link InteractionController#clickAndSync(final int x, final 39 // int y, long timeout)}). 40 // So we have to change the text right after the click to initialize a 41 // `TYPE_WINDOW_CONTENT_CHANGED` event. 42 // Otherwise, the click action will only block everything until the delayed text change 43 // (as the `postDelayed()` method doing), which invalidates this testing mechanism. 44 ((TextView) view).setText("text_1_clicked"); 45 new Handler().postDelayed(() -> ((TextView) view).setText("text_1_changed"), 46 3_000); 47 }); 48 } 49 } 50