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; 18 19 import static com.google.common.truth.Truth.assertWithMessage; 20 21 import android.os.CancellationSignal; 22 23 import java.util.concurrent.CountDownLatch; 24 import java.util.concurrent.TimeUnit; 25 26 /** 27 * Custom {@link android.os.CancellationSignal.OnCancelListener} used to assert that 28 * {@link android.os.CancellationSignal.OnCancelListener} was called, and just once. 29 */ 30 public final class OneTimeCancellationSignalListener 31 implements CancellationSignal.OnCancelListener { 32 private final CountDownLatch mLatch = new CountDownLatch(1); 33 private final long mTimeoutMs; 34 OneTimeCancellationSignalListener(long timeoutMs)35 public OneTimeCancellationSignalListener(long timeoutMs) { 36 mTimeoutMs = timeoutMs; 37 } 38 assertOnCancelCalled()39 public void assertOnCancelCalled() throws Exception { 40 final boolean called = mLatch.await(mTimeoutMs, TimeUnit.MILLISECONDS); 41 assertWithMessage("Timeout (%s ms) waiting for onCancel()", mTimeoutMs) 42 .that(called).isTrue(); 43 } 44 45 @Override onCancel()46 public void onCancel() { 47 mLatch.countDown(); 48 } 49 } 50