aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-12-02 16:17:23 -0500
committerAlon Zakai <alonzakai@gmail.com>2013-12-02 16:17:23 -0500
commita42f659391fcbd0b85b29e5b8725b48a364eb20e (patch)
tree1bdf2e538a4bc630dbb39bfac40a3dea7369b671
parent10b92e403dd2128fb3df40658eb88bbc7c7bd517 (diff)
fix file preloading in workers and add test
-rw-r--r--tests/hello_world_worker.cpp10
-rw-r--r--tests/test_browser.py15
-rw-r--r--tools/file_packager.py8
3 files changed, 26 insertions, 7 deletions
diff --git a/tests/hello_world_worker.cpp b/tests/hello_world_worker.cpp
index 5ea26d91..5b673df8 100644
--- a/tests/hello_world_worker.cpp
+++ b/tests/hello_world_worker.cpp
@@ -1,9 +1,17 @@
+#include <string.h>
#include <stdio.h>
#include <emscripten.h>
int main()
{
printf("you should not see this text when in a worker!\n"); // this should not crash, but also should not show up anywhere if you are in a worker
- emscripten_run_script("if (typeof postMessage !== 'undefined') { postMessage('hello from worker!') }");
+ FILE *f = fopen("file.dat", "r");
+ char buffer[100];
+ memset(buffer, 0, 100);
+ buffer[0] = 0;
+ fread(buffer, 10, 1, f);
+ char buffer2[100];
+ sprintf(buffer2, "if (typeof postMessage !== 'undefined') { postMessage('hello from worker, and |%s|') }", buffer);
+ emscripten_run_script(buffer2);
}
diff --git a/tests/test_browser.py b/tests/test_browser.py
index b2cd62f8..d0618af8 100644
--- a/tests/test_browser.py
+++ b/tests/test_browser.py
@@ -1218,10 +1218,7 @@ keydown(100);keyup(100); // trigger the end
def test_worker(self):
# Test running in a web worker
- output = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'], stdout=PIPE, stderr=PIPE).communicate()
- assert len(output[0]) == 0, output[0]
- assert os.path.exists('worker.js'), output
- self.assertContained('you should not see this text when in a worker!', run_js('worker.js')) # code should run standalone
+ open('file.dat', 'w').write('data for worker')
html_file = open('main.html', 'w')
html_file.write('''
<html>
@@ -1240,7 +1237,15 @@ keydown(100);keyup(100); // trigger the end
</html>
''')
html_file.close()
- self.run_browser('main.html', 'You should see that the worker was called, and said "hello from worker!"', '/report_result?hello%20from%20worker!')
+
+ # no file data
+ for file_data in [0, 1]:
+ print 'file data', file_data
+ output = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'] + (['--preload-file', 'file.dat'] if file_data else []) , stdout=PIPE, stderr=PIPE).communicate()
+ assert len(output[0]) == 0, output[0]
+ assert os.path.exists('worker.js'), output
+ if not file_data: self.assertContained('you should not see this text when in a worker!', run_js('worker.js')) # code should run standalone
+ self.run_browser('main.html', '', '/report_result?hello%20from%20worker,%20and%20|' + ('data%20for%20w' if file_data else '') + '|')
def test_chunked_synchronous_xhr(self):
main = 'chunked_sync_xhr.html'
diff --git a/tools/file_packager.py b/tools/file_packager.py
index 3ba5b23f..7d9344cd 100644
--- a/tools/file_packager.py
+++ b/tools/file_packager.py
@@ -453,7 +453,13 @@ if has_preloaded:
package_uuid = uuid.uuid4();
remote_package_name = os.path.basename(Compression.compressed_name(data_target) if Compression.on else data_target)
code += r'''
- var PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
+ var PACKAGE_PATH;
+ if (typeof window === 'object') {
+ PACKAGE_PATH = window['encodeURIComponent'](window.location.pathname.toString().substring(0, window.location.pathname.toString().lastIndexOf('/')) + '/');
+ } else {
+ // worker
+ PACKAGE_PATH = encodeURIComponent(location.pathname.toString().substring(0, location.pathname.toString().lastIndexOf('/')) + '/');
+ }
var PACKAGE_NAME = '%s';
var REMOTE_PACKAGE_NAME = '%s';
var PACKAGE_UUID = '%s';