Skip to content

Commit 86cc778

Browse files
committed
Fix tests so they auto-start when run via cordova-paramedic
1 parent cbfa195 commit 86cc778

File tree

1 file changed

+107
-118
lines changed

1 file changed

+107
-118
lines changed

tests/tests.js

Lines changed: 107 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
*/
2121

22-
/* global MSApp, alert */
22+
/* global MSApp */
2323

2424
var cordova = require('cordova');
2525
var isWindows = cordova.platformId === 'windows';
@@ -33,128 +33,126 @@ if (isWindows && navigator && navigator.notification && navigator.notification.a
3333
}
3434

3535
exports.defineAutoTests = function () {
36-
3736
var createTests = function (platformOpts) {
3837
platformOpts = platformOpts || '';
3938

40-
describe('cordova.InAppBrowser', function () {
39+
describe('cordova.InAppBrowser', function () {
4140

42-
it('inappbrowser.spec.1 should exist', function () {
43-
expect(cordova.InAppBrowser).toBeDefined();
44-
});
41+
it('inappbrowser.spec.1 should exist', function () {
42+
expect(cordova.InAppBrowser).toBeDefined();
43+
});
4544

46-
it('inappbrowser.spec.2 should contain open function', function () {
47-
expect(cordova.InAppBrowser.open).toBeDefined();
48-
expect(cordova.InAppBrowser.open).toEqual(jasmine.any(Function));
49-
});
45+
it('inappbrowser.spec.2 should contain open function', function () {
46+
expect(cordova.InAppBrowser.open).toBeDefined();
47+
expect(cordova.InAppBrowser.open).toEqual(jasmine.any(Function));
5048
});
49+
});
5150

52-
describe('open method', function () {
51+
describe('open method', function () {
5352

54-
if (cordova.platformId === 'osx') {
55-
pending('Open method not fully supported on OSX.');
56-
return;
57-
}
53+
if (cordova.platformId === 'osx') {
54+
pending('Open method not fully supported on OSX.');
55+
return;
56+
}
5857

59-
var iabInstance;
60-
var originalTimeout;
61-
var url = 'https://dist.apache.org/repos/dist/dev/cordova/';
62-
var badUrl = 'http://bad-uri/';
58+
var iabInstance;
59+
var originalTimeout;
60+
var url = 'https://dist.apache.org/repos/dist/dev/cordova/';
61+
var badUrl = 'http://bad-uri/';
6362

64-
beforeEach(function () {
65-
// increase timeout to ensure test url could be loaded within test time
66-
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
67-
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
63+
beforeEach(function () {
64+
// increase timeout to ensure test url could be loaded within test time
65+
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
66+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
6867

69-
iabInstance = null;
70-
});
71-
72-
afterEach(function (done) {
73-
// restore original timeout
74-
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
68+
iabInstance = null;
69+
});
7570

76-
if (iabInstance !== null && iabInstance.close) {
77-
iabInstance.close();
78-
}
79-
iabInstance = null;
80-
// add some extra time so that iab dialog is closed
81-
setTimeout(done, 2000);
82-
});
71+
afterEach(function (done) {
72+
// restore original timeout
73+
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
8374

84-
function verifyEvent (evt, type) {
85-
expect(evt).toBeDefined();
86-
expect(evt.type).toEqual(type);
87-
// `exit` event does not have url field, browser returns null url for CORS requests
88-
if (type !== 'exit' && !isBrowser) {
89-
expect(evt.url).toEqual(url);
90-
}
75+
if (iabInstance !== null && iabInstance.close) {
76+
iabInstance.close();
9177
}
78+
iabInstance = null;
79+
// add some extra time so that iab dialog is closed
80+
setTimeout(done, 2000);
81+
});
9282

93-
function verifyLoadErrorEvent (evt) {
94-
expect(evt).toBeDefined();
95-
expect(evt.type).toEqual('loaderror');
96-
expect(evt.url).toEqual(badUrl);
97-
expect(evt.code).toEqual(jasmine.any(Number));
98-
expect(evt.message).toEqual(jasmine.any(String));
83+
function verifyEvent (evt, type) {
84+
expect(evt).toBeDefined();
85+
expect(evt.type).toEqual(type);
86+
// `exit` event does not have url field, browser returns null url for CORS requests
87+
if (type !== 'exit' && !isBrowser) {
88+
expect(evt.url).toEqual(url);
9989
}
90+
}
91+
92+
function verifyLoadErrorEvent (evt) {
93+
expect(evt).toBeDefined();
94+
expect(evt.type).toEqual('loaderror');
95+
expect(evt.url).toEqual(badUrl);
96+
expect(evt.code).toEqual(jasmine.any(Number));
97+
expect(evt.message).toEqual(jasmine.any(String));
98+
}
10099

101-
it('inappbrowser.spec.3 should return InAppBrowser instance with required methods', function () {
100+
it('inappbrowser.spec.3 should return InAppBrowser instance with required methods', function () {
102101
iabInstance = cordova.InAppBrowser.open(url, '_blank', platformOpts);
103102

104-
expect(iabInstance).toBeDefined();
103+
expect(iabInstance).toBeDefined();
105104

106-
expect(iabInstance.addEventListener).toEqual(jasmine.any(Function));
107-
expect(iabInstance.removeEventListener).toEqual(jasmine.any(Function));
108-
expect(iabInstance.close).toEqual(jasmine.any(Function));
109-
expect(iabInstance.show).toEqual(jasmine.any(Function));
110-
expect(iabInstance.hide).toEqual(jasmine.any(Function));
111-
expect(iabInstance.executeScript).toEqual(jasmine.any(Function));
112-
expect(iabInstance.insertCSS).toEqual(jasmine.any(Function));
113-
});
105+
expect(iabInstance.addEventListener).toEqual(jasmine.any(Function));
106+
expect(iabInstance.removeEventListener).toEqual(jasmine.any(Function));
107+
expect(iabInstance.close).toEqual(jasmine.any(Function));
108+
expect(iabInstance.show).toEqual(jasmine.any(Function));
109+
expect(iabInstance.hide).toEqual(jasmine.any(Function));
110+
expect(iabInstance.executeScript).toEqual(jasmine.any(Function));
111+
expect(iabInstance.insertCSS).toEqual(jasmine.any(Function));
112+
});
114113

115-
it('inappbrowser.spec.4 should support loadstart and loadstop events', function (done) {
116-
var onLoadStart = jasmine.createSpy('loadstart event callback').and.callFake(function (evt) {
117-
verifyEvent(evt, 'loadstart');
118-
});
114+
it('inappbrowser.spec.4 should support loadstart and loadstop events', function (done) {
115+
var onLoadStart = jasmine.createSpy('loadstart event callback').and.callFake(function (evt) {
116+
verifyEvent(evt, 'loadstart');
117+
});
119118

120119
iabInstance = cordova.InAppBrowser.open(url, '_blank', platformOpts);
121-
iabInstance.addEventListener('loadstart', onLoadStart);
122-
iabInstance.addEventListener('loadstop', function (evt) {
123-
verifyEvent(evt, 'loadstop');
124-
if (!isBrowser) {
125-
// according to documentation, "loadstart" event is not supported on browser
126-
// https://github.com/apache/cordova-plugin-inappbrowser#browser-quirks-1
127-
expect(onLoadStart).toHaveBeenCalled();
128-
}
129-
done();
130-
});
120+
iabInstance.addEventListener('loadstart', onLoadStart);
121+
iabInstance.addEventListener('loadstop', function (evt) {
122+
verifyEvent(evt, 'loadstop');
123+
if (!isBrowser) {
124+
// according to documentation, "loadstart" event is not supported on browser
125+
// https://github.com/apache/cordova-plugin-inappbrowser#browser-quirks-1
126+
expect(onLoadStart).toHaveBeenCalled();
127+
}
128+
done();
131129
});
130+
});
132131

133-
it('inappbrowser.spec.5 should support exit event', function (done) {
132+
it('inappbrowser.spec.5 should support exit event', function (done) {
134133
iabInstance = cordova.InAppBrowser.open(url, '_blank', platformOpts);
135-
iabInstance.addEventListener('exit', function (evt) {
136-
verifyEvent(evt, 'exit');
137-
done();
138-
});
139-
iabInstance.close();
140-
iabInstance = null;
134+
iabInstance.addEventListener('exit', function (evt) {
135+
verifyEvent(evt, 'exit');
136+
done();
141137
});
138+
iabInstance.close();
139+
iabInstance = null;
140+
});
142141

143-
it('inappbrowser.spec.6 should support loaderror event', function (done) {
144-
if (isBrowser) {
145-
// according to documentation, "loaderror" event is not supported on browser
146-
// https://github.com/apache/cordova-plugin-inappbrowser#browser-quirks-1
147-
pending('Browser platform doesn\'t support loaderror event');
148-
}
142+
it('inappbrowser.spec.6 should support loaderror event', function (done) {
143+
if (isBrowser) {
144+
// according to documentation, "loaderror" event is not supported on browser
145+
// https://github.com/apache/cordova-plugin-inappbrowser#browser-quirks-1
146+
pending('Browser platform doesn\'t support loaderror event');
147+
}
149148
iabInstance = cordova.InAppBrowser.open(badUrl, '_blank', platformOpts);
150-
iabInstance.addEventListener('loaderror', function (evt) {
151-
verifyLoadErrorEvent(evt);
152-
done();
153-
});
149+
iabInstance.addEventListener('loaderror', function (evt) {
150+
verifyLoadErrorEvent(evt);
151+
done();
154152
});
155153
});
154+
});
156155
};
157-
158156
if (isIos) {
159157
createTests('usewkwebview=no');
160158
createTests('usewkwebview=yes');
@@ -165,6 +163,15 @@ exports.defineAutoTests = function () {
165163

166164
exports.defineManualTests = function (contentEl, createActionButton) {
167165

166+
var platformOpts = '';
167+
var platform_info = '';
168+
if (isIos) {
169+
platformOpts = 'usewkwebview=no';
170+
platform_info = '<h1>Webview</h1>' +
171+
'<p>Use this button to toggle the Webview implementation.</p>' +
172+
'<div id="webviewToggle"></div>';
173+
}
174+
168175
function doOpen (url, target, params, numExpectedRedirects, useWindowOpen) {
169176
numExpectedRedirects = numExpectedRedirects || 0;
170177
useWindowOpen = useWindowOpen || false;
@@ -173,7 +180,6 @@ exports.defineManualTests = function (contentEl, createActionButton) {
173180
var counts;
174181
var lastLoadStartURL;
175182
var wasReset = false;
176-
177183
function reset () {
178184
counts = {
179185
'loaderror': 0,
@@ -183,7 +189,6 @@ exports.defineManualTests = function (contentEl, createActionButton) {
183189
};
184190
lastLoadStartURL = '';
185191
}
186-
187192
reset();
188193

189194
var iab;
@@ -277,11 +282,11 @@ exports.defineManualTests = function (contentEl, createActionButton) {
277282
};
278283
if (cssUrl) {
279284
iab.addEventListener('loadstop', function (event) {
280-
iab.insertCSS({file: cssUrl}, useCallback && callback);
285+
iab.insertCSS({ file: cssUrl }, useCallback && callback);
281286
});
282287
} else {
283288
iab.addEventListener('loadstop', function (event) {
284-
iab.insertCSS({code: '#style-update-literal { \ndisplay: block !important; \n}'},
289+
iab.insertCSS({ code: '#style-update-literal { \ndisplay: block !important; \n}' },
285290
useCallback && callback);
286291
});
287292
}
@@ -291,7 +296,7 @@ exports.defineManualTests = function (contentEl, createActionButton) {
291296
var iab = doOpen(url, '_blank', 'location=yes');
292297
if (jsUrl) {
293298
iab.addEventListener('loadstop', function (event) {
294-
iab.executeScript({file: jsUrl}, useCallback && function (results) {
299+
iab.executeScript({ file: jsUrl }, useCallback && function (results) {
295300
if (results && results.length === 0) {
296301
alert('Results verified'); // eslint-disable-line no-undef
297302
} else {
@@ -303,11 +308,11 @@ exports.defineManualTests = function (contentEl, createActionButton) {
303308
} else {
304309
iab.addEventListener('loadstop', function (event) {
305310
var code = '(function(){\n' +
306-
' var header = document.getElementById("header");\n' +
307-
' header.innerHTML = "Script literal successfully injected";\n' +
308-
' return "abc";\n' +
309-
'})()';
310-
iab.executeScript({code: code}, useCallback && function (results) {
311+
' var header = document.getElementById("header");\n' +
312+
' header.innerHTML = "Script literal successfully injected";\n' +
313+
' return "abc";\n' +
314+
'})()';
315+
iab.executeScript({ code: code }, useCallback && function (results) {
311316
if (results && results.length === 1 && results[0] === 'abc') {
312317
alert('Results verified'); // eslint-disable-line no-undef
313318
} else {
@@ -318,11 +323,8 @@ exports.defineManualTests = function (contentEl, createActionButton) {
318323
});
319324
}
320325
}
321-
322326
var hiddenwnd = null;
323-
var loadlistener = function (event) {
324-
alert('background window loaded ');
325-
}; // eslint-disable-line no-undef
327+
var loadlistener = function (event) { alert('background window loaded '); }; // eslint-disable-line no-undef
326328
function openHidden (url, startHidden) {
327329
var shopt = (startHidden) ? 'hidden=yes' : '';
328330
if (platformOpts) {
@@ -335,13 +337,11 @@ exports.defineManualTests = function (contentEl, createActionButton) {
335337
}
336338
if (startHidden) hiddenwnd.addEventListener('loadstop', loadlistener);
337339
}
338-
339340
function showHidden () {
340341
if (hiddenwnd) {
341342
hiddenwnd.show();
342343
}
343344
}
344-
345345
function closeHidden () {
346346
if (hiddenwnd) {
347347
hiddenwnd.removeEventListener('loadstop', loadlistener);
@@ -358,15 +358,6 @@ exports.defineManualTests = function (contentEl, createActionButton) {
358358
'<h4>User-Agent: <span id="user-agent"> </span></hr>' +
359359
'</div>';
360360

361-
var platformOpts = '';
362-
var platform_info = '';
363-
if (isIos) {
364-
platformOpts = 'usewkwebview=no';
365-
platform_info = '<h1>Webview</h1>' +
366-
'<p>Use this button to toggle the Webview implementation.</p>' +
367-
'<div id="webviewToggle"></div>';
368-
}
369-
370361
var local_tests = '<h1>Local URL</h1>' +
371362
'<div id="openLocal"></div>' +
372363
'Expected result: opens successfully in CordovaWebView.' +
@@ -523,13 +514,11 @@ exports.defineManualTests = function (contentEl, createActionButton) {
523514
var injectjs = isWindows ? basePath + 'inject.js' : 'inject.js';
524515
var injectcss = isWindows ? basePath + 'inject.css' : 'inject.css';
525516
var videohtml = basePath + 'video.html';
526-
527517
if (isIos) {
528518
createActionButton('Webview=UIWebView', function () {
529519
var webviewOption = 'usewkwebview=';
530520
var webviewToggle = document.getElementById('webviewToggle');
531521
var button = webviewToggle.getElementsByClassName('topcoat-button')[0];
532-
533522
if (platformOpts === webviewOption + 'yes') {
534523
platformOpts = webviewOption + 'no';
535524
button.textContent = 'Webview=UIWebView';

0 commit comments

Comments
 (0)