- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / android / javatests / src / org / chromium / chrome / test / util / InfoBarTestAnimationListener.java
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;
5
6 import android.os.SystemClock;
7
8 import java.util.concurrent.TimeUnit;
9
10 import org.chromium.chrome.browser.infobar.AnimationHelper;
11 import org.chromium.chrome.browser.infobar.InfoBarContainer;
12
13 /**
14  * Allow tests to register for animation finished notifications.
15  */
16 public class InfoBarTestAnimationListener implements InfoBarContainer.InfoBarAnimationListener {
17
18     private static long WAIT_MILLIS = TimeUnit.SECONDS.toMillis(5);
19
20     private static class ConditionalWait {
21         private volatile Boolean mCondition;
22         private Object mLock;
23
24         ConditionalWait() {
25             mCondition = false;
26             mLock = new Object();
27         }
28
29         /**
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.
32          *
33          * @return true if the condition becomes true before the specified {@code millis}.
34          */
35         public boolean waitAndExpire(long millis) throws InterruptedException {
36             synchronized(mLock) {
37                 while (!mCondition && millis > 0) {
38                     long start = SystemClock.elapsedRealtime();
39                     mLock.wait(millis);
40                     millis -= (SystemClock.elapsedRealtime() - start);
41                 }
42                 boolean result = mCondition;
43                 mCondition = false;
44                 return result;
45             }
46         }
47
48         public void set(boolean value) {
49             synchronized(mLock) {
50                 mCondition = value;
51                 if (value) {
52                     mLock.notify();
53                 }
54             }
55         }
56      }
57
58     private ConditionalWait mAddAnimationFinished;
59     private ConditionalWait mSwapAnimationFinished;
60     private ConditionalWait mRemoveAnimationFinished;
61
62
63     public InfoBarTestAnimationListener() {
64         mAddAnimationFinished = new ConditionalWait();
65         mSwapAnimationFinished = new ConditionalWait();
66         mRemoveAnimationFinished = new ConditionalWait();
67     }
68
69     @Override
70     public void notifyAnimationFinished(int animationType) {
71         switch(animationType) {
72             case AnimationHelper.ANIMATION_TYPE_SHOW:
73                 mAddAnimationFinished.set(true);
74                 break;
75             case AnimationHelper.ANIMATION_TYPE_SWAP:
76                 mSwapAnimationFinished.set(true);
77                 break;
78             case AnimationHelper.ANIMATION_TYPE_HIDE:
79                 mRemoveAnimationFinished.set(true);
80                 break;
81             default:
82                throw new UnsupportedOperationException(
83                        "Animation finished for unknown type " + animationType);
84         }
85     }
86
87     public boolean addInfoBarAnimationFinished() throws InterruptedException {
88         return mAddAnimationFinished.waitAndExpire(WAIT_MILLIS);
89     }
90
91     public boolean swapInfoBarAnimationFinished() throws InterruptedException {
92         return mSwapAnimationFinished.waitAndExpire(WAIT_MILLIS);
93     }
94
95     public boolean removeInfoBarAnimationFinished() throws InterruptedException {
96         return mRemoveAnimationFinished.waitAndExpire(WAIT_MILLIS);
97     }
98 }