1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 package org.chromium.chrome.test.util;
6 import android.os.SystemClock;
8 import org.chromium.chrome.browser.infobar.AnimationHelper;
9 import org.chromium.chrome.browser.infobar.InfoBarContainer;
11 import java.util.concurrent.TimeUnit;
14 * Allow tests to register for animation finished notifications.
16 public class InfoBarTestAnimationListener implements InfoBarContainer.InfoBarAnimationListener {
18 private static final long WAIT_MILLIS = TimeUnit.SECONDS.toMillis(5);
20 private static class ConditionalWait {
21 private volatile Boolean mCondition;
30 * Waits for {@code millis} or until the value of the condition becomes true
31 * if it does it resets it to false before returning so it can be reused.
33 * @return true if the condition becomes true before the specified {@code millis}.
35 public boolean waitAndExpire(long millis) throws InterruptedException {
36 synchronized (mLock) {
37 while (!mCondition && millis > 0) {
38 long start = SystemClock.elapsedRealtime();
40 millis -= (SystemClock.elapsedRealtime() - start);
42 boolean result = mCondition;
48 public void set(boolean value) {
49 synchronized (mLock) {
58 private ConditionalWait mAddAnimationFinished;
59 private ConditionalWait mSwapAnimationFinished;
60 private ConditionalWait mRemoveAnimationFinished;
63 public InfoBarTestAnimationListener() {
64 mAddAnimationFinished = new ConditionalWait();
65 mSwapAnimationFinished = new ConditionalWait();
66 mRemoveAnimationFinished = new ConditionalWait();
70 public void notifyAnimationFinished(int animationType) {
71 switch(animationType) {
72 case AnimationHelper.ANIMATION_TYPE_SHOW:
73 mAddAnimationFinished.set(true);
75 case AnimationHelper.ANIMATION_TYPE_SWAP:
76 mSwapAnimationFinished.set(true);
78 case AnimationHelper.ANIMATION_TYPE_HIDE:
79 mRemoveAnimationFinished.set(true);
82 throw new UnsupportedOperationException(
83 "Animation finished for unknown type " + animationType);
87 public boolean addInfoBarAnimationFinished() throws InterruptedException {
88 return mAddAnimationFinished.waitAndExpire(WAIT_MILLIS);
91 public boolean swapInfoBarAnimationFinished() throws InterruptedException {
92 return mSwapAnimationFinished.waitAndExpire(WAIT_MILLIS);
95 public boolean removeInfoBarAnimationFinished() throws InterruptedException {
96 return mRemoveAnimationFinished.waitAndExpire(WAIT_MILLIS);