001 /*
002 * Created on Jan 27, 2008
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2008-2010 the original author or authors.
014 */
015 package org.fest.swing.driver;
016
017 import static java.awt.Frame.*;
018 import static org.fest.swing.driver.ComponentStateValidator.validateIsEnabledAndShowing;
019 import static org.fest.swing.driver.WindowLikeContainerLocations.iconifyLocationOf;
020 import static org.fest.swing.driver.WindowLikeContainerLocations.maximizeLocationOf;
021 import static org.fest.swing.edt.GuiActionRunner.execute;
022 import static org.fest.swing.exception.ActionFailedException.actionFailure;
023
024 import java.awt.*;
025
026 import org.fest.swing.annotation.RunsInEDT;
027 import org.fest.swing.annotation.ThreadSafeAction;
028 import org.fest.swing.core.Robot;
029 import org.fest.swing.edt.GuiQuery;
030 import org.fest.swing.exception.ActionFailedException;
031
032 /**
033 * Understands functional testing of <code>{@link Frame}</code>s:
034 * <ul>
035 * <li>user input simulation</li>
036 * <li>state verification</li>
037 * <li>property value query</li>
038 * </ul>
039 * This class is intended for internal use only. Please use the classes in the package
040 * <code>{@link org.fest.swing.fixture}</code> in your tests.
041 *
042 * @author Alex Ruiz
043 * @author Yvonne Wang
044 */
045 public class FrameDriver extends WindowDriver {
046
047 /**
048 * Creates a new </code>{@link FrameDriver}</code>.
049 * @param robot the robot to use to simulate user input.
050 */
051 public FrameDriver(Robot robot) {
052 super(robot);
053 }
054
055 /**
056 * Iconifies the given <code>{@link Frame}</code>.
057 * @param frame the given <code>Frame</code>.
058 * @throws IllegalStateException if the <code>Frame</code> is not enabled.
059 * @throws IllegalStateException if the <code>Frame</code> is not showing on the screen.
060 */
061 @RunsInEDT
062 public void iconify(Frame frame) {
063 moveMouseIgnoringAnyError(frame, iconifyInfo(frame));
064 robot.waitForIdle();
065 updateFrameExtendedState(frame, ICONIFIED);
066 }
067
068 @RunsInEDT
069 private static Point iconifyInfo(final Frame frame) {
070 return execute(new GuiQuery<Point>() {
071 protected Point executeInEDT() {
072 validateIsEnabledAndShowing(frame);
073 return iconifyLocationOf(frame);
074 }
075 });
076 }
077
078 /**
079 * Deiconifies the given <code>{@link Frame}</code>.
080 * @param frame the given <code>Frame</code>.
081 * @throws IllegalStateException if the <code>Frame</code> is not enabled.
082 * @throws IllegalStateException if the <code>Frame</code> is not showing on the screen.
083 */
084 @RunsInEDT
085 public void deiconify(Frame frame) {
086 assertIsEnabledAndShowing(frame);
087 updateFrameExtendedState(frame, NORMAL);
088 }
089
090 /**
091 * Normalizes the given <code>{@link Frame}</code>.
092 * @param frame the given <code>Frame</code>.
093 * @throws IllegalStateException if the <code>Frame</code> is not enabled.
094 * @throws IllegalStateException if the <code>Frame</code> is not showing on the screen.
095 */
096 @RunsInEDT
097 public void normalize(Frame frame) {
098 assertIsEnabledAndShowing(frame);
099 updateFrameExtendedState(frame, NORMAL);
100 }
101
102 /**
103 * Makes the <code>{@link Frame}</code> full size.
104 * @param frame the target <code>Frame</code>.
105 * @throws IllegalStateException if the <code>Frame</code> is not enabled.
106 * @throws IllegalStateException if the <code>Frame</code> is not showing on the screen.
107 * @throws ActionFailedException if the operating system does not support maximizing frames.
108 */
109 @RunsInEDT
110 public void maximize(Frame frame) {
111 moveMouseIgnoringAnyError(frame, maximizeInfo(frame));
112 if (!supportsMaximize(Toolkit.getDefaultToolkit()))
113 throw actionFailure("Platform does not support maximizing frames");
114 updateFrameExtendedState(frame, MAXIMIZED_BOTH);
115 }
116
117 @RunsInEDT
118 private static Point maximizeInfo(final Frame frame) {
119 return execute(new GuiQuery<Point>() {
120 protected Point executeInEDT() {
121 validateIsEnabledAndShowing(frame);
122 return maximizeLocationOf(frame);
123 }
124 });
125 }
126
127 @ThreadSafeAction
128 private void updateFrameExtendedState(Frame frame, int state) {
129 frame.setExtendedState(state);
130 }
131
132 private static boolean supportsMaximize(Toolkit toolkit) {
133 return toolkit.isFrameStateSupported(MAXIMIZED_BOTH);
134 }
135 }