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.BrowserFunction;
20 import org.eclipse.swt.browser.ProgressEvent;
21 import org.eclipse.swt.browser.ProgressListener;
22 import org.eclipse.swt.layout.FillLayout;
23 import org.eclipse.swt.widgets.Display;
24 import org.eclipse.swt.widgets.Shell;
25 
26 public class Bug510972_WinClearedSignal_auto {
27 	static int count = 0;
28 
main(String[] args)29 	public static void main(String[] args) {
30 		Display display = new Display();
31 		final Shell shell = new Shell(display);
32 		shell.setLayout(new FillLayout());
33 		final Browser browser = new Browser(shell, SWT.NONE);
34 
35 		class CustomFunction extends BrowserFunction { // Note: Local class defined inside method.
36 			CustomFunction(Browser browser, String name) {
37 				super(browser, name);
38 			}
39 
40 			@Override
41 			public Object function(Object[] arguments) {
42 				System.out.println( this.getName() + " called from javascript");
43 				if (count == 0) {
44 					browser.setText("2nd page load");
45 					count++;
46 				} else {
47 					System.out.println("Test passed.");
48 				}
49 				return null;
50 			}
51 		}
52 		browser.setText("1st (initial) page load");
53 		new CustomFunction(browser, "callCustomFunction");
54 		browser.execute("callCustomFunction()");
55 
56 
57 		browser.addProgressListener(new ProgressListener() {
58 			@Override
59 			public void completed(ProgressEvent event) {
60 				System.out.println("load finished.");
61 				browser.execute("document.body.style.backgroundColor = 'red'");
62 				browser.execute("callCustomFunction()");
63 			}
64 
65 			@Override
66 			public void changed(ProgressEvent event) {
67 			}
68 		});
69 
70 		shell.open();
71 		while (!shell.isDisposed()) {
72 			if (!display.readAndDispatch())
73 				display.sleep();
74 		}
75 		display.dispose();
76 
77 	}
78 
79 }
80