EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic]

COVERAGE SUMMARY FOR SOURCE FILE [JomicTestFixture.java]

nameclass, %method, %block, %line, %
JomicTestFixture.java100% (1/1)92%  (12/13)76%  (262/347)79%  (62.5/79)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JomicTestFixture100% (1/1)92%  (12/13)76%  (262/347)79%  (62.5/79)
JomicTestFixture (String): void 0%   (0/1)0%   (0/4)0%   (0/2)
waitForOpen (): void 100% (1/1)45%  (22/49)64%  (9/14)
waitForWindowToShow (String): Window 100% (1/1)47%  (35/74)52%  (8.3/16)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
fixtureSetUp (): void 100% (1/1)87%  (33/38)99%  (9.9/10)
getGoMenu (): JMenu 100% (1/1)92%  (46/50)95%  (10.5/11)
setUpJomicFrame (File): JomicFrame 100% (1/1)94%  (44/47)90%  (9/10)
JomicTestFixture (): void 100% (1/1)100% (3/3)100% (2/2)
findMenuItem (String): JMenuItem 100% (1/1)100% (9/9)100% (1/1)
fixtureTearDown (): void 100% (1/1)100% (14/14)100% (5/5)
getTimeOut (): long 100% (1/1)100% (2/2)100% (1/1)
setTestProperty (String, String): void 100% (1/1)100% (36/36)100% (5/5)
setUpJomicFrame (): JomicFrame 100% (1/1)100% (6/6)100% (1/1)

