|
1 /* vim: set ts=2 et sw=2 tw=80: */ |
|
2 /* Any copyright is dedicated to the Public Domain. |
|
3 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
4 |
|
5 function test() |
|
6 { |
|
7 waitForExplicitFinish(); |
|
8 |
|
9 gBrowser.selectedTab = gBrowser.addTab(); |
|
10 gBrowser.selectedBrowser.addEventListener("load", function onLoad() { |
|
11 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); |
|
12 openScratchpad(runTests); |
|
13 }, true); |
|
14 |
|
15 content.location = "data:text/html,test context switch in Scratchpad"; |
|
16 } |
|
17 |
|
18 |
|
19 function runTests() |
|
20 { |
|
21 let sp = gScratchpadWindow.Scratchpad; |
|
22 let contentMenu = gScratchpadWindow.document.getElementById("sp-menu-content"); |
|
23 let chromeMenu = gScratchpadWindow.document.getElementById("sp-menu-browser"); |
|
24 let notificationBox = sp.notificationBox; |
|
25 |
|
26 ok(contentMenu, "found #sp-menu-content"); |
|
27 ok(chromeMenu, "found #sp-menu-browser"); |
|
28 ok(notificationBox, "found Scratchpad.notificationBox"); |
|
29 |
|
30 let tests = [{ |
|
31 method: "run", |
|
32 prepare: function() { |
|
33 sp.setContentContext(); |
|
34 |
|
35 is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT, |
|
36 "executionContext is content"); |
|
37 |
|
38 is(contentMenu.getAttribute("checked"), "true", |
|
39 "content menuitem is checked"); |
|
40 |
|
41 isnot(chromeMenu.getAttribute("checked"), "true", |
|
42 "chrome menuitem is not checked"); |
|
43 |
|
44 ok(!notificationBox.currentNotification, |
|
45 "there is no notification in content context"); |
|
46 |
|
47 sp.editor.setText("window.foobarBug636725 = 'aloha';"); |
|
48 |
|
49 ok(!content.wrappedJSObject.foobarBug636725, |
|
50 "no content.foobarBug636725"); |
|
51 }, |
|
52 then: function() { |
|
53 is(content.wrappedJSObject.foobarBug636725, "aloha", |
|
54 "content.foobarBug636725 has been set"); |
|
55 } |
|
56 }, |
|
57 { |
|
58 method: "run", |
|
59 prepare: function() { |
|
60 sp.setBrowserContext(); |
|
61 |
|
62 is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_BROWSER, |
|
63 "executionContext is chrome"); |
|
64 |
|
65 is(chromeMenu.getAttribute("checked"), "true", |
|
66 "chrome menuitem is checked"); |
|
67 |
|
68 isnot(contentMenu.getAttribute("checked"), "true", |
|
69 "content menuitem is not checked"); |
|
70 |
|
71 ok(notificationBox.currentNotification, |
|
72 "there is a notification in browser context"); |
|
73 |
|
74 let [ from, to ] = sp.editor.getPosition(31, 32); |
|
75 sp.editor.replaceText("2'", from, to); |
|
76 |
|
77 is(sp.getText(), "window.foobarBug636725 = 'aloha2';", |
|
78 "setText() worked"); |
|
79 }, |
|
80 then: function() { |
|
81 is(window.foobarBug636725, "aloha2", |
|
82 "window.foobarBug636725 has been set"); |
|
83 |
|
84 delete window.foobarBug636725; |
|
85 ok(!window.foobarBug636725, "no window.foobarBug636725"); |
|
86 } |
|
87 }, |
|
88 { |
|
89 method: "run", |
|
90 prepare: function() { |
|
91 sp.editor.replaceText("gBrowser", sp.editor.getPosition(7)); |
|
92 |
|
93 is(sp.getText(), "window.gBrowser", |
|
94 "setText() worked with no end for the replace range"); |
|
95 }, |
|
96 then: function([, , result]) { |
|
97 is(result.class, "XULElement", |
|
98 "chrome context has access to chrome objects"); |
|
99 } |
|
100 }, |
|
101 { |
|
102 method: "run", |
|
103 prepare: function() { |
|
104 // Check that the sandbox is cached. |
|
105 sp.editor.setText("typeof foobarBug636725cache;"); |
|
106 }, |
|
107 then: function([, , result]) { |
|
108 is(result, "undefined", "global variable does not exist"); |
|
109 } |
|
110 }, |
|
111 { |
|
112 method: "run", |
|
113 prepare: function() { |
|
114 sp.editor.setText("var foobarBug636725cache = 'foo';" + |
|
115 "typeof foobarBug636725cache;"); |
|
116 }, |
|
117 then: function([, , result]) { |
|
118 is(result, "string", |
|
119 "global variable exists across two different executions"); |
|
120 } |
|
121 }, |
|
122 { |
|
123 method: "run", |
|
124 prepare: function() { |
|
125 sp.editor.setText("var foobarBug636725cache2 = 'foo';" + |
|
126 "typeof foobarBug636725cache2;"); |
|
127 }, |
|
128 then: function([, , result]) { |
|
129 is(result, "string", |
|
130 "global variable exists across two different executions"); |
|
131 } |
|
132 }, |
|
133 { |
|
134 method: "run", |
|
135 prepare: function() { |
|
136 sp.setContentContext(); |
|
137 |
|
138 is(sp.executionContext, gScratchpadWindow.SCRATCHPAD_CONTEXT_CONTENT, |
|
139 "executionContext is content"); |
|
140 |
|
141 sp.editor.setText("typeof foobarBug636725cache2;"); |
|
142 }, |
|
143 then: function([, , result]) { |
|
144 is(result, "undefined", |
|
145 "global variable no longer exists after changing the context"); |
|
146 } |
|
147 }]; |
|
148 |
|
149 runAsyncCallbackTests(sp, tests).then(() => { |
|
150 sp.setBrowserContext(); |
|
151 sp.editor.setText("delete foobarBug636725cache;" + |
|
152 "delete foobarBug636725cache2;"); |
|
153 sp.run().then(finish); |
|
154 }); |
|
155 } |