imcplugin: Add automated tests
[nativeclient.git] / imcplugin / test.py
blobd75f1b4737367223f211e58cace96f7184cd7112
2 import os
3 import shutil
4 import signal
5 import subprocess
6 import tempfile
7 import unittest
10 HEREDIR = os.path.dirname(__file__)
13 def write_file(filename, data):
14 fh = open(filename, "w")
15 try:
16 fh.write(data)
17 finally:
18 fh.close()
21 class TempDirTestCase(unittest.TestCase):
23 def setUp(self):
24 self._on_teardown = []
26 def make_temp_dir(self):
27 temp_dir = tempfile.mkdtemp(prefix="tmp-%s-" % self.__class__.__name__)
28 def tear_down():
29 shutil.rmtree(temp_dir)
30 self._on_teardown.append(tear_down)
31 return temp_dir
33 def tearDown(self):
34 for func in reversed(self._on_teardown):
35 func()
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
46 # profile name.
47 write_file(os.path.join(home_dir, ".mozilla/firefox/profiles.ini"),
48 """
49 [General]
50 StartWithLastProfile=0
52 [Profile0]
53 Name=default
54 IsRelative=1
55 Path=default
56 """)
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)
69 try:
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()
74 if line == "":
75 raise AssertionError("Output ended too soon")
76 # Ignore noise from Mozilla.
77 if line.startswith("*** e = [Exception"):
78 continue
79 self.assertEquals(line.rstrip("\n"), expect.pop(0))
80 finally:
81 os.kill(proc.pid, signal.SIGKILL)
82 proc.wait()
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"""
88 Testing plugin...
89 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
90 <script type="text/javascript">
91 function onload() {
92 try {
93 var plugin = document.getElementById("plugin");
94 plugin.get_file("test-file", function(file) {
95 dump("got file\n");
96 dump("END\n");
97 });
99 catch(e) {
100 dump(e + "\n");
103 window.onload = onload;
104 </script>
105 """)
106 self._run_firefox(os.path.join(html_dir, "test.html"),
107 ["got file"])
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"""
114 Testing plugin...
115 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
116 <script type="text/javascript">
117 function callback() {}
118 function onload() {
119 try {
120 var plugin = document.getElementById("plugin");
121 plugin.get_file("printargs", function(file) {
122 plugin.launch(file, ["arg 1 foo", "arg 2 baz"], callback);
125 catch(e) {
126 dump(e + "\n");
129 window.onload = onload;
130 </script>
131 """)
132 expect = ["NaCl module started",
133 "argc = 3",
134 'argv[0] = "NaClMain"',
135 'argv[1] = "arg 1 foo"',
136 'argv[2] = "arg 2 baz"',
137 "PASS"]
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"""
145 Testing plugin...
146 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
147 <script type="text/javascript">
148 function callback() {}
149 function onload() {
150 try {
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", []);
158 catch(e) {
159 dump(e + "\n");
162 window.onload = onload;
163 </script>
164 """)
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"""
176 Testing plugin...
177 <object id="plugin" type="application/x-nacl-imc">Plugin not working</object>
178 <script type="text/javascript">
179 function onload() {
180 try {
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);
189 catch(e) {
190 dump(e + "\n");
193 window.onload = onload;
194 </script>
195 """)
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__":
202 unittest.main()