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.uispec4j;
17  
18  import java.io.File;
19  import java.util.Map;
20  import net.sf.webphotos.WebPhotos;
21  import org.uispec4j.Button;
22  import org.uispec4j.ComponentAmbiguityException;
23  import org.uispec4j.ItemNotFoundException;
24  import org.uispec4j.Table;
25  import org.uispec4j.Trigger;
26  import org.uispec4j.UISpec4J;
27  import org.uispec4j.Window;
28  import org.uispec4j.interception.FileChooserHandler;
29  import org.uispec4j.interception.WindowInterceptor;
30  import org.uispec4j.utils.MainClassTrigger;
31  
32  /**
33   *
34   * @author Guilherme
35   */
36  public class WebPhotosDelegate implements net.sf.webphotos.gui.util.WebPhotosDelegate {
37  
38      private static final WebPhotosDelegate WEB_PHOTOS_DELEGATE = new WebPhotosDelegate();
39  
40      static {
41          UISpec4J.init();
42          UISpec4J.setWindowInterceptionTimeLimit(30000);
43      }
44      private static Window mainWindow;
45      private static Trigger initialTrigger;
46  
47      /**
48       * Tests Button
49       *
50       * @param button
51       * @throws RuntimeException no errors allowed
52       */
53      @Override
54      public void checkIsButtonEnabled(final String button) throws RuntimeException {
55          getButton(button).isEnabled().check();
56      }
57  
58      /**
59       * Tests Button Text
60       *
61       * @param button
62       * @param textShown
63       * @throws RuntimeException no errors allowed
64       */
65      @Override
66      public void checkButtonHasText(final String button, final String textShown) throws RuntimeException {
67          getButton(button).textEquals(textShown).check();
68      }
69  
70      /**
71       * Tests Combox Text
72       *
73       * @param comboBoxName
74       * @param textShown
75       * @throws RuntimeException no errors allowed
76       */
77      @Override
78      public void checkComboBoxHasText(final String comboBoxName, final String textShown) throws RuntimeException {
79          WebPhotosDelegate.mainWindow.getComboBox(comboBoxName).contains(textShown).check();
80      }
81  
82      /**
83       * Tests mainWindow
84       */
85      @Override
86      public void validateMainWindowIsPresent() throws RuntimeException {
87          WebPhotosDelegate.mainWindow.isVisible().check();
88          WebPhotosDelegate.mainWindow.isEnabled().check();
89      }
90  
91      /**
92       * Dispose all tests
93       *
94       * @throws Exception no errors allowed
95       */
96      @Override
97      public void tearDownClass() throws Exception {
98          mainWindow.dispose();
99      }
100 
101     /**
102      * Prepare all tests
103      *
104      * @throws Exception no errors allowed
105      */
106     @Override
107     public void setUpClass() throws Exception {
108         WebPhotosDelegate.initialTrigger = WindowInterceptor.getModalDialog(new MainClassTrigger(WebPhotos.class, "")).getButton("OK").triggerClick();
109         WebPhotosDelegate.mainWindow = WindowInterceptor.run(WebPhotosDelegate.initialTrigger);
110     }
111 
112     /**
113      *
114      * @param fileNames
115      * @param buttonName
116      * @param folderNameToVerify
117      * @param fileDialogName
118      * @throws RuntimeException no errors allowed
119      */
120     @Override
121     public void addPhotosToAlbumAndCheck(final String[] fileNames, final String buttonName, final File folderNameToVerify, final String fileDialogName) throws RuntimeException {
122         Table tbFotos = addPhotosToAlbum(fileNames, buttonName, folderNameToVerify, fileDialogName);
123         checkPhotosTable(tbFotos, fileNames);
124     }
125 
126     /**
127      * Writes text to a Modal Dialog
128      *
129      * @param buttonName
130      * @param modalTittle
131      * @param data
132      * @throws RuntimeException no errors allowed
133      */
134     @Override
135     public void fillModalWithText(final String buttonName, final String modalTittle, final String data) throws RuntimeException {
136         WindowInterceptor.init(mainWindowTriggerClick(buttonName)).process(new ModalWindowHandler(modalTittle, data)).run();
137     }
138 
139     /**
140      * Returns the Delegate
141      *
142      * @return
143      */
144     public static WebPhotosDelegate getWebPhotosDelegate() {
145         return WEB_PHOTOS_DELEGATE;
146     }
147 
148     /**
149      * Setup one Test
150      */
151     @Override
152     public void setUp() {
153     }
154 
155     @Override
156     public void fillAlbumForm(String albumTitle, String albumDescription, String categoryName, Map<String, String[]> photoData) {
157         mainWindow.getTextBox("txtTitulo").setText(albumTitle);
158         mainWindow.getTextBox("txtDescricao").setText(albumDescription);
159         mainWindow.getComboBox("lstCategoriasAlbum").select(categoryName);
160 
161         final Table tbFotos = WebPhotosDelegate.mainWindow.getTable("tbFotos");
162         for (String photoName : photoData.keySet()) {
163             tbFotos.selectRowsWithText(0, photoName);
164             System.out.println("Line Selected (tbFotos.getAwtComponent().getSelectedRow()) : " + tbFotos.getAwtComponent().getSelectedRow());
165             tbFotos.getAwtComponent().setValueAt(photoData.get(photoName)[0], tbFotos.getAwtComponent().getSelectedRow(), 1);
166             //tbFotos.editCell(tbFotos.getAwtComponent().getSelectedRow(), 1, photoData.get(photoName)[0], true);
167             //mainWindow.getTextBox("txtLegenda").setText(photoData.get(photoName)[0]);
168             //mainWindow.findSwingComponent(JTextArea.class, "txtLegenda").setText(photoData.get(photoName)[0]);
169             mainWindow.getComboBox("lstCreditos").select(photoData.get(photoName)[1]);
170             tbFotos.selectRowsWithText(0, photoName);
171         }
172 
173         WindowInterceptor.run(getButton("btNovo").triggerClick());
174         //getButton("btNovo").click();
175     }
176 
177     @Override
178     public void checkNewAlbum() {
179         Table tbAlbuns = mainWindow.getTable("tbAlbuns");
180         tbAlbuns.rowCountEquals(1).check();
181     }
182 
183     private Table addPhotosToAlbum(final String[] fileNames, final String targetButton, final File startUpFolder, final String fileChooserName) throws RuntimeException {
184         FileChooserHandler openDialog = FileChooserHandler.init().titleEquals(fileChooserName).assertAcceptsFilesOnly().assertMultiSelectionEnabled(true).assertCurrentDirEquals(startUpFolder);
185         WindowInterceptor.init(getButton(targetButton).triggerClick()).process(openDialog.select(fileNames)).run();
186         final Table tbFotos = WebPhotosDelegate.mainWindow.getTable("tbFotos");
187         return tbFotos;
188     }
189 
190     private Trigger mainWindowTriggerClick(final String buttonName) throws RuntimeException {
191         return getButton(buttonName).triggerClick();
192     }
193 
194     private void checkPhotosTable(Table tbFotos, final String[] fileNames) {
195         tbFotos.rowCountEquals(fileNames.length).check();
196         tbFotos.hasHeader().check();
197         tbFotos.columnCountEquals(3).check();
198         for (int i = 0; i < fileNames.length; i++) {
199             System.out.printf("fileNames[%d] = %s", i, fileNames[i]);
200             System.out.println();
201             System.out.printf("tbFotos.getContentAt(%d, 0).toString() = %s", i, tbFotos.getContentAt(i, 0).toString());
202             System.out.println();
203             tbFotos.cellEquals(i + 1, 1, fileNames[i]);
204         }
205     }
206 
207     private Button getButton(final String button) throws ItemNotFoundException, ComponentAmbiguityException {
208         return mainWindow.getButton(button);
209     }
210 
211     /**
212      * Private Constructor: Singleton Pattern
213      */
214     private WebPhotosDelegate() {
215     }
216 }