Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3ea13ecabfd79a4fca09f55236d14e45f4e7369 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*******************************************************************************
 * Copyright 2007 new SWTBot, http://swtbot.org/
 * Copyright (c) 2008 Ketan Padegaonkar and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ketan Padegaonkar - initial API and implementation
 *     Vincent Mahe - http://swtbot.org/bugzilla/show_bug.cgi?id=123
 *******************************************************************************/
package org.eclipse.swtbot.swt.finder;

import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.TimeoutException;

/**
 * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>
 * @author Vincent Mahe <vmahe [at] irisa [dot] fr>
 * @version $Id$
 */
public class SWTBotTest extends AbstractMenuExampleTest {

	private SWTBot	bot;

	public void testWaits5SecondsAndFailsForFailingCondition() throws Exception {
		final long begin = System.currentTimeMillis();
		try {
			bot.waitUntil(new DefaultCondition() {
				public boolean test() throws Exception {
					return false;
				}

				public String getFailureMessage() {
					return "timed out";
				}
			});
			fail("Expected a timeout exception");
		} catch (TimeoutException expected) {
			final long end = System.currentTimeMillis();
			final long timeout = end - begin;
			assertTrue(timeout >= 5000);
			assertTrue(timeout <= 6000);
			assertEquals("Timeout after: 5000 ms.: timed out", expected.getMessage());
		}
	}

	public void testDoesNotWait5SecondsAndPassesForPassingCondition() throws Exception {
		final long begin = System.currentTimeMillis();
		bot.waitUntil(new DefaultCondition() {
			public boolean test() throws Exception {
				return true;
			}

			public String getFailureMessage() {
				return "timed out";
			}
		});
		final long end = System.currentTimeMillis();
		final long timeout = end - begin;
		assertTrue(timeout <= 100);

	}

	public void testThrowsExceptionOnNegativeTimeOut() throws Exception {
		try {
			bot.waitUntil(null, -10);
			fail("expecting an exception");
		} catch (Exception e) {
			assertEquals("assertion failed: timeout value is negative", e.getMessage());
		}
	}

	public void testGetsAllShells() throws Exception {
		assertEquals(7, bot.shells().length);
	}

	public void testFindsShellsById() throws Exception {
		final SWTBotShell shell = bot.shell("SWT Clipboard");
		UIThreadRunnable.syncExec(new VoidResult() {
			public void run() {
				shell.widget.setData("foo-shell", "bar");
			}
		});

		assertSame(shell.widget, bot.shellWithId("foo-shell", "bar").widget);
	}

	public void testWaitsLessThan5sWhenConditionSwitchToFailing() {
		final long begin = System.currentTimeMillis();
		bot.waitWhile(new DefaultCondition() {

			public String getFailureMessage() {
				return "time out";
			}

			public boolean test() throws Exception {
				if (System.currentTimeMillis() < begin + 2500)
					return true;
				return false;
			}

		});
		final long end = System.currentTimeMillis();
		final long timeout = end - begin;
		assertTrue(timeout >= 2500);
		assertTrue(timeout < 5000);
	}

	public void testWaitsMoreThan5sWhenConditionDoesNotSwitchToFailing() {
		final long begin = System.currentTimeMillis();
		try {
			bot.waitWhile(new DefaultCondition() {

				public String getFailureMessage() {
					return "time out";
				}

				public boolean test() throws Exception {
					if (System.currentTimeMillis() < begin + 10000)
						return true;
					return false;
				}

			});
		} catch (TimeoutException expected) {
			final long end = System.currentTimeMillis();
			final long timeout = end - begin;
			assertTrue(timeout >= 5000);
			assertTrue(timeout <= 6000);
			assertEquals("Timeout after: 5000 ms.: time out", expected.getMessage());
		}
	}

	protected void setUp() throws Exception {
		super.setUp();
		bot = new SWTBot();
	}
}

Back to the top