1 /* 2 * Copyright (C) 2017 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 package android.autofillservice.cts; 17 18 import static com.google.common.truth.Truth.assertWithMessage; 19 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.widget.Button; 23 import android.widget.EditText; 24 import android.widget.TimePicker; 25 26 import java.util.concurrent.CountDownLatch; 27 import java.util.concurrent.TimeUnit; 28 29 /** 30 * Base class for an activity that has the following fields: 31 * 32 * <ul> 33 * <li>A TimePicker (id: date_picker) 34 * <li>An EditText that is filled with the TimePicker when it changes (id: output) 35 * <li>An OK button that finishes it and navigates to the {@link WelcomeActivity} 36 * </ul> 37 * 38 * <p>It's abstract because the sub-class must provide the view id, so it can support multiple 39 * UI types (like clock and spinner). 40 */ 41 abstract class AbstractTimePickerActivity extends AbstractAutoFillActivity { 42 43 private static final long OK_TIMEOUT_MS = 1000; 44 45 static final String ID_TIME_PICKER = "time_picker"; 46 static final String ID_OUTPUT = "output"; 47 48 private TimePicker mTimePicker; 49 private EditText mOutput; 50 private Button mOk; 51 52 private FillExpectation mExpectation; 53 private CountDownLatch mOkLatch; 54 55 protected abstract int getContentView(); 56 57 @Override 58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 61 setContentView(getContentView()); 62 63 mTimePicker = (TimePicker) findViewById(R.id.time_picker); 64 65 mTimePicker.setOnTimeChangedListener((v, m, h) -> updateOutputWithTime(m, h)); 66 67 mOutput = (EditText) findViewById(R.id.output); 68 mOk = (Button) findViewById(R.id.ok); 69 mOk.setOnClickListener((v) -> ok()); 70 } 71 72 private void updateOutputWithTime(int hour, int minute) { 73 final String time = hour + ":" + minute; 74 mOutput.setText(time); 75 } 76 77 private void ok() { 78 final Intent intent = new Intent(this, WelcomeActivity.class); 79 intent.putExtra(WelcomeActivity.EXTRA_MESSAGE, "It's Adventure Time!"); 80 startActivity(intent); 81 if (mOkLatch != null) { 82 // Latch is not set when activity launched outside tests 83 mOkLatch.countDown(); 84 } 85 finish(); 86 } 87 88 /** 89 * Sets the expectation for an auto-fill request, so it can be asserted through 90 * {@link #assertAutoFilled()} later. 91 */ 92 void expectAutoFill(String output, int hour, int minute) { 93 mExpectation = new FillExpectation(output, hour, minute); 94 mOutput.addTextChangedListener(mExpectation.outputWatcher); 95 mTimePicker.setOnTimeChangedListener((v, h, m) -> { 96 updateOutputWithTime(h, m); 97 mExpectation.timeListener.onTimeChanged(v, h, m); 98 }); 99 } 100 101 /** 102 * Asserts the activity was auto-filled with the values passed to 103 * {@link #expectAutoFill(String, int, int)}. 104 */ 105 void assertAutoFilled() throws Exception { 106 assertWithMessage("expectAutoFill() not called").that(mExpectation).isNotNull(); 107 mExpectation.timeListener.assertAutoFilled(); 108 mExpectation.outputWatcher.assertAutoFilled(); 109 } 110 111 /** 112 * Visits the {@code output} in the UiThread. 113 */ 114 void onOutput(Visitor<EditText> v) { 115 syncRunOnUiThread(() -> v.visit(mOutput)); 116 } 117 118 /** 119 * Sets the time in the {@link TimePicker}. 120 */ 121 void setTime(int hour, int minute) { 122 syncRunOnUiThread(() -> { 123 mTimePicker.setHour(hour); 124 mTimePicker.setMinute(minute); 125 }); 126 } 127 128 /** 129 * Taps the ok button in the UI thread. 130 */ 131 void tapOk() throws Exception { 132 mOkLatch = new CountDownLatch(1); 133 syncRunOnUiThread(() -> mOk.performClick()); 134 boolean called = mOkLatch.await(OK_TIMEOUT_MS, TimeUnit.MILLISECONDS); 135 assertWithMessage("Timeout (%s ms) waiting for OK action", OK_TIMEOUT_MS) 136 .that(called).isTrue(); 137 } 138 139 /** 140 * Holder for the expected auto-fill values. 141 */ 142 private final class FillExpectation { 143 private final MultipleTimesTextWatcher outputWatcher; 144 private final MultipleTimesTimeListener timeListener; 145 146 private FillExpectation(String output, int hour, int minute) { 147 // Output is called twice: by the TimeChangeListener and by auto-fill. 148 outputWatcher = new MultipleTimesTextWatcher("output", 2, mOutput, output); 149 timeListener = new MultipleTimesTimeListener("timePicker", 1, mTimePicker, hour, 150 minute); 151 } 152 } 153 } 154