1 /* JToJSSetTest.java
2 Copyright (C) 2012 Red Hat, Inc.
3 
4 This file is part of IcedTea.
5 
6 IcedTea is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 2.
9 
10 IcedTea is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with IcedTea; see the file COPYING.  If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301 USA.
19 
20 Linking this library statically or dynamically with other modules is
21 making a combined work based on this library.  Thus, the terms and
22 conditions of the GNU General Public License cover the whole
23 combination.
24 
25 As a special exception, the copyright holders of this library give you
26 permission to link this library with independent modules to produce an
27 executable, regardless of the license terms of these independent
28 modules, and to copy and distribute the resulting executable under
29 terms of your choice, provided that you also meet, for each linked
30 independent module, the terms and conditions of the license of that
31 module.  An independent module is a module which is not derived from
32 or based on this library.  If you modify this library, you may extend
33 this exception to your version of the library, but you are not
34 obligated to do so.  If you do not wish to do so, delete this
35 exception statement from your version.
36  */
37 
38 import net.sourceforge.jnlp.ProcessResult;
39 import net.sourceforge.jnlp.ServerAccess;
40 import net.sourceforge.jnlp.browsertesting.BrowserTest;
41 import net.sourceforge.jnlp.browsertesting.Browsers;
42 import net.sourceforge.jnlp.closinglisteners.CountingClosingListener;
43 import net.sourceforge.jnlp.annotations.NeedsDisplay;
44 import net.sourceforge.jnlp.annotations.TestInBrowsers;
45 import org.junit.Assert;
46 
47 import org.junit.Test;
48 
49 public class JavascriptSetTest extends BrowserTest {
50 
51     public final boolean doNotRunInOpera = false;
52 
53     private final String initStr = "JToJSSet applet initialized.";
54     private final String afterStr = "afterTests";
55 
56     private class CountingClosingListenerImpl extends CountingClosingListener {
57 
58         @Override
isAlowedToFinish(String s)59         protected boolean isAlowedToFinish(String s) {
60             return (s.contains(initStr) && s.contains(afterStr));
61         }
62     }
63 
evaluateStdoutContents(String[] expectedStdoutsOR, ProcessResult pr)64     private void evaluateStdoutContents(String[] expectedStdoutsOR, ProcessResult pr) {
65         // Assert that the applet was initialized.
66         Assert.assertTrue("JToJSSetTest stdout should contain " + initStr + " but it didnt.", pr.stdout.contains(initStr));
67 
68         // Assert that the values set from JavaScript are ok
69         boolean atLeastOne = false;
70         for (String s : expectedStdoutsOR) {
71             if (pr.stdout.contains(s)) {
72                 atLeastOne = true;
73             }
74         }
75         Assert.assertTrue("JToJSSet: the output should include at least one of expected Stdouts, but it didnt.", atLeastOne);
76     }
77 
javaToJSSetTest(String urlEnd, String[] expectedValsOR)78     private void javaToJSSetTest(String urlEnd, String[] expectedValsOR) throws Exception {
79 
80         if (doNotRunInOpera) {
81             Browsers b = server.getCurrentBrowser().getID();
82             if (b == Browsers.opera) {
83                 return;
84             }
85         }
86 
87         String strURL = "/JavascriptSet.html?" + urlEnd;
88         ProcessResult pr = server.executeBrowser(strURL, new CountingClosingListenerImpl(), new CountingClosingListenerImpl());
89         evaluateStdoutContents(expectedValsOR, pr);
90     }
91 
92     @Test
93     @TestInBrowsers(testIn = { Browsers.all })
94     @NeedsDisplay
AppletJToJSSet_int_Test()95     public void AppletJToJSSet_int_Test() throws Exception {
96         javaToJSSetTest("jjsSetInt", new String[] { "1" });
97     }
98 
99     @Test
100     @TestInBrowsers(testIn = { Browsers.all })
101     @NeedsDisplay
AppletJToJSSet_Integer_Test()102     public void AppletJToJSSet_Integer_Test() throws Exception {
103         javaToJSSetTest("jjsSetInteger", new String[] { "2" });
104     }
105 
106     @Test
107     @TestInBrowsers(testIn = { Browsers.all })
108     @NeedsDisplay
AppletJToJSSet_double_Test()109     public void AppletJToJSSet_double_Test() throws Exception {
110         javaToJSSetTest("jjsSetdouble", new String[] { "2.5" });
111     }
112 
113     @Test
114     @TestInBrowsers(testIn = { Browsers.all })
115     @NeedsDisplay
AppletJToJSSet_Double_Test()116     public void AppletJToJSSet_Double_Test() throws Exception {
117         javaToJSSetTest("jjsSetDouble", new String[] { "2.5" });
118     }
119 
120     @Test
121     @TestInBrowsers(testIn = { Browsers.all })
122     @NeedsDisplay
AppletJToJSSet_float_Test()123     public void AppletJToJSSet_float_Test() throws Exception {
124         javaToJSSetTest("jjsSetfloat", new String[] { "2.5" }); // 2.3->2.2999...
125     }
126 
127     @Test
128     @TestInBrowsers(testIn = { Browsers.all })
129     @NeedsDisplay
AppletJToJSSet_Float_Test()130     public void AppletJToJSSet_Float_Test() throws Exception {
131         javaToJSSetTest("jjsSetFloat", new String[] { "2.5" });
132     }
133 
134     @Test
135     @TestInBrowsers(testIn = { Browsers.all })
136     @NeedsDisplay
AppletJToJSSet_long_Test()137     public void AppletJToJSSet_long_Test() throws Exception {
138         javaToJSSetTest("jjsSetlong", new String[] { "4294967296" });
139     }
140 
141     @Test
142     @TestInBrowsers(testIn = { Browsers.all })
143     @NeedsDisplay
AppletJToJSSet_Long_Test()144     public void AppletJToJSSet_Long_Test() throws Exception {
145         javaToJSSetTest("jjsSetLong", new String[] { "4294967297" });
146     }
147 
148     @Test
149     @TestInBrowsers(testIn = { Browsers.all })
150     @NeedsDisplay
AppletJToJSSet_short_Test()151     public void AppletJToJSSet_short_Test() throws Exception {
152         javaToJSSetTest("jjsSetshort", new String[] { "3" });
153     }
154 
155     @Test
156     @TestInBrowsers(testIn = { Browsers.all })
157     @NeedsDisplay
AppletJToJSSet_Short_Test()158     public void AppletJToJSSet_Short_Test() throws Exception {
159         javaToJSSetTest("jjsSetShort", new String[] { "4" });
160     }
161 
162     @Test
163     @TestInBrowsers(testIn = { Browsers.all })
164     @NeedsDisplay
AppletJToJSSet_byte_Test()165     public void AppletJToJSSet_byte_Test() throws Exception {
166         javaToJSSetTest("jjsSetbyte", new String[] { "5" });
167     }
168 
169     @Test
170     @TestInBrowsers(testIn = { Browsers.all })
171     @NeedsDisplay
AppletJToJSSet_Byte_Test()172     public void AppletJToJSSet_Byte_Test() throws Exception {
173         javaToJSSetTest("jjsSetByte", new String[] { "6" });
174     }
175 
176     @Test
177     @TestInBrowsers(testIn = { Browsers.all })
178     @NeedsDisplay
AppletJToJSSet_char_Test()179     public void AppletJToJSSet_char_Test() throws Exception {
180         javaToJSSetTest("jjsSetchar", new String[] { "97" }); // i.e. 'a'
181     }
182 
183     @Test
184     @TestInBrowsers(testIn = { Browsers.all })
185     @NeedsDisplay
AppletJToJSSet_Character_Test()186     public void AppletJToJSSet_Character_Test() throws Exception {
187         javaToJSSetTest("jjsSetCharacter", new String[] { "97" }); // i.e. 'a'
188     }
189 
190     @Test
191     @TestInBrowsers(testIn = { Browsers.all })
192     @NeedsDisplay
AppletJToJSSet_boolean_Test()193     public void AppletJToJSSet_boolean_Test() throws Exception {
194         javaToJSSetTest("jjsSetboolean", new String[] { "true" });
195     }
196 
197     @Test
198     @TestInBrowsers(testIn = { Browsers.all })
199     @NeedsDisplay
AppletJToJSSet_Boolean_Test()200     public void AppletJToJSSet_Boolean_Test() throws Exception {
201         javaToJSSetTest("jjsSetBoolean", new String[] { "true" });
202     }
203 
204     @Test
205     @TestInBrowsers(testIn = { Browsers.all })
206     @NeedsDisplay
AppletJToJSSet_String_Test()207     public void AppletJToJSSet_String_Test() throws Exception {
208         javaToJSSetTest("jjsSetString", new String[] { "��〒£$ǣ€��" });
209     }
210 
211     @Test
212     @TestInBrowsers(testIn = { Browsers.all })
213     @NeedsDisplay
AppletJToJSSet_object_Test()214     public void AppletJToJSSet_object_Test() throws Exception {
215         javaToJSSetTest("jjsSetObject", new String[] { "DummyObject2" });
216     }
217 
218     @Test
219     @TestInBrowsers(testIn = { Browsers.all })
220     @NeedsDisplay
AppletJToJSSet_1DArrayElement_Test()221     public void AppletJToJSSet_1DArrayElement_Test() throws Exception {
222         javaToJSSetTest("jjsSet1DArray", new String[] { "100" });
223     }
224 
225     @Test
226     @TestInBrowsers(testIn = { Browsers.all })
227     @NeedsDisplay
AppletJToJSSet_2DArrayElement_Test()228     public void AppletJToJSSet_2DArrayElement_Test() throws Exception {
229         javaToJSSetTest("jjsSet2DArray", new String[] { "200" });
230     }
231 }
232