1 /* 2 * Copyright (C) 2016 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 com.android.server.telecom.tests; 18 19 import com.android.internal.util.StateMachine; 20 21 import java.util.List; 22 import java.util.concurrent.CountDownLatch; 23 24 public abstract class StateMachineTestBase<T extends StateMachine> extends TelecomTestCase { 25 abstract static class TestParameters {} 26 27 protected final void waitForStateMachineActionCompletion(T stateMachine, int runnableCode) { 28 final CountDownLatch lock = new CountDownLatch(1); 29 Runnable actionComplete = new Runnable() { 30 @Override 31 public void run() { 32 lock.countDown(); 33 } 34 }; 35 stateMachine.sendMessage(runnableCode, actionComplete); 36 while (lock.getCount() > 0) { 37 try { 38 lock.await(); 39 } catch (InterruptedException e) { 40 // do nothing 41 } 42 } 43 } 44 45 protected final void parametrizedTestStateMachine( 46 List<? extends TestParameters> paramList) throws Throwable { 47 for (TestParameters params : paramList) { 48 try { 49 runParametrizedTestCase(params); 50 } catch (Throwable e) { 51 String newMessage = "Failed at parameters: \n" + params.toString() + '\n' 52 + e.getMessage(); 53 Throwable t = new Throwable(newMessage, e); 54 t.setStackTrace(e.getStackTrace()); 55 throw t; 56 } 57 } 58 } 59 60 protected abstract void runParametrizedTestCase(TestParameters params) throws Throwable; 61 } 62