Skip to content

Commit c78dfce

Browse files
committed
Move the goog.exportSymbol statements in javascript/webdriver/atoms/inputs.js to a separate file.
These statements would prevent the exported symbols from being removed by the Closure compiler in any target that included inputs.js. We only one to keep the symbols for //javascript/webdriver/atoms:inputs
1 parent 5462098 commit c78dfce

File tree

8 files changed

+185
-41
lines changed

8 files changed

+185
-41
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.openqa.selenium.atoms;
2+
3+
import org.junit.runner.RunWith;
4+
import org.junit.runners.Suite;
5+
6+
/**
7+
* All rhino-based tests for the browser atoms.
8+
*/
9+
@RunWith(Suite.class)
10+
@Suite.SuiteClasses({
11+
CompiledAtomsNotLeakingTest.class,
12+
InputAtomsTest.class
13+
})
14+
public class AtomsRhinoTests {
15+
}

java/client/test/org/openqa/selenium/atoms/CompiledAtomsNotLeakingTest.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,28 @@
2020

2121
import static org.junit.Assert.assertEquals;
2222

23-
import org.openqa.selenium.Build;
24-
import org.openqa.selenium.testing.InProject;
25-
26-
import com.google.common.base.Charsets;
27-
import com.google.common.io.Files;
28-
import com.google.common.io.Resources;
2923
import net.sourceforge.htmlunit.corejs.javascript.Context;
3024
import net.sourceforge.htmlunit.corejs.javascript.ContextAction;
3125
import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
3226
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
27+
3328
import org.json.JSONException;
3429
import org.json.JSONObject;
3530
import org.junit.Before;
3631
import org.junit.BeforeClass;
3732
import org.junit.Test;
3833
import org.junit.runner.RunWith;
39-
import org.junit.runners.BlockJUnit4ClassRunner;
34+
import org.junit.runners.JUnit4;
4035

41-
import java.io.File;
4236
import java.io.IOException;
43-
import java.net.URL;
4437

