1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     Red Hat - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.tests.gtk.snippets;
15 
16 
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.browser.Browser;
19 import org.eclipse.swt.browser.LocationEvent;
20 import org.eclipse.swt.browser.LocationListener;
21 import org.eclipse.swt.browser.ProgressEvent;
22 import org.eclipse.swt.browser.ProgressListener;
23 import org.eclipse.swt.browser.VisibilityWindowListener;
24 import org.eclipse.swt.browser.WindowEvent;
25 import org.eclipse.swt.events.MouseEvent;
26 import org.eclipse.swt.events.MouseListener;
27 import org.eclipse.swt.layout.FillLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Shell;
31 
32 public class Bug505418_Listeners_evals {
33 
34 	public static int count = 0;
35 
main(String[] args)36 	public static void main(String[] args) {
37 //		locationChange();  // new url
38 		progressListener(); // make progress.
39 //		statusTextListener();
40 //		closeWindowListener();
41 //		openWindowListener();
42 //		titleListener();
43 //		visibilityWindowListener(); // Quircky on webkit2.
44 //		miscSWTListeners();
45 	}
46 
47 	@SuppressWarnings("unused")
locationChange()48 	private static void locationChange() {
49 		Display display = new Display();
50 		final Shell shell = new Shell(display);
51 		shell.setLayout(new FillLayout());
52 		Button button = new Button(shell, SWT.PUSH);
53 		final Browser browser = new Browser(shell, SWT.NONE);
54 		button.setText("Click to increase count.");
55 		button.addMouseListener(new MouseListener() {
56 			@Override
57 			public void mouseUp(MouseEvent e) {}
58 			@Override
59 			public void mouseDown(MouseEvent e) {
60 				shell.setText("Count: " + count++);
61 			}
62 			@Override
63 			public void mouseDoubleClick(MouseEvent e) {}
64 		});
65 
66 		browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
67 		browser.addProgressListener(new ProgressListener() {
68 			@Override
69 			public void completed(ProgressEvent event) {
70 				browser.addLocationListener(new LocationListener() {
71 					@Override
72 					public void changing(LocationEvent event) {
73 						System.out.println ("changing...");
74 
75 						String value = (String)browser.evaluate("return 'Changing eval...'");
76 						System.out.println("changing returned: " + value);
77 
78 //						widget.browser.evaluate("return 'Changing eval...'");
79 
80 //						event.doit = false; // Stop page load.
81 					}
82 
83 					@Override
84 					public void changed(LocationEvent event) {
85 						System.out.print("Changed!");
86 						String value = (String)browser.evaluate("return 'finished callback'");
87 						System.out.println("Changed returned: " + value);
88 					}
89 				});
90 			}
91 			@Override
92 			public void changed(ProgressEvent event) {}
93 		});
94 
95 		shell.open();
96 		while (!shell.isDisposed()) {
97 			if (!display.readAndDispatch())
98 				display.sleep();
99 		}
100 		display.dispose();
101 	}
102 
progressListener()103 	private static void progressListener () {
104 		Display display = new Display();
105 		final Shell shell = new Shell(display);
106 		shell.setLayout(new FillLayout());
107 		Button button = new Button(shell, SWT.PUSH);
108 		final Browser browser = new Browser(shell, SWT.NONE);
109 		button.setText("Click to increase count.");
110 		button.addMouseListener(new MouseListener() {
111 			@Override
112 			public void mouseUp(MouseEvent e) {}
113 			@Override
114 			public void mouseDown(MouseEvent e) {
115 				shell.setText("Count: " + count++);
116 			}
117 			@Override
118 			public void mouseDoubleClick(MouseEvent e) {}
119 		});
120 
121 		browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
122 		browser.addProgressListener(new ProgressListener() {
123 
124 			@Override
125 			public void completed(ProgressEvent event) {
126 				System.out.println("compleated!! " + event.current);
127 				evalTest(browser, "");
128 			}
129 
130 			@Override
131 			public void changed(ProgressEvent event) {
132 				System.out.println("changing..." + event.current);
133 				evalTest(browser, "");
134 
135 
136 			}
137 		});
138 		shell.open();
139 		while (!shell.isDisposed()) {
140 			if (!display.readAndDispatch())
141 				display.sleep();
142 		}
143 		display.dispose();
144 	}
145 
statusTextListener()146 	static void statusTextListener() {
147 		Display display = new Display();
148 		final Shell shell = new Shell(display);
149 		shell.setLayout(new FillLayout());
150 		Button button = new Button(shell, SWT.PUSH);
151 		final Browser browser = new Browser(shell, SWT.NONE);
152 		button.setText("Click to increase count.");
153 		button.addMouseListener(new MouseListener() {
154 			@Override
155 			public void mouseUp(MouseEvent e) {}
156 			@Override
157 			public void mouseDown(MouseEvent e) {
158 				shell.setText("Count: " + count++);
159 			}
160 			@Override
161 			public void mouseDoubleClick(MouseEvent e) {}
162 		});
163 
164 		browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
165 		browser.addStatusTextListener(event -> {
166 			System.out.println("Status has changed");
167 			evalTest(browser, "Status changed");
168 		});
169 
170 		shell.open();
171 		while (!shell.isDisposed()) {
172 			if (!display.readAndDispatch())
173 				display.sleep();
174 		}
175 		display.dispose();
176 	}
177 
closeWindowListener()178 	static void closeWindowListener() {
179 		Display display = new Display();
180 		final Shell shell = new Shell(display);
181 		shell.setLayout(new FillLayout());
182 		Button button = new Button(shell, SWT.PUSH);
183 		Button closeBrowserButton = new Button(shell, SWT.PUSH);
184 		final Browser browser = new Browser(shell, SWT.NONE);
185 		closeBrowserButton.setText("Close widget.browser button");
186 		closeBrowserButton.addMouseListener(new MouseListener() {
187 			@Override
188 			public void mouseUp(MouseEvent e) {
189 			}
190 
191 			@Override
192 			public void mouseDown(MouseEvent e) {
193 				browser.execute("window.close()");
194 			}
195 
196 			@Override
197 			public void mouseDoubleClick(MouseEvent e) {
198 			}
199 		});
200 
201 		button.setText("Click to increase count.");
202 		button.addMouseListener(new MouseListener() {
203 			@Override
204 			public void mouseUp(MouseEvent e) {}
205 			@Override
206 			public void mouseDown(MouseEvent e) {
207 				shell.setText("Count: " + count++);
208 			}
209 			@Override
210 			public void mouseDoubleClick(MouseEvent e) {}
211 		});
212 		browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
213 		browser.addCloseWindowListener(event -> {
214 			System.out.println("closing...");
215 			evalTest(browser, "closing...");
216 			System.out.println("Browser getText(): "+ browser.getText());
217 		});
218 		shell.open();
219 		while (!shell.isDisposed()) {
220 			if (!display.readAndDispatch())
221 				display.sleep();
222 		}
223 		display.dispose();
224 	}
225 
openWindowListener()226 	static void openWindowListener() {
227 		Display display = new Display();
228 		final Shell shell = new Shell(display);
229 		shell.setLayout(new FillLayout());
230 		Button button = new Button(shell, SWT.PUSH);
231 		Button closeBrowserButton = new Button(shell, SWT.PUSH);
232 		final Browser browser = new Browser(shell, SWT.NONE);
233 		closeBrowserButton.setText("Close widget.browser button");
234 		closeBrowserButton.addMouseListener(new MouseListener() {
235 			@Override
236 			public void mouseUp(MouseEvent e) {
237 			}
238 
239 			@Override
240 			public void mouseDown(MouseEvent e) {
241 				browser.execute("window.open()");
242 			}
243 
244 			@Override
245 			public void mouseDoubleClick(MouseEvent e) {
246 			}
247 		});
248 
249 		button.setText("Click to increase count.");
250 		button.addMouseListener(new MouseListener() {
251 			@Override
252 			public void mouseUp(MouseEvent e) {}
253 			@Override
254 			public void mouseDown(MouseEvent e) {
255 				shell.setText("Count: " + count++);
256 			}
257 			@Override
258 			public void mouseDoubleClick(MouseEvent e) {}
259 		});
260 		browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
261 		browser.addOpenWindowListener(event -> {
262 			System.out.println("opening...");
263 			evalTest(browser, "opening...");
264 		});
265 		shell.open();
266 		while (!shell.isDisposed()) {
267 			if (!display.readAndDispatch())
268 				display.sleep();
269 		}
270 		display.dispose();
271 	}
272 
titleListener()273 	static void titleListener() {
274 		Display display = new Display();
275 		final Shell shell = new Shell(display);
276 		shell.setLayout(new FillLayout());
277 		Button button = new Button(shell, SWT.PUSH);
278 		final Browser browser = new Browser(shell, SWT.NONE);
279 		browser.addTitleListener(event -> {
280 			System.out.println("Title has changed :" + event.title);
281 			evalTest(browser, "title change... ");
282 		});
283 
284 		button.setText("Click to increase count.");
285 		button.addMouseListener(new MouseListener() {
286 			@Override
287 			public void mouseUp(MouseEvent e) {}
288 			@Override
289 			public void mouseDown(MouseEvent e) {
290 				shell.setText("Count: " + count++);
291 			}
292 			@Override
293 			public void mouseDoubleClick(MouseEvent e) {}
294 		});
295 		browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
296 		shell.open();
297 		while (!shell.isDisposed()) {
298 			if (!display.readAndDispatch())
299 				display.sleep();
300 		}
301 		display.dispose();
302 	}
303 
visibilityWindowListener()304 	static void visibilityWindowListener() {
305 		Display display = new Display();
306 		final Shell shell = new Shell(display);
307 		shell.setLayout(new FillLayout());
308 		Button button = new Button(shell, SWT.PUSH);
309 		Button closeBrowserButton = new Button(shell, SWT.PUSH);
310 		final Browser browser = new Browser(shell, SWT.NONE);
311 
312 		final Shell shell2 = new Shell(display);
313 		shell2.setLayout(new FillLayout());
314 		final Browser browser2 = new Browser(shell2, SWT.NONE);
315 		browser2.addVisibilityWindowListener(new VisibilityWindowListener() {
316 
317 			@Override
318 			public void show(WindowEvent event) {
319 				System.out.println("showing...");
320 				shell2.open();
321 				evalTest(browser2, "browse2 show");
322 				evalTest(browser, "browse1 show");
323 			}
324 
325 			@Override
326 			public void hide(WindowEvent event) {
327 			}
328 		});
329 
330 		closeBrowserButton.setText("Close widget.browser button");
331 		closeBrowserButton.addMouseListener(new MouseListener() {
332 			@Override
333 			public void mouseUp(MouseEvent e) {
334 			}
335 
336 			@Override
337 			public void mouseDown(MouseEvent e) {
338 				browser.execute("document.body.style.backgroundColor = 'red'");
339 				browser.execute("window.open('https://www.google.com', 'Dialog')");
340 			}
341 
342 			@Override
343 			public void mouseDoubleClick(MouseEvent e) {
344 			}
345 		});
346 
347 		button.setText("Click to increase count.");
348 		button.addMouseListener(new MouseListener() {
349 			@Override
350 			public void mouseUp(MouseEvent e) {}
351 			@Override
352 			public void mouseDown(MouseEvent e) {
353 				shell.setText("Count: " + count++);
354 			}
355 			@Override
356 			public void mouseDoubleClick(MouseEvent e) {}
357 		});
358 		browser.setText("<html><title>Snippet</title><body><a href=\"https://eclipse.org/\">Eclipse.org  Disposing on link change causes hang</a></body></html>");
359 		browser.addOpenWindowListener(event -> {
360 			System.out.println("opening...");
361 			event.browser = browser2;
362 
363 		});
364 		shell.open();
365 		while (!shell.isDisposed()) {
366 			if (!display.readAndDispatch())
367 				display.sleep();
368 		}
369 		display.dispose();
370 	}
371 
372 	@SuppressWarnings("unused")
miscSWTListeners()373 	private static void miscSWTListeners() {
374 		Display display = new Display();
375 		final Shell shell = new Shell(display);
376 		shell.setLayout(new FillLayout());
377 		final Browser browser = new Browser(shell, SWT.NONE);
378 		browser.setText("Hello world!");
379 		browser.addMouseMoveListener(e -> {
380 			System.out.println("mouse moved!");
381 			String str = (String) browser.evaluate("return 'hello world'");
382 			System.out.println("Evaluated: " + str);
383 		});
384 
385 		shell.open();
386 		while (!shell.isDisposed()) {
387 			if (!display.readAndDispatch())
388 				display.sleep();
389 		}
390 		display.dispose();
391 	}
392 
evalTest(final Browser browser, Object msg)393 	private static void evalTest(final Browser browser, Object msg) {
394 		String value = (String)browser.evaluate("return 'hello'");
395 		System.out.println("Returned: " + value);
396 		String script = "document.body.style.backgroundColor = 'red'";
397 		browser.execute(script);
398 	}
399 
400 }
401