1 // Copyright 2007, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
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
;
29 currentUrl
.substring(0, 1 + currentUrl
.lastIndexOf('/'));
30 var sameOriginWorkerFile
= '../testcases/workerpool_same_origin.js';
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() {
40 var wp
= google
.gears
.factory
.create('beta.workerpool');
41 wp
.onmessage = function(text
, sender
, m
) {
44 var childId
= wp
.createWorkerFromUrl(sameOriginWorkerFile
);
45 wp
.sendMessage('PING1', childId
);
48 function testCreateWorkerFromUrl2() {
51 // Cleanup any local DB before starting test.
52 var db
= google
.gears
.factory
.create('beta.database');
54 db
.execute('drop table if exists PING2').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');
62 var rs
= db
.execute('select * from sqlite_master where name = ? limit 1',
64 handleResult(rs
, function(rs
) {
65 assert(rs
.isValidRow(), 'PING2 table should have been created');
71 var childId
= wp
.createWorkerFromUrl(sameOriginPath
+ sameOriginWorkerFile
);
72 wp
.sendMessage('PING2', childId
);
75 function testCreateWorkerFromUrl3() {
78 // Cleanup any local DB before starting test.
79 var db
= google
.gears
.factory
.create('beta.database');
81 db
.execute('drop table if exists PING3').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');
89 var rs
= db
.execute('select * from sqlite_master where name = ? limit 1',
91 handleResult(rs
, function(rs
) {
92 assert(!rs
.isValidRow(), 'PING3 table should not have been created');
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
);
103 // var childId = wp.createWorkerFromUrl(sameOriginPath +
104 // crossOriginWorkerFile,
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
);