45-
@RunWith(BlockJUnit4ClassRunner.class)
38+
@RunWith(JUnit4.class)
4639
public class CompiledAtomsNotLeakingTest {
4740

48-
private static final String FRAGMENT_TASK = "//javascript/webdriver/atoms:execute_script";
41+
private static final String FRAGMENT_TASK =
42+
"//javascript/atoms/fragments:execute_script";
4943
private static final String FRAGMENT_PATH =
50-
"build/javascript/webdriver/atoms/execute_script.js";
44+
JavaScriptLoader.taskToBuildOutput(FRAGMENT_TASK);
5145
private static final String RESOURCE_PATH = "/org/openqa/selenium/atoms/execute_script.js";
5246

5347
private static String fragment;
@@ -56,22 +50,13 @@ public class CompiledAtomsNotLeakingTest {
5650

5751
@BeforeClass
5852
public static void loadFragment() throws IOException {
59-
URL resourceUrl = CompiledAtomsNotLeakingTest.class.getResource(RESOURCE_PATH);
60-
if (resourceUrl != null) {
61-
fragment = Resources.toString(resourceUrl, Charsets.UTF_8);
62-
} else {
63-
File topDir = InProject.locate("Rakefile").getParentFile();
64-
File atomFile = new File(topDir, FRAGMENT_PATH);
65-
if (!atomFile.exists()) {
66-
new Build().of(FRAGMENT_TASK).go();
67-
}
68-
fragment = Files.toString(atomFile, Charsets.UTF_8);
69-
}
53+
fragment = JavaScriptLoader.loadResource(RESOURCE_PATH, FRAGMENT_TASK);
7054
}
7155

7256
@Before
7357
public void prepareGlobalObject() {
7458
ContextFactory.getGlobal().call(new ContextAction() {
59+
@Override
7560
public Object run(Context context) {
7661
global = context.initStandardObjects();
7762
global.defineProperty("_", 1234, ScriptableObject.EMPTY);
@@ -95,6 +80,7 @@ public Object run(Context context) {
9580
@Test
9681
public void fragmentWillNotLeakVariablesToEnclosingScopes() {
9782
ContextFactory.getGlobal().call(new ContextAction() {
83+
@Override
9884
public Object run(Context context) {
9985
eval(context, "(" + fragment + ")()", FRAGMENT_PATH);
10086
assertEquals(1234, eval(context, "_"));
@@ -121,6 +107,7 @@ public Object run(Context context) {
121107
@Test
122108
public void nestedFragmentsShouldNotLeakVariables() {
123109
ContextFactory.getGlobal().call(new ContextAction() {
110+
@Override
124111
public Object run(Context context) {
125112
// executeScript atom recursing on itself to execute "return 1+2".
126113
// Should result in {status:0,value:{status:0,value:3}}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.openqa.selenium.atoms;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import net.sourceforge.htmlunit.corejs.javascript.Context;
6+
import net.sourceforge.htmlunit.corejs.javascript.ContextAction;
7+
import net.sourceforge.htmlunit.corejs.javascript.ContextFactory;
8+
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
9+
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.junit.runners.JUnit4;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* Sanity tests against the //javascript/webdriver/atoms:inputs target.
18+
*/
19+
@RunWith(JUnit4.class)
20+
public class InputAtomsTest {
21+
22+
private static final String RESOURCE_PATH = "/org/openqa/selenium/atoms/atoms_inputs.js";
23+
private static final String RESOURCE_TASK = "//javascript/webdriver/atoms:inputs";
24+
25+
@Test
26+
public void exportsTheExpectedNames() throws IOException {
27+
final String source = JavaScriptLoader.loadResource(RESOURCE_PATH, RESOURCE_TASK);
28+
ContextFactory.getGlobal().call(new ContextAction() {
29+
private ScriptableObject global;
30+
31+
@Override
32+
public Object run(Context context) {
33+
global = context.initStandardObjects();
34+
35+
// Check assumptions abut the global context, which the atoms assumes is a DOM window.
36+
assertEquals(global, eval(context, "this.window=this;"));
37+
assertEquals(global, eval(context, "this"));
38+
assertEquals(global, eval(context, "window"));
39+
assertEquals(true, eval(context, "this === window"));
40+
41+
eval(context, source, JavaScriptLoader.taskToBuildOutput(RESOURCE_TASK));
42+
43+
assertFunction(context, "webdriver.atoms.inputs.sendKeys");
44+
assertFunction(context, "webdriver.atoms.inputs.click");
45+
assertFunction(context, "webdriver.atoms.inputs.mouseMove");
46+
assertFunction(context, "webdriver.atoms.inputs.mouseButtonDown");
47+
assertFunction(context, "webdriver.atoms.inputs.mouseButtonUp");
48+
assertFunction(context, "webdriver.atoms.inputs.doubleClick");
49+
assertFunction(context, "webdriver.atoms.inputs.rightClick");
50+
51+
return null;
52+
}
53+
54+
private void assertFunction(Context context, String property) {
55+
assertEquals(
56+
"Expected " + property + " to be a function",
57+
"function",
58+
eval(context, "typeof " + property));
59+
}
60+
61+
@SuppressWarnings({"unchecked"})
62+
private <T> T eval(Context context, String script) {
63+
return (T) eval(context, script, "");
64+
}
65+
66+
@SuppressWarnings({"unchecked"})
67+
private <T> T eval(Context context, String script, String src) {
68+
return (T) context.evaluateString(global, script, src, 1, null);
69+
}
70+
});
71+
}
72+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.openqa.selenium.atoms;
2+
3+
import com.google.common.base.Charsets;
4+
import com.google.common.io.Files;
5+
import com.google.common.io.Resources;
6+
7+
import org.openqa.selenium.Build;
8+
import org.openqa.selenium.testing.InProject;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.net.URL;
13+
14+
/**
15+
* Utility class for loading JavaScript resources.
16+
*/
17+
class JavaScriptLoader {
18+
private JavaScriptLoader() {} // Utility class.
19+
20+
static String loadResource(String resourcePath, String resourceTask) throws IOException {
21+
URL resourceUrl = JavaScriptLoader.class.getResource(resourcePath);
22+
if (resourceUrl != null) {
23+
return Resources.toString(resourceUrl, Charsets.UTF_8);
24+
} else {
25+
new Build().of(resourceTask).go();
26+
27+
File topDir = InProject.locate("Rakefile").getParentFile();
28+
File builtFile = new File(topDir, taskToBuildOutput(resourceTask));
29+
return Files.toString(builtFile, Charsets.UTF_8);
30+
}
31+
}
32+
33+
static String taskToBuildOutput(String taskName) {
34+
return taskName.replace("//", "build/") .replace(":", "/") + ".js";
35+
}
36+
}

java/client/test/org/openqa/selenium/atoms/build.desc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@ rename(
55
"//javascript/atoms/fragments:execute_script.js",
66
])
77

8+
rename(
9+
name = "atoms_inputs",
10+
out = "atoms_inputs.js",
11+
deps = [
12+
"//javascript/webdriver/atoms:inputs.js",
13+
])
14+
815

916
java_test(name = "test",
1017
srcs = [ "*.java" ],
11-
test_suite = "org.openqa.selenium.atoms.CompiledAtomsNotLeakingTest",
18+
test_suite = "org.openqa.selenium.atoms.AtomsRhinoTests",
1219
embedded = [
1320
":execute_script",
21+
":atoms_inputs",
1422
],
1523
deps = [
1624
"//javascript/atoms/fragments:execute_script",

javascript/webdriver/atoms/build.desc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ js_deps(name = "deps",
99
])
1010

1111
js_binary(name = "inputs",
12-
srcs = [ "inputs.js" ],
12+
srcs = [
13+
"inputs.js",
14+
"exports/inputs.js",
15+
],
1316
flags = [
14-
"--compilation_level=ADVANCED_OPTIMIZATIONS",
17+
"--compilation_level=ADVANCED_OPTIMIZATIONS",
1518
],
1619
no_format = "true",
1720
deps = [
18-
":deps",
21+
":deps",
1922
])
2023

2124
js_library(name = "atoms_lib",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2013 WebDriver committers
2+
// Copyright 2013 Software Freedom Conservancy
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* @fileoverview Exports various symbols from the webdriver.atoms.inputs
18+
* namespace.
19+
*/
20+
21+
goog.require('webdriver.atoms.inputs');
22+
23+
24+
goog.exportSymbol('webdriver.atoms.inputs.click',
25+
webdriver.atoms.inputs.click);
26+
goog.exportSymbol('webdriver.atoms.inputs.doubleClick',
27+
webdriver.atoms.inputs.doubleClick);
28+
goog.exportSymbol('webdriver.atoms.inputs.rightClick',
29+
webdriver.atoms.inputs.rightClick);
30+
goog.exportSymbol('webdriver.atoms.inputs.mouseButtonDown',
31+
webdriver.atoms.inputs.mouseButtonDown);
32+
goog.exportSymbol('webdriver.atoms.inputs.mouseButtonUp',
33+
webdriver.atoms.inputs.mouseButtonUp);
34+
goog.exportSymbol('webdriver.atoms.inputs.mouseMove',
35+
webdriver.atoms.inputs.mouseMove);
36+
goog.exportSymbol('webdriver.atoms.inputs.sendKeys',
37+
webdriver.atoms.inputs.sendKeys);

javascript/webdriver/atoms/inputs.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ webdriver.atoms.inputs.sendKeys = function(
5454

5555
return keyboard.getState();
5656
};
57-
goog.exportSymbol('webdriver.atoms.inputs.sendKeys',
58-
webdriver.atoms.inputs.sendKeys);
5957

6058

6159
/**
@@ -76,8 +74,6 @@ webdriver.atoms.inputs.click = function(element, opt_state) {
7674
bot.action.click(element, null, mouse);
7775
return mouse.getState();
7876
};
79-
goog.exportSymbol('webdriver.atoms.inputs.click',
80-
webdriver.atoms.inputs.click);
8177

8278

8379
/**
@@ -125,8 +121,6 @@ webdriver.atoms.inputs.mouseMove = function(element, x_offset, y_offset,
125121
mouse.move(target, coords);
126122
return mouse.getState();
127123
};
128-
goog.exportSymbol('webdriver.atoms.inputs.mouseMove',
129-
webdriver.atoms.inputs.mouseMove);
130124

131125

132126
/**
@@ -140,8 +134,6 @@ webdriver.atoms.inputs.mouseButtonDown = function(opt_state) {
140134
mouse.pressButton(bot.Mouse.Button.LEFT);
141135
return mouse.getState();
142136
};
143-
goog.exportSymbol('webdriver.atoms.inputs.mouseButtonDown',
144-
webdriver.atoms.inputs.mouseButtonDown);
145137

146138

147139
/**
@@ -155,8 +147,6 @@ webdriver.atoms.inputs.mouseButtonUp = function(opt_state) {
155147
mouse.releaseButton();
156148
return mouse.getState();
157149
};
158-
goog.exportSymbol('webdriver.atoms.inputs.mouseButtonUp',
159-
webdriver.atoms.inputs.mouseButtonUp);
160150

161151

162152
/**
@@ -173,8 +163,6 @@ webdriver.atoms.inputs.doubleClick = function(opt_state) {
173163
mouse.releaseButton();
174164
return mouse.getState();
175165
};
176-
goog.exportSymbol('webdriver.atoms.inputs.doubleClick',
177-
webdriver.atoms.inputs.doubleClick);
178166

179167

180168
/**
@@ -189,5 +177,3 @@ webdriver.atoms.inputs.rightClick = function(opt_state) {
189177
mouse.releaseButton();
190178
return mouse.getState();
191179
};
192-
goog.exportSymbol('webdriver.atoms.inputs.rightClick',
193-
webdriver.atoms.inputs.rightClick);

0 commit comments

Comments
 (0)