1// Jomic - a viewer for comic book archives.
2// Copyright (C) 2004-2008 Thomas Aglassinger
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16package net.sf.jomic;
17 
18import java.awt.Window;
19import java.io.File;
20import java.io.IOException;
21 
22import javax.swing.JMenu;
23import javax.swing.JMenuItem;
24 
25import junit.extensions.abbot.ComponentTestFixture;
26import net.sf.jomic.common.JomicStartup;
27import net.sf.jomic.common.PropertyConstants;
28import net.sf.jomic.common.Settings;
29import net.sf.jomic.tools.LocaleTools;
30import net.sf.jomic.tools.StandardConstants;
31import net.sf.jomic.tools.StringTools;
32import net.sf.jomic.tools.TestTools;
33import net.sf.jomic.ui.JomicFrame;
34import net.sf.jomic.ui.JomicMenuBar;
35import net.sf.wraplog.Logger;
36import abbot.finder.ComponentNotFoundException;
37import abbot.finder.MultipleComponentsFoundException;
38import abbot.finder.matchers.ClassMatcher;
39import abbot.finder.matchers.JMenuItemMatcher;
40import abbot.finder.matchers.WindowMatcher;
41 
42/**
43 *  Test fixture for Jomic GUI tests based on Abbot.
44 *
45 * @author    Thomas Aglassinger
46 */
47public class JomicTestFixture extends ComponentTestFixture
48{
49    private Logger logger;
50    private Settings settings;
51    private StringTools stringTools;
52    private TestTools testTools;
53 
54    public JomicTestFixture(String name) {
55        super(name);
56    }
57 
58    public JomicTestFixture() {
59        super();
60    }
61 
62    /**
63     *  Open Jomic frame with the default test comic already loaded.
64     */
65    protected JomicFrame setUpJomicFrame()
66        throws ComponentNotFoundException, IOException, MultipleComponentsFoundException {
67        return setUpJomicFrame(testTools.getTestComicFile());
68    }
69 
70    /**
71     *  Open Jomic frame with <code>comicFile</code> already loaded.
72     */
73    protected JomicFrame setUpJomicFrame(File comicFile)
74        throws ComponentNotFoundException, IOException, MultipleComponentsFoundException {
75        Jomic result = new Jomic(comicFile);
76        Jomic jomic = result;
77 
78        jomic.show();
79 
80        JomicFrame jomicFrame = (JomicFrame) getFinder().find(new ClassMatcher(JomicFrame.class));
81 
82        // Wait for "Go" menu to be enabled, indicating the comic has been
83        // loaded and the JomicFrame is basically ready to accept commands.
84        waitForOpen();
85 
86        // Make sure we are at the front page showing only one page.
87        JMenuItem goFirstItem = (JMenuItem) getFinder().find(new JMenuItemMatcher("Go|First"));
88 
89        if (goFirstItem.isEnabled()) {
90            goFirstItem.doClick();
91        }
92        settings.setTwoPageMode(false);
93 
94        return jomicFrame;
95    }
96 
97    private void setTestProperty(String name, String value) {
98        String fullName = PropertyConstants.SYSTEM_PROPERTY_PREFIX + name;
99 
100        if (logger.isInfoEnabled()) {
101            logger.info("setting test property " + fullName + " to " + stringTools.sourced(value));
102        }
103        System.setProperty(fullName, value);
104    }
105 
106    /**
107     *  Get timeout in milliseconds after which we should stop waiting for UI operations to be
108     *  finished.
109     */
110    protected long getTimeOut() {
111        return 10000;
112    }
113 
114    private JMenu getGoMenu()
115        throws ComponentNotFoundException, MultipleComponentsFoundException {
116        JMenu result = null;
117        JomicMenuBar menuBar = (JomicMenuBar) getFinder().find(new ClassMatcher(JomicMenuBar.class));
118        int menuIndex = 0;
119 
120        while ((result == null) && (menuIndex < menuBar.getComponentCount())) {
121            JMenu currentMenu = menuBar.getMenu(menuIndex);
122 
123            if (currentMenu.getText().equals("Go")) {
124                result = currentMenu;
125            } else {
126                menuIndex += 1;
127            }
128        }
129        assert result != null;
130        return result;
131    }
132 
133    /**
134     *  Find menu item in front most frame.
135     *
136     * @param  itemPath  a menu path, for example "File|Open next".
137     */
138    protected JMenuItem findMenuItem(String itemPath)
139        throws ComponentNotFoundException, MultipleComponentsFoundException {
140        return (JMenuItem) getFinder().find(new JMenuItemMatcher(itemPath));
141    }
142 
143    protected void fixtureSetUp()
144        throws Throwable {
145        super.fixtureSetUp();
146        testTools = TestTools.instance();
147        JomicStartup.instance().startup();
148        LocaleTools.instance().setLocale("en");
149        stringTools = StringTools.instance();
150        settings = Settings.instance();
151        logger = Logger.getLogger(JomicTestFixture.class);
152        setTestProperty(PropertyConstants.TEST_IGNORE_EXIT, Boolean.TRUE.toString());
153        setTestProperty(PropertyConstants.TEST_IGNORE_MESSAGE_DIALOGS, Boolean.TRUE.toString());
154    }
155 
156    protected void fixtureTearDown()
157        throws Throwable {
158        setTestProperty(PropertyConstants.TEST_IGNORE_MESSAGE_DIALOGS, Boolean.FALSE.toString());
159        settings = null;
160        testTools = null;
161        super.fixtureTearDown();
162    }
163 
164    protected void waitForOpen()
165        throws ComponentNotFoundException, MultipleComponentsFoundException {
166        JMenu goMenu = getGoMenu();
167        long startTime = System.currentTimeMillis();
168 
169        while (!goMenu.isEnabled()) {
170            try {
171                Thread.sleep(StandardConstants.TICK);
172            } catch (InterruptedException interruption) {
173                logger.warn("sleep interrupted, continuing", interruption);
174            }
175            if (System.currentTimeMillis() - startTime > getTimeOut()) {
176                throw new IllegalStateException("goMenu must be enabled before " + getTimeOut()
177                        + " milliseconds have passed");
178            }
179        }
180 
181        // HACK: Wait some time and hope the JomicFrame is really ready to accept commands..
182        try {
183            Thread.sleep(1000);
184        } catch (InterruptedException interruption) {
185            logger.warn("sleep interrupted, moving on", interruption);
186        }
187    }
188 
189    protected Window waitForWindowToShow(String id)
190        throws ComponentNotFoundException, MultipleComponentsFoundException {
191        Window result;
192        long startTime = System.currentTimeMillis();
193 
194        while (!isShowing(id) && (System.currentTimeMillis() - startTime < getTimeOut())) {
195            try {
196                Thread.sleep(StandardConstants.TICK);
197            } catch (InterruptedException interruption) {
198                logger.warn("sleep interrupted, continuing");
199            }
200        }
201        if (!isShowing(id)) {
202            throw new IllegalStateException("window with id=" + StringTools.instance().sourced(id)
203                    + " must show within " + getTimeOut() + " milliseconds");
204        }
205        result = (Window) getFinder().find(new WindowMatcher(id, true));
206 
207        // HACK: Wait some time to actually make the window visible.
208        try {
209            Thread.sleep(1000);
210        } catch (InterruptedException interruption) {
211            IllegalStateException error = new IllegalStateException("sleep interrupted");
212 
213            error.initCause(interruption);
214            throw error;
215        }
216 
217        return result;
218    }
219 
220}

[all classes][net.sf.jomic]
EMMA 2.0.4217 (C) Vladimir Roubtsov