Skip to content

Commit f730900

Browse files
author
Vasil Hristov
authored
Add worker PostMessage data test. (#250)
1 parent 4175a57 commit f730900

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
In NativeScript, a file with the same name as an XML file is known as
3+
a code-behind file. The code-behind is a great place to place your view
4+
logic, and to set up your page’s data binding.
5+
*/
6+
7+
/*
8+
NativeScript adheres to the CommonJS specification for dealing with
9+
JavaScript modules. The CommonJS require() function is how you import
10+
JavaScript modules defined in other files.
11+
*/
12+
const createViewModel = require("./main-view-model").createViewModel;
13+
14+
function onNavigatingTo(args) {
15+
/*
16+
This gets a reference this page’s <Page> UI component. You can
17+
view the API reference of the Page to see what’s available at
18+
https://docs.nativescript.org/api-reference/classes/_ui_page_.page.html
19+
*/
20+
const page = args.object;
21+
22+
/*
23+
A page’s bindingContext is an object that should be used to perform
24+
data binding between XML markup and JavaScript code. Properties
25+
on the bindingContext can be accessed using the {{ }} syntax in XML.
26+
In this example, the {{ message }} and {{ onTap }} bindings are resolved
27+
against the object returned by createViewModel().
28+
29+
You can learn more about data binding in NativeScript at
30+
https://docs.nativescript.org/core-concepts/data-binding.
31+
*/
32+
page.bindingContext = createViewModel();
33+
}
34+
35+
let worker;
36+
if (global.TNS_WEBPACK) {
37+
const testWorker = require("nativescript-worker-loader!./worker.js");
38+
worker = new testWorker();
39+
} else {
40+
worker = new Worker("./worker.js");
41+
}
42+
worker.onmessage = (msg) => {
43+
console.dir(msg.data);
44+
};
45+
worker.onerror = function(err) {
46+
console.log("Worker Error: ", err, err.stack);
47+
};
48+
/*
49+
Exporting a function in a NativeScript code-behind file makes it accessible
50+
to the file’s corresponding XML file. In this case, exporting the onNavigatingTo
51+
function here makes the navigatingTo="onNavigatingTo" binding in this page’s XML
52+
file work.
53+
*/
54+
exports.onNavigatingTo = onNavigatingTo;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require ("tns-core-modules/globals/globals");
2+
// Give NS a couple seconds to get everything up on main thread...
3+
setTimeout( () => { startWorker(); }, 3000);
4+
function startWorker() {
5+
let r1 = {blah: "a"}; // This is our row data...
6+
let d = {Hi: "Hi", table1: [r1, r1], table2: [r1]}; // This is our final data...
7+
global.postMessage({id: 1, data: d, data2: JSON.stringify(d)}); // Send it on thru
8+
}

tests/runtimes/android/android_runtime_tests.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,48 @@ def test_446_test_print_stack_trace_in_release_and_debug(self):
531531
timeout=15, period=5)
532532
assert test_result, 'Error message in tns log cat should be shown! Logs:' + log_cat
533533

534+
def test_447_test_worker_post_message(self):
535+
"""
536+
https://github.com/NativeScript/android-runtime/issues/1408
537+
"""
538+
539+
source_js = os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files', 'android-runtime-1408',
540+
'main-page.js')
541+
target_js = os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'main-page.js')
542+
File.copy(source=source_js, target=target_js, backup_files=True)
543+
source_js = os.path.join(TEST_RUN_HOME, 'assets', 'runtime', 'android', 'files', 'android-runtime-1408',
544+
'worker.js')
545+
target_js = os.path.join(TEST_RUN_HOME, APP_NAME, 'app', 'worker.js')
546+
File.copy(source=source_js, target=target_js, backup_files=True)
547+
log = Tns.run_android(APP_NAME, device=self.emulator.id, wait=False, verify=False)
548+
correct_object = """JS: ==== object dump start ====
549+
JS: id: "1"
550+
JS: data: {
551+
JS: "Hi": "Hi",
552+
JS: "table1": [
553+
JS: {
554+
JS: "blah": "a"
555+
JS: },
556+
JS: {
557+
JS: "blah": "a"
558+
JS: }
559+
JS: ],
560+
JS: "table2": [
561+
JS: {
562+
JS: "blah": "a"
563+
JS: }
564+
JS: ]
565+
JS: }
566+
JS: data2: "{"Hi":"Hi","table1":[{"blah":"a"},{"blah":"a"}],"table2":[{"blah":"a"}]}"
567+
JS: ==== object dump end ===="""
568+
strings = ['Successfully synced application', 'Successfully installed on device with identifier']
569+
570+
test_result = Wait.until(lambda: all(string in File.read(log.log_file) for string in strings), timeout=300,
571+
period=5)
572+
assert test_result, 'Build was not successful! Logs:' + File.read(log.log_file)
573+
test_result = Wait.until(lambda: correct_object in File.read(log.log_file), timeout=30, period=5)
574+
assert test_result, 'Worker PostMessage Data is not correct! Logs:' + File.read(log.log_file)
575+
534576
def test_500_test_ES6_support(self):
535577
"""
536578
https://github.com/NativeScript/android-runtime/issues/1375

0 commit comments

Comments
 (0)