15379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot/*
25379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * Copyright (C) 2012 The Android Open Source Project
35379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot *
45379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * Licensed under the Apache License, Version 2.0 (the "License");
55379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * you may not use this file except in compliance with the License.
65379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * You may obtain a copy of the License at
75379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot *
85379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot *      http://www.apache.org/licenses/LICENSE-2.0
95379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot *
105379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * Unless required by applicable law or agreed to in writing, software
115379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * distributed under the License is distributed on an "AS IS" BASIS,
125379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * See the License for the specific language governing permissions and
145379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * limitations under the License.
155379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot */
165379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabotpackage com.android.test.runner.listener;
175379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
185379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabotimport android.util.Log;
195379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
205379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabotimport org.junit.runner.Description;
215379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabotimport org.junit.runner.notification.RunListener;
225379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
235379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot/**
245379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot * A {@link RunListener} that injects a given delay between tests.
255379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot */
265379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabotpublic class DelayInjector extends RunListener {
275379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
285379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    private final int mDelayMsec;
295379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
305379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    /**
315379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot     * @param delayMsec
325379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot     */
335379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    public DelayInjector(int delayMsec) {
345379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        mDelayMsec = delayMsec;
355379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    }
365379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
375379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    @Override
385379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    public void testRunStarted(Description description) throws Exception {
395379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        // delay before first test
405379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        delay();
415379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    }
425379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
435379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    @Override
445379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    public void testFinished(Description description) throws Exception {
455379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        // delay after every test
465379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        delay();
475379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    }
485379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot
495379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    private void delay() {
505379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        try {
515379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot            Thread.sleep(mDelayMsec);
525379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        } catch (InterruptedException e) {
535379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot            Log.e("DelayInjector", "interrupted", e);
545379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot        }
555379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot    }
565379a16a2ba03e54c8aa72011d4aac61c87bf384Brett Chabot}
57