OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 | |
5 package org.chromium.content.browser.test; | |
6 | |
7 import org.chromium.content.browser.ContentView; | |
8 | |
9 import java.lang.reflect.Field; | |
10 | |
11 | |
12 /** | |
13 * Helper methods for creating and managing criteria. | |
14 */ | |
15 public class CriteriaHelper { | |
16 | |
17 /** The default maximum time to wait for a criteria to become valid. */ | |
18 public static final long DEFAULT_MAX_TIME_TO_POLL = 3000; | |
19 /** The default polling interval to wait between checking for a satisfied cr iteria. */ | |
20 public static final long DEFAULT_POLLING_INTERVAL = 50; | |
21 | |
22 /** | |
Yaron
2012/09/05 06:10:43
indentation looks off
Ted C
2012/09/05 18:45:35
Done.
| |
23 * Checks whether the given Criteria is satisfied at a given interval, until either | |
24 * the criteria is satisfied, or the specified maxTimeoutMs number of ms has elapsed. | |
25 * @param criteria The Criteria that will be checked. | |
26 * @param maxTimeoutMs The maximum number of ms that this check will be perf ormed for | |
27 * before timeout. | |
28 * @param checkIntervalMs The number of ms between checks. | |
29 * @return true iff checking has ended with the criteria being satisfied. | |
30 * @throws InterruptedException | |
31 */ | |
32 public static boolean pollForCriteria(Criteria criteria, long maxTimeoutMs, | |
33 long checkIntervalMs) throws InterruptedException { | |
34 boolean isSatisfied = criteria.isSatisfied(); | |
35 long startTime = System.currentTimeMillis(); | |
36 while (!isSatisfied && System.currentTimeMillis() - startTime < maxTimeo utMs) { | |
37 Thread.sleep(checkIntervalMs); | |
38 isSatisfied = criteria.isSatisfied(); | |
39 } | |
40 return isSatisfied; | |
41 } | |
42 | |
43 /** | |
44 * Checks whether the given Criteria is satisfied polling at a default inter val. | |
45 * | |
46 * @param criteria The Criteria that will be checked. | |
47 * @return iff checking has ended with the criteria being satisfied. | |
48 * @throws InterruptedException | |
49 * @see #pollForCriteria(Criteria, long, long) | |
50 */ | |
51 public static boolean pollForCriteria(Criteria criteria) throws InterruptedE xception { | |
52 return pollForCriteria(criteria, DEFAULT_MAX_TIME_TO_POLL, DEFAULT_POLLI NG_INTERVAL); | |
53 } | |
54 | |
55 /** | |
56 * Performs the runnable action, then checks whether the given criteria are satisfied | |
57 * until the specified timeout, using the pollForCriteria method. If not, th en the runnable | |
58 * action is performed again, to a maximum of maxAttempts tries. | |
59 */ | |
60 public static boolean runUntilCriteria(Runnable runnable, Criteria criteria, | |
61 int maxAttempts, long maxTimeoutMs, long checkIntervalMs) throws Int erruptedException { | |
62 int count = 0; | |
63 boolean success = false; | |
64 while (count < maxAttempts && !success) { | |
65 count++; | |
66 runnable.run(); | |
67 success = pollForCriteria(criteria, maxTimeoutMs, checkIntervalMs); | |
68 } | |
69 return success; | |
70 } | |
71 | |
72 public static class NotZoomingCriteria implements Criteria { | |
Yaron
2012/09/05 06:10:43
How re-used is this? Seems like it doesn't really
Ted C
2012/09/05 18:45:35
Removed as it isn't used yet.
| |
73 private ContentView mContentView; | |
74 private float mCurrentMagnifyScale; | |
75 | |
76 public NotZoomingCriteria(ContentView chromeView) { | |
77 mContentView = chromeView; | |
78 } | |
79 | |
80 @Override | |
81 public boolean isSatisfied() { | |
82 float lastMagnifyScale = mCurrentMagnifyScale; | |
83 try { | |
84 mCurrentMagnifyScale = getMagnifyScale(); | |
85 } catch (Exception e) { | |
86 return false; | |
87 } | |
88 return lastMagnifyScale != 0 && lastMagnifyScale == mCurrentMagnifyS cale; | |
89 } | |
90 | |
91 private float getMagnifyScale() throws NoSuchFieldException, | |
92 IllegalArgumentException, IllegalAccessException { | |
93 Field field; | |
94 field = ContentView.class.getDeclaredField("mMagnifyScale"); | |
95 field.setAccessible(true); | |
96 return field.getFloat(mContentView); | |
97 } | |
98 } | |
99 } | |
OLD | NEW |