Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
20   99   11   2.86
8   51   0.55   7
7     1.57  
1    
 
  ComponentShownWaiter       Line # 35 20 0% 11 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Nov 21, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2008-2010 the original author or authors.
15    */
16    package org.fest.swing.driver;
17   
18    import static org.fest.swing.query.ComponentVisibleQuery.isVisible;
19    import static org.fest.swing.timing.Pause.pause;
20    import static org.fest.swing.util.TimeoutWatch.startWatchWithTimeoutOf;
21   
22    import java.awt.Component;
23    import java.awt.event.ComponentAdapter;
24    import java.awt.event.ComponentEvent;
25   
26    import org.fest.swing.annotation.RunsInEDT;
27    import org.fest.swing.exception.WaitTimedOutError;
28    import org.fest.swing.util.TimeoutWatch;
29   
30    /**
31    * Understands waiting for a <code>{@link Component}</code> to be shown.
32    *
33    * @author Alex Ruiz
34    */
 
35    public final class ComponentShownWaiter extends ComponentAdapter {
36   
37    private static final int DEFAULT_TIMEOUT = 5000;
38    private static final int DEFAULT_SLEEP_TIME = 10;
39   
40    private Component toWaitFor;
41    private volatile boolean shown;
42   
43    /**
44    * Waits until the given component is shown on the screen, using a timeout of 5 seconds.
45    * @param toWaitFor the component to wait for.
46    * @throws WaitTimedOutError if the component is not shown before the default timeout of 5 seconds.
47    */
 
48  1 toggle public static void waitTillShown(Component toWaitFor) {
49  1 new ComponentShownWaiter(toWaitFor).startWaiting(DEFAULT_TIMEOUT);
50    }
51   
52    /**
53    * Waits until the given component is shown on the screen.
54    * @param toWaitFor the component to wait for.
55    * @param timeout the amount to time (in milliseconds) to wait for the component to be shown.
56    * @throws WaitTimedOutError if the component is not shown before the given timeout expires.
57    */
 
58  3 toggle public static void waitTillShown(Component toWaitFor, long timeout) {
59  3 new ComponentShownWaiter(toWaitFor).startWaiting(timeout);
60    }
61   
 
62  4 toggle private ComponentShownWaiter(Component toWaitFor) {
63  4 this.toWaitFor = toWaitFor;
64  4 toWaitFor.addComponentListener(this);
65    }
66   
 
67  4 toggle private void startWaiting(long timeout) {
68  1 if (alreadyVisible()) return;
69  3 TimeoutWatch watch = startWatchWithTimeoutOf(timeout);
70  615 while (!shown) {
71  614 pause(DEFAULT_SLEEP_TIME);
72  614 if (watch.isTimeOut()) {
73  2 done();
74  2 throw new WaitTimedOutError("Timed out waiting for component to be visible");
75    }
76    }
77    }
78   
 
79  4 toggle private boolean alreadyVisible() {
80  3 if (!isVisible(toWaitFor)) return false;
81  1 done();
82  1 return true;
83    }
84   
85    /**
86    * Notification that the component to wait for is finally shown on the screen.
87    * @param e the event raised when the component has been made visible.
88    */
 
89  1 toggle @RunsInEDT
90    @Override public void componentShown(ComponentEvent e) {
91  1 shown = true;
92  1 done();
93    }
94   
 
95  4 toggle private void done() {
96  4 toWaitFor.removeComponentListener(this);
97  4 toWaitFor = null;
98    }
99    }