View Javadoc

1   /**
2    * Copyright 2008 WebPhotos
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.webphotos.gui.util.fest;
17  
18  import java.awt.Dialog;
19  import java.awt.Frame;
20  import static java.awt.event.KeyEvent.*;
21  import java.io.File;
22  import java.util.Arrays;
23  import java.util.Comparator;
24  import java.util.Map;
25  import javax.swing.JButton;
26  import net.sf.webphotos.WebPhotos;
27  import static org.fest.assertions.Assertions.assertThat;
28  import org.fest.swing.core.BasicRobot;
29  import org.fest.swing.core.ComponentLookupScope;
30  import org.fest.swing.core.GenericTypeMatcher;
31  import org.fest.swing.core.MouseButton;
32  import org.fest.swing.core.Robot;
33  import org.fest.swing.finder.JFileChooserFinder;
34  import static org.fest.swing.finder.WindowFinder.*;
35  import org.fest.swing.fixture.DialogFixture;
36  import org.fest.swing.fixture.FrameFixture;
37  import org.fest.swing.fixture.JButtonFixture;
38  import org.fest.swing.fixture.JFileChooserFixture;
39  import static org.fest.swing.launcher.ApplicationLauncher.*;
40  import org.fest.swing.security.NoExitSecurityManagerInstaller;
41  
42  /**
43   *
44   * @author Guilherme
45   */
46  public class WebPhotosDelegate implements net.sf.webphotos.gui.util.WebPhotosDelegate {
47  
48      private static final WebPhotosDelegate WEB_PHOTOS_DELEGATE = new WebPhotosDelegate();
49      private static final Robot robot;
50      private static final int TIMEOUT_SECONDS = 10;
51      
52  
53      private static final Comparator<String> IGNORE_CASE_COMPARATOR = new Comparator<String>() {
54  
55          @Override
56          public int compare(String o1, String o2) {
57              System.out.println(o1+".compareToIgnoreCase("+o2+") = " + o1.compareToIgnoreCase(o2));
58              return o1.compareToIgnoreCase(o2);
59          }
60      };
61      
62      private FrameFixture mainFrame;
63      private NoExitSecurityManagerInstaller noExitSecurityManagerInstaller;
64  
65      static {
66          robot = BasicRobot.robotWithCurrentAwtHierarchy();
67          robot.settings().delayBetweenEvents(50);
68          robot.settings().componentLookupScope(ComponentLookupScope.ALL);
69      }
70  
71      @Override
72      public void setUpClass() throws Exception {
73          
74          noExitSecurityManagerInstaller = NoExitSecurityManagerInstaller.installNoExitSecurityManager();
75  
76          java.awt.EventQueue.invokeLater(new Runnable() {
77              @Override
78              public void run() {
79                  application(WebPhotos.class).start();
80              }
81          });
82  
83          DialogFixture dialog = findDialog(new LoginDialogTypeMatcherImpl("Web")).withTimeout(TIMEOUT_SECONDS, java.util.concurrent.TimeUnit.SECONDS).using(robot);
84  
85          dialog.button(new GenericTypeMatcher<JButton>(JButton.class) {
86              @Override
87              protected boolean isMatching(JButton okButton) {
88                  return "OK".equalsIgnoreCase(okButton.getText()) && okButton.isShowing();
89              }
90          }).click(MouseButton.LEFT_BUTTON);
91  
92          mainFrame = findFrame(new MainFrameTypeMatcherImpl("Web")).withTimeout(TIMEOUT_SECONDS, java.util.concurrent.TimeUnit.SECONDS).using(robot);
93  
94      }
95  
96      @Override
97      public void addPhotosToAlbumAndCheck(String[] fileNames, String buttonName, File folderNameToVerify, String fileDialogName) throws RuntimeException {
98          JButtonFixture button = clickButton(buttonName);
99          JFileChooserFixture fileChooser = JFileChooserFinder.findFileChooser().withTimeout(TIMEOUT_SECONDS, java.util.concurrent.TimeUnit.SECONDS).using(button.robot);
100 
101         fileChooser.component().getCurrentDirectory().equals(folderNameToVerify);
102         
103         File[] files = new File[fileNames.length];
104         for (int i = 0; i < files.length; i++) {
105             files[i] = new File(fileNames[i]);
106         }
107         fileChooser.selectFiles(files).pressKey(VK_ENTER);
108         
109     }
110 
111     @Override
112     public void checkButtonHasText(String button, String textShown) throws RuntimeException {
113         assertThat(mainFrame.button(button).text().contains(textShown)).isTrue();
114     }
115 
116     @Override
117     public void checkComboBoxHasText(String comboBoxName, String textShown) throws RuntimeException {
118         System.out.println("mainFrame.comboBox("+comboBoxName+").contents()" + mainFrame.comboBox(comboBoxName).contents().toString());
119         Arrays.sort(mainFrame.comboBox(comboBoxName).contents(), IGNORE_CASE_COMPARATOR);
120         assertThat(Arrays.binarySearch(mainFrame.comboBox(comboBoxName).contents(), textShown, IGNORE_CASE_COMPARATOR) >= 0).isTrue();
121     }
122 
123     @Override
124     public void checkIsButtonEnabled(String button) throws RuntimeException {
125         assertThat(mainFrame.button(button).component().isVisible()).isTrue();
126         assertThat(mainFrame.button(button).component().isEnabled()).isTrue();
127     }
128 
129     @Override
130     public void fillModalWithText(final String buttonName, String modalTittle, String data) throws RuntimeException {
131         JButtonFixture button = clickButton(buttonName);
132 
133         DialogFixture dialog = findDialog(new LoginDialogTypeMatcherImpl(modalTittle)).withTimeout(TIMEOUT_SECONDS, java.util.concurrent.TimeUnit.SECONDS).using(button.robot);
134 
135         dialog.textBox().setText(data);
136         dialog.pressKey(VK_ENTER);
137     }
138 
139     @Override
140     public void tearDownClass() throws Exception {
141         System.out.println("Shutting down FEST");
142         noExitSecurityManagerInstaller.uninstall();
143         mainFrame.close();
144     }
145 
146     @Override
147     public void validateMainWindowIsPresent() {
148         assertThat(mainFrame.component().isVisible()).isTrue();
149         assertThat(mainFrame.component().isEnabled()).isTrue();
150     }
151 
152     /**
153      * Setup for One Test
154      */
155     @Override
156     public void setUp() {
157     }
158 
159     /**
160      * Returns the Delegate
161      *
162      * @return
163      */
164     public static WebPhotosDelegate getWebPhotosDelegate() {
165         return WEB_PHOTOS_DELEGATE;
166     }
167 
168     private JButtonFixture clickButton(final String buttonName) {
169         JButtonFixture button = mainFrame.button(new JButtonTypeMatcherImpl(JButton.class, buttonName)).click(MouseButton.LEFT_BUTTON);
170         return button;
171     }
172 
173     @Override
174     public void fillAlbumForm(String albumTitle, String albumDescription, String categoryName, Map<String, String[]> photoData) {
175         throw new UnsupportedOperationException("Not supported yet.");
176     }
177 
178     @Override
179     public void checkNewAlbum() {
180         throw new UnsupportedOperationException("Not supported yet.");
181     }
182 
183     private static class LoginDialogTypeMatcherImpl extends GenericTypeMatcher<Dialog> {
184 
185         private String dialogName;
186 
187         public LoginDialogTypeMatcherImpl(final String dialogName) {
188             super(Dialog.class);
189             this.dialogName = dialogName;
190         }
191 
192         @Override
193         protected boolean isMatching(Dialog frame) {
194             return frame.getTitle().contains(dialogName) && frame.isShowing();
195         }
196     }
197 
198     private static class MainFrameTypeMatcherImpl extends GenericTypeMatcher<Frame> {
199 
200         private String frameName;
201 
202         public MainFrameTypeMatcherImpl(final String frameName) {
203             super(Frame.class);
204             this.frameName = frameName;
205         }
206 
207         @Override
208         protected boolean isMatching(Frame frame) {
209             return frame.getTitle().contains(frameName) && frame.isShowing();
210         }
211     }
212 
213     private static class JButtonTypeMatcherImpl extends GenericTypeMatcher<JButton> {
214 
215         private final String buttonName;
216 
217         public JButtonTypeMatcherImpl(Class<JButton> supportedType, String buttonName) {
218             super(supportedType);
219             this.buttonName = buttonName;
220         }
221 
222         @Override
223         protected boolean isMatching(JButton chosenButtonName) {
224             return buttonName.equalsIgnoreCase(chosenButtonName.getName());
225         }
226     }
227 }