1 /* 2 * Copyright (C) 2019 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 com.android.game.qualification.tests; 17 18 import static com.google.common.truth.Truth.assertWithMessage; 19 20 import android.app.Activity; 21 22 import java.util.concurrent.CountDownLatch; 23 import java.util.concurrent.TimeUnit; 24 25 public class TestActivity extends Activity { 26 27 private CountDownLatch latch = new CountDownLatch(1); 28 29 @Override onDestroy()30 protected void onDestroy() { 31 super.onDestroy(); 32 latch.countDown(); 33 } 34 35 /** 36 * Wait until onDestroy is called or 10 seconds has elapsed. 37 * 38 * AssertionError is thrown if wait time exceeded timeout. 39 */ waitUntilDestroyed()40 public void waitUntilDestroyed() throws InterruptedException { 41 waitUntilDestroyed(10000); 42 } 43 44 /** 45 * Wait until onDestroy is called or the specified timeoutMs has elapsed. 46 * 47 * AssertionError is thrown if wait time exceeded timeout. 48 */ waitUntilDestroyed(int timeoutMs)49 public void waitUntilDestroyed(int timeoutMs) throws InterruptedException { 50 boolean timeout = latch.await(timeoutMs, TimeUnit.MILLISECONDS); 51 assertWithMessage("Timeout waiting for the activity to be destroyed").that(timeout).isTrue(); 52 } 53 54 static { 55 System.loadLibrary("gamecore_java_tests_jni"); 56 } 57 58 } 59