10 HEREDIR
= os
.path
.dirname(__file__
)
13 def write_file(filename
, data
):
14 fh
= open(filename
, "w")
21 class TempDirTestCase(unittest
.TestCase
):
24 self
._on
_teardown
= []
26 def make_temp_dir(self
):
27 temp_dir
= tempfile
.mkdtemp(prefix
="tmp-%s-" % self
.__class
__.__name
__)
29 shutil
.rmtree(temp_dir
)
30 self
._on
_teardown
.append(tear_down
)
34 for func
in reversed(self
._on
_teardown
):
38 class ImcPluginTests(TempDirTestCase
):
40 def _run_firefox(self
, html_file
, expect
):
41 home_dir
= self
.make_temp_dir()
42 os
.makedirs(os
.path
.join(home_dir
, ".mozilla/plugins"))
43 os
.makedirs(os
.path
.join(home_dir
, ".mozilla/firefox/default"))
44 # This profiles.ini file tells Mozilla to use the "default"
45 # profile with our user.js file, rather than making up a new
47 write_file(os
.path
.join(home_dir
, ".mozilla/firefox/profiles.ini"),
50 StartWithLastProfile=0
57 write_file(os
.path
.join(home_dir
, ".mozilla/firefox/default/user.js"),
58 'user_pref("browser.dom.window.dump.enabled", true);\n')
59 shutil
.copy(os
.path
.join(HEREDIR
, "plugin.so"),
60 os
.path
.join(home_dir
, ".mozilla/plugins/plugin.so"))
61 env
= os
.environ
.copy()
62 # Reusing current X display. TODO: launch a scratch Xvfb.
63 if "XAUTHORITY" not in env
:
64 env
["XAUTHORITY"] = os
.path
.expanduser("~/.Xauthority")
65 env
["HOME"] = home_dir
67 proc
= subprocess
.Popen(["firefox", html_file
],
68 env
=env
, stdout
=subprocess
.PIPE
)
70 while len(expect
) > 0:
71 # TODO: add a timeout. Unfortunately we can't really
72 # tell if the test has failed.
73 line
= proc
.stdout
.readline()
75 raise AssertionError("Output ended too soon")
76 # Ignore noise from Mozilla.
77 if line
.startswith("*** e = [Exception"):
79 self
.assertEquals(line
.rstrip("\n"), expect
.pop(0))
81 os
.kill(proc
.pid
, signal
.SIGKILL
)
84 def test_getting_file(self
):
85 html_dir
= self
.make_temp_dir()
86 write_file(os
.path
.join(html_dir
, "test-file"), "blah blah")
87 write_file(os
.path
.join(html_dir
, "test.html"), r
"""
89 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
90 <script type="text/javascript">
93 var plugin = document.getElementById("plugin");
94 plugin.get_file("test-file", function(file) {
103 window.onload = onload;
106 self
._run
_firefox
(os
.path
.join(html_dir
, "test.html"),
109 def test_launching_nacl_process(self
):
110 html_dir
= self
.make_temp_dir()
111 shutil
.copy(os
.path
.join(HEREDIR
, "tests/printargs"),
112 os
.path
.join(html_dir
, "printargs"))
113 write_file(os
.path
.join(html_dir
, "test.html"), r
"""
115 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
116 <script type="text/javascript">
117 function callback() {}
120 var plugin = document.getElementById("plugin");
121 plugin.get_file("printargs", function(file) {
122 plugin.launch(file, ["arg 1 foo", "arg 2 baz"], callback);
129 window.onload = onload;
132 expect
= ["NaCl module started",
134 'argv[0] = "NaClMain"',
135 'argv[1] = "arg 1 foo"',
136 'argv[2] = "arg 2 baz"',
138 self
._run
_firefox
(os
.path
.join(html_dir
, "test.html"), expect
)
140 def test_sending_to_nacl(self
):
141 html_dir
= self
.make_temp_dir()
142 shutil
.copy(os
.path
.join(HEREDIR
, "tests/imc-read"),
143 os
.path
.join(html_dir
, "imc-read"))
144 write_file(os
.path
.join(html_dir
, "test.html"), r
"""
146 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
147 <script type="text/javascript">
148 function callback() {}
151 var plugin = document.getElementById("plugin");
152 plugin.get_file("imc-read", function(file) {
153 var proc = plugin.launch(file, [], callback);
154 proc.send("Hello from Javascript to NaCl\n", []);
155 proc.send("Second message\n", []);
162 window.onload = onload;
165 expect
= ["NaCl got message: Hello from Javascript to NaCl",
166 "NaCl received 0 FDs",
167 "NaCl got message: Second message",
168 "NaCl received 0 FDs"]
169 self
._run
_firefox
(os
.path
.join(html_dir
, "test.html"), expect
)
171 def test_receiving_from_nacl(self
):
172 html_dir
= self
.make_temp_dir()
173 shutil
.copy(os
.path
.join(HEREDIR
, "tests/imc-send"),
174 os
.path
.join(html_dir
, "imc-send"))
175 write_file(os
.path
.join(html_dir
, "test.html"), r
"""
177 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
178 <script type="text/javascript">
181 var plugin = document.getElementById("plugin");
182 function callback(msg) {
183 dump("Javascript got message: " + msg + "\n");
185 plugin.get_file("imc-send", function(file) {
186 var proc = plugin.launch(file, [], callback);
193 window.onload = onload;
196 expect
= ["Javascript got message: Hello from NaCl to Javascript!",
197 "Javascript got message: Message 2"]
198 self
._run
_firefox
(os
.path
.join(html_dir
, "test.html"), expect
)
201 if __name__
== "__main__":