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 
17 package android.autofillservice.cts.testcore;
18 
19 import static android.autofillservice.cts.testcore.Timeouts.FILL_TIMEOUT;
20 
21 import static com.google.common.truth.Truth.assertWithMessage;
22 
23 import android.widget.TimePicker;
24 
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 
28 /**
29  * Custom {@OnDateChangedListener} used to assert a {@link TimePicker} was auto-filled properly.
30  */
31 public final class MultipleTimesTimeListener implements TimePicker.OnTimeChangedListener {
32     private final String name;
33     private final CountDownLatch latch;
34     private final TimePicker timePicker;
35     private final int expectedHour;
36     private final int expectedMinute;
37 
MultipleTimesTimeListener(String name, int times, TimePicker timePicker, int expectedHour, int expectedMinute)38     public MultipleTimesTimeListener(String name, int times, TimePicker timePicker,
39             int expectedHour, int expectedMinute) {
40         this.name = name;
41         this.timePicker = timePicker;
42         this.expectedHour = expectedHour;
43         this.expectedMinute = expectedMinute;
44         this.latch = new CountDownLatch(times);
45     }
46 
47     @Override
onTimeChanged(TimePicker view, int hour, int minute)48     public void onTimeChanged(TimePicker view, int hour, int minute) {
49         latch.countDown();
50     }
51 
assertAutoFilled()52     public void assertAutoFilled() throws Exception {
53         final boolean set = latch.await(FILL_TIMEOUT.ms(), TimeUnit.MILLISECONDS);
54         assertWithMessage("Timeout (%s ms) on TimePicker %s", FILL_TIMEOUT.ms(), name)
55                 .that(set).isTrue();
56         assertWithMessage("Wrong hour on TimePicker %s", name)
57                 .that(timePicker.getHour()).isEqualTo(expectedHour);
58         assertWithMessage("Wrong minute on TimePicker %s", name)
59                 .that(timePicker.getMinute()).isEqualTo(expectedMinute);
60     }
61 }
62