[Author: ace]
[google-gears.git] / gears / test / testcases / workerpool_createworkerfromurl_tests.js
blobadf6ca44aa9951b7937212017d7ff107d026e7bd
1 // Copyright 2007, Google Inc.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 var currentUrl = location.href;
28 var sameOriginPath =
29 currentUrl.substring(0, 1 + currentUrl.lastIndexOf('/'));
30 var sameOriginWorkerFile = '../testcases/workerpool_same_origin.js';
32 var crossOriginPath =
33 'http://google-gears.googlecode.com/svn/trunk/gears/test/testcases/';
34 var crossOriginWorkerFile = 'workerpool_cross_origin.js';
35 var crossOriginWorkerFileNoPerms = 'workerpool_same_origin.js';
37 function testCreateWorkerFromUrl1() {
38 startAsync();
40 var wp = google.gears.factory.create('beta.workerpool');
41 wp.onmessage = function(text, sender, m) {
42 completeAsync();
44 var childId = wp.createWorkerFromUrl(sameOriginWorkerFile);
45 wp.sendMessage('PING1', childId);
48 function testCreateWorkerFromUrl2() {
49 startAsync();
51 // Cleanup any local DB before starting test.
52 var db = google.gears.factory.create('beta.database');
53 db.open('worker_js');
54 db.execute('drop table if exists PING2').close();
55 db.close();
57 var wp = google.gears.factory.create('beta.workerpool');
58 wp.onmessage = function(text, sender, m) {
59 // Worker database SHOULD exist in parent origin.
60 var db = google.gears.factory.create('beta.database');
61 db.open('worker_js');
62 var rs = db.execute('select * from sqlite_master where name = ? limit 1',
63 ['PING2']);
64 handleResult(rs, function(rs) {
65 assert(rs.isValidRow(), 'PING2 table should have been created');
66 });
67 db.close();
68 completeAsync();
71 var childId = wp.createWorkerFromUrl(sameOriginPath + sameOriginWorkerFile);
72 wp.sendMessage('PING2', childId);
75 function testCreateWorkerFromUrl3() {
76 startAsync();
78 // Cleanup any local DB before starting test.
79 var db = google.gears.factory.create('beta.database');
80 db.open('worker_js');
81 db.execute('drop table if exists PING3').close();
82 db.close();
84 var wp = google.gears.factory.create('beta.workerpool');
85 wp.onmessage = function(text, sender, m) {
86 // Worker database should NOT exist in parent origin.
87 var db = google.gears.factory.create('beta.database');
88 db.open('worker_js');
89 var rs = db.execute('select * from sqlite_master where name = ? limit 1',
90 ['PING3']);
91 handleResult(rs, function(rs) {
92 assert(!rs.isValidRow(), 'PING3 table should not have been created');
93 });
94 db.close();
95 completeAsync();
98 // TODO(cprince): In dbg builds, add a 2nd param to createWorkerFromUrl()
99 // so callers can simulate a different origin without being online.
100 //if (!gIsDebugBuild) {
101 var childId = wp.createWorkerFromUrl(crossOriginPath + crossOriginWorkerFile);
102 //} else {
103 // var childId = wp.createWorkerFromUrl(sameOriginPath +
104 // crossOriginWorkerFile,
105 // crossOriginPath);
107 wp.sendMessage('PING3', childId);
110 function testCreateWorkerFromUrl4() {
111 var workerUrl = '/non-existent-file.js';
113 waitForGlobalErrors([workerUrl]);
115 var wp = google.gears.factory.create('beta.workerpool');
116 wp.createWorkerFromUrl(workerUrl);
119 function testCreateWorkerFromUrl5() {
120 var expectedError = 'Page does not have permission to use Google Gears';
122 var wp = google.gears.factory.create('beta.workerpool');
123 // Have to keep a reference to the workerpool otherwise, sometimes the message
124 // never gets processed!
125 // TODO(aa): Investigate why this happens -- ThreadInfo objects are
126 // AddRef()'ing the workerpool, so I would assume this shouldn't be possible.
127 testCreateWorkerFromUrl5.wp = wp;
129 waitForGlobalErrors([expectedError]);
131 var childId = wp.createWorkerFromUrl(
132 crossOriginPath + crossOriginWorkerFileNoPerms);
133 // TODO(cprince): Could add debug-only origin override here too.
134 wp.sendMessage('PING5', childId);
137 function testOneShotWorkerFromUrl() {
138 // Not having a global reference to wp or any callbacks causes this
139 // GearsWorkerPool instance to get GC'd before page unload. This found a bug
140 // where the HttpRequest used to load from url was getting destroyed from a
141 // different thread than it was created on.
142 var wp = google.gears.factory.create('beta.workerpool');
143 wp.createWorkerFromUrl(sameOriginWorkerFile);