1 /*******************************************************************************
2  * Copyright (c) 2018 Red Hat and others. All rights reserved.
3  * The contents of this file are made available under the terms
4  * of the GNU Lesser General Public License (LGPL) Version 2.1 that
5  * accompanies this distribution (lgpl-v21.txt).  The LGPL is also
6  * available at http://www.gnu.org/licenses/lgpl.html.  If the version
7  * of the LGPL at http://www.gnu.org is different to the version of
8  * the LGPL accompanying this distribution and there is any conflict
9  * between the two license versions, the terms of the LGPL accompanying
10  * this distribution shall govern.
11  *
12  * Contributors:
13  *     Red Hat - initial API and implementation
14  */
15 package org.eclipse.swt.tests.gtk.snippets;
16 
17 import java.util.concurrent.atomic.AtomicBoolean;
18 
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.browser.Browser;
21 import org.eclipse.swt.browser.ProgressAdapter;
22 import org.eclipse.swt.browser.ProgressEvent;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Shell;
27 
28 
29 public class Bug535392_getText {
30 	static int run = 1;
main(String[] args)31 	public static void main(String[] args) {
32 		Display display = new Display();
33 		Shell shell = new Shell(display);
34 		shell.setSize(400, 400);
35 		shell.setLayout(new GridLayout(2, false));
36 		Browser browser = new Browser(shell, SWT.BORDER);
37 		browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
38 		shell.open();
39 
40 		// Most of below work, but a few single unicode characters are miss-understood. Heuristic works better with many characters.
41 		// https://en.wikipedia.org/wiki/List_of_Unicode_characters
42 		testValue(display, shell, browser, "-", true); //  working, regular ascii '-'
43 		testValue(display, shell, browser, "‐", true);  // BROKEN, (single char read as UTF-8 instead of UTF-16).
44 		testValue(display, shell, browser, "ABC", true); // 65 66 67
45 		testValue(display, shell, browser, "A®A", true); //  U+00AE 	® 	0174 	® 	Registered sign 	0110
46 		testValue(display, shell, browser, "A¢A", true); // U+00BF 	¿ 	0191 	¿ 	Inverted Question Mark 	0127
47 		testValue(display, shell, browser, "ABCüDü", true); // U+00FC 	ü 	0252 	ü 	Latin Small Letter U with diaeresis 	0188
48 		testValue(display, shell, browser, "AӛB", true); // U+04DB 	ӛ 	Cyrillic Small Letter Schwa with diaeresis 	0620
49 		testValue(display, shell, browser, "Ё", true); // BROKEN. (single char read as UTF-8 instead of UTF-16).  U+04DB 	ӛ 	Cyrillic Small Letter Schwa with diaeresis 	0620
50 
51 		if (run == 0) {
52 			for (int i = 0; i < 100000; i++) {
53 				final String testStr = new String(new char [] {'A', (char) i});
54 				testValue(display, shell, browser, testStr, i, true);
55 
56 			}
57 		}
58 
59 		testValue(display, shell, browser, "SYNOPSIS\n" +
60 				"       find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]\n" +
61 				"\n" +
62 				"DESCRIPTION\n" +
63 				"       This manual page documents the GNU version of find.  GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence (see sec‐\n" +
64 				"       tion OPERATORS), until the outcome is known (the left hand side is false for and operations, true for or), at which point find moves on to the next file name.  If no starting-point is specified, `.' is assumed.\n" +
65 				"\n" +
66 				"       If you are using find in an environment where security is important (for example if you are using it to search directories that are writable by other users), you should read the \"Security Considerations\" chapter of  the  findu‐\n" +
67 				"       tils documentation, which is called Finding Files and comes with findutils.   That document also includes a lot more detail and discussion than this manual page, so you may find it a more useful source of information.\n" +
68 				"\n" +
69 				"OPTIONS\n" +
70 				"       The  -H,  -L  and  -P  options control the treatment of symbolic links.  Command-line arguments following these are taken to be names of files or directories to be examined, up to the first argument that begins with `-', or the\n" +
71 				"       argument `(' or `!'.  That argument and any following arguments are taken to be the expression describing what is to be searched for.  If no paths are given, the current directory is  used.   If  no  expression  is  given,  the\n" +
72 				"       expression -print is used (but you should probably consider using -print0 instead, anyway).\n" +
73 				"\n" +
74 				"       This  manual page talks about `options' within the expression list.  These options control the behaviour of find but are specified immediately after the last path name.  The five `real' options -H, -L, -P, -D and -O must appear\n" +
75 				"       before the first path name, if at all.  A double dash -- can also be used to signal that any remaining arguments are not options (though ensuring that all start points begin with either `./' or `/' is generally safer if you use\n" +
76 				"       wildcards in the list of start points).", true);
77 
78 
79 
80 
81 		display.dispose();
82 	}
83 
84 
testValue(Display display, Shell shell, Browser browser, String testStr, boolean autoTest)85 	private static void testValue(Display display, Shell shell, Browser browser, String testStr, boolean autoTest) {
86 		testValue(display, shell, browser, testStr, 0, autoTest);
87 	}
88 
89 	// I think this is broken for values above 127 :-/.
testValue(Display display, Shell shell, Browser browser, String testStr, int testID, boolean autoTest)90 	private static void testValue(Display display, Shell shell, Browser browser, String testStr, int testID, boolean autoTest) {
91 		AtomicBoolean testFinished = new AtomicBoolean(false);
92 		browser.setText(testStr);
93 
94 		ProgressAdapter completionTester = new ProgressAdapter() {
95 			@Override
96 			public void completed(ProgressEvent event) {
97 				Browser browser = (Browser) event.widget;
98 				String returnedStr = browser.getText();
99 				if (testStr.equals(returnedStr)) {
100 					System.out.println("(PASS): testStr/returnedStr: " + testStr + "/" + returnedStr + "  Test id:" + testID);
101 				} else {
102 					System.err.println("(FAIL): testStr/returnedStr: " + testStr + "/" + returnedStr + "  Test id:" + testID);
103 				}
104 				testFinished.set(true);
105 			}
106 		};
107 
108 		browser.addProgressListener(completionTester);
109 
110 		if (autoTest) {
111 			while (!shell.isDisposed() && !testFinished.get()) {
112 				display.readAndDispatch();
113 			}
114 			browser.removeProgressListener(completionTester);
115 		} else {
116 			while (!shell.isDisposed()) {
117 				if (!display.readAndDispatch()) {
118 					display.sleep();
119 				}
120 			}
121 		}
122 	}
123 }
124