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
43   143   22   3.07
10   95   0.51   14
14     1.57  
1    
 
  WindowStatus       Line # 38 43 0% 22 3 95.5% 0.95522386
 
No Tests
 
1    /*
2    * Created on Oct 17, 2007
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 @2007-2010 the original author or authors.
15    */
16    package org.fest.swing.monitor;
17   
18    import static java.lang.Math.max;
19    import static java.util.logging.Level.WARNING;
20    import static org.fest.swing.edt.GuiActionRunner.execute;
21    import static org.fest.swing.query.ComponentSizeQuery.sizeOf;
22   
23    import java.awt.*;
24    import java.util.logging.Logger;
25   
26    import org.fest.swing.annotation.RunsInCurrentThread;
27    import org.fest.swing.annotation.RunsInEDT;
28    import org.fest.swing.edt.GuiQuery;
29    import org.fest.swing.edt.GuiTask;
30    import org.fest.swing.util.Pair;
31    import org.fest.swing.util.RobotFactory;
32   
33    /**
34    * Understands verification of the state of a window.
35    *
36    * @author Alex Ruiz
37    */
 
38    class WindowStatus {
39   
40    private static final Logger LOGGER = Logger.getAnonymousLogger();
41   
42    private static final int ARBITRARY_EXTRA_VALUE = 20;
43   
44    private static int sign = 1;
45   
46    private final Windows windows;
47   
48    final Robot robot;
49   
 
50  625 toggle WindowStatus(Windows windows) {
51  625 this(windows, new RobotFactory());
52    }
53   
 
54  628 toggle WindowStatus(Windows windows, RobotFactory robotFactory) {
55  628 this.windows = windows;
56  628 Robot r = null;
57  628 try {
58  628 r = robotFactory.newRobotInPrimaryScreen();
59    } catch (AWTException ignored) {
60  1 LOGGER.log(WARNING, "Error ocurred when creating a new Robot", ignored);
61    }
62  628 robot = r;
63    }
64   
 
65  621 toggle Windows windows() { return windows; }
66   
67    /**
68    * Checks whether the given window is ready for input.
69    * @param w the given window.
70    */
 
71  2692 toggle @RunsInEDT
72    void checkIfReady(Window w) {
73  2 if (robot == null) return;
74  2690 try {
75  2690 checkSafelyIfReady(w);
76    } catch (Exception ignored) {
77    // We are getting InterruptedException in Xwnc
78    // http://groups.google.com/group/easytesting/browse_frm/thread/116cc070ab7b22e6
79  0 LOGGER.log(WARNING, "Error ocurred when checking if window is ready", ignored);
80    }
81    }
82   
 
83  2690 toggle private void checkSafelyIfReady(final Window w) {
84    // Must avoid frame borders, which are insensitive to mouse motion (at least on w32).
85  2690 Pair<WindowMetrics, Point> metricsAndCenter = metricsAndCenter(w);
86  2690 final WindowMetrics metrics = metricsAndCenter.i;
87  2690 mouseMove(w, metricsAndCenter.ii);
88  2375 if (!windows.isShowingButNotReady(w)) return;
89  315 execute(new GuiTask() {
 
90  315 toggle protected void executeInEDT() {
91  315 makeLargeEnoughToReceiveEvents(w, metrics);
92    }
93    });
94    }
95   
 
96  2690 toggle @RunsInEDT
97    private static Pair<WindowMetrics, Point> metricsAndCenter(final Window w) {
98  2690 return execute(new GuiQuery<Pair<WindowMetrics, Point>>() {
 
99  2690 toggle protected Pair<WindowMetrics, Point> executeInEDT() {
100  2690 WindowMetrics metrics = new WindowMetrics(w);
101  2690 return new Pair<WindowMetrics, Point>(metrics, metrics.center());
102    }
103    });
104    }
105   
 
106  2690 toggle @RunsInEDT
107    private void mouseMove(Window w, Point point) {
108  2690 int x = point.x;
109  2690 int y = point.y;
110  2690 if (x == 0 || y == 0) return;
111  2690 robot.mouseMove(x, y);
112  2690 Dimension windowSize = sizeOf(w);
113  2528 if (windowSize.width > windowSize.height) robot.mouseMove(x + sign, y);
114  162 else robot.mouseMove(x, y + sign);
115  2690 sign = -sign;
116    }
117   
 
118  315 toggle @RunsInCurrentThread
119    private void makeLargeEnoughToReceiveEvents(Window window, WindowMetrics metrics) {
120  312 if (!isEmptyFrame(window)) return;
121  3 int w = max(window.getWidth(), proposedWidth(metrics));
122  3 int h = max(window.getHeight(), proposedHeight(metrics));
123  3 window.setSize(new Dimension(w, h));
124    }
125   
 
126  315 toggle @RunsInCurrentThread
127    private boolean isEmptyFrame(Window w) {
128  315 Insets insets = w.getInsets();
129  315 return insets.top + insets.bottom == w.getHeight() || insets.left + insets.right == w.getWidth();
130    }
131   
 
132  3 toggle @RunsInCurrentThread
133    private int proposedWidth(WindowMetrics metrics) {
134  3 return metrics.leftAndRightInsets() + ARBITRARY_EXTRA_VALUE;
135    }
136   
 
137  3 toggle @RunsInCurrentThread
138    private int proposedHeight(WindowMetrics metrics) {
139  3 return metrics.topAndBottomInsets() + ARBITRARY_EXTRA_VALUE;
140    }
141   
 
142  2 toggle static int sign() { return sign; }
143    }