1 | package org.openstreetmap.josm.testframework;
|
---|
2 |
|
---|
3 | import java.awt.AWTEvent;
|
---|
4 | import java.awt.AWTException;
|
---|
5 | import java.awt.Toolkit;
|
---|
6 | import java.awt.event.AWTEventListener;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 |
|
---|
11 | import javax.swing.JButton;
|
---|
12 | import javax.swing.JDialog;
|
---|
13 | import javax.swing.JFrame;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.SwingUtilities;
|
---|
16 |
|
---|
17 | import org.junit.Before;
|
---|
18 | import org.junit.BeforeClass;
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.Preferences;
|
---|
21 | import org.openstreetmap.josm.data.projection.Epsg4326;
|
---|
22 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
23 |
|
---|
24 | public class MainMock {
|
---|
25 |
|
---|
26 | private static JDialog lastPopup;
|
---|
27 |
|
---|
28 | @Before public void clearFoundPopup() {
|
---|
29 | lastPopup = null;
|
---|
30 | }
|
---|
31 |
|
---|
32 | @BeforeClass public static void mockMain() throws Exception {
|
---|
33 | Main.pref = new Preferences(){
|
---|
34 | @Override protected void save() {}
|
---|
35 | @Override public void load() throws IOException {}
|
---|
36 | @Override public Collection<Bookmark> loadBookmarks() throws IOException {return Collections.emptyList();}
|
---|
37 | @Override public void saveBookmarks(Collection<Bookmark> bookmarks) throws IOException {}
|
---|
38 | };
|
---|
39 | Main.parent = new JFrame();
|
---|
40 | Main.proj = new Epsg4326();
|
---|
41 | Main.pleaseWaitDlg = new PleaseWaitDialog(Main.parent);
|
---|
42 | Main.main = new Main(){};
|
---|
43 | }
|
---|
44 |
|
---|
45 | @BeforeClass public static void startPopupKiller() throws AWTException {
|
---|
46 | Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener(){
|
---|
47 | public void eventDispatched(AWTEvent event) {
|
---|
48 | if (event.getSource() instanceof JButton) {
|
---|
49 | JButton b = (JButton)event.getSource();
|
---|
50 | if (b.getParent().getParent() instanceof JOptionPane) {
|
---|
51 | lastPopup = (JDialog)SwingUtilities.getRoot(b);
|
---|
52 | b.doClick();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }, AWTEvent.FOCUS_EVENT_MASK);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void assertPopup() {
|
---|
60 | waitForPopup();
|
---|
61 | lastPopup = null;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public JDialog waitForPopup() {
|
---|
65 | for (int i = 0; i < 100; ++i) {
|
---|
66 | if (lastPopup != null)
|
---|
67 | return lastPopup;
|
---|
68 | try {Thread.sleep(10);} catch (InterruptedException e) {}
|
---|
69 | }
|
---|
70 | throw new AssertionError("Expected Popup dialog");
|
---|
71 | }
|
---|
72 | }
|
---|