Skip to content

Commit c210fa8

Browse files
committed
[test] add test case for Window.cookies
1 parent e92564d commit c210fa8

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>TEST CASE FOR COOKIES API</title>
5+
</head>
6+
<body>
7+
<h1>Hello World!</h1>
8+
We are using node.js
9+
<script>
10+
var gui = require('nw.gui');
11+
var win = gui.Window.get( window.open('http://115.com'));
12+
var results = new Array();
13+
var changed = false;
14+
// action 1: retrieve(get), retrieveAll(getAll) set
15+
// action 2: set
16+
// action 3: set and onChanged
17+
var action = gui.App.argv[0];
18+
19+
//retrieveAll(".115.com")
20+
function retrieveAll(domain) {
21+
win.cookies.getAll({ 'domain': domain}, function(cookies) {
22+
console.log(cookies);
23+
if (cookies != null && cookies != undefined)
24+
results.push(true);
25+
else
26+
results.push(false);
27+
// for (var i = 0; i < cookies.length; i++) {
28+
// console.log("name: " + cookies[i].name + "\n" + "path: " + cookies[i].path + "\n" + "domain: " + cookies[i].domain + "\n" + "session: " + cookies[i].session.toString() + "\n" + "value: " + cookies[i].value + " " + "\n");
29+
// }
30+
})
31+
}
32+
33+
//remove("http://www.115.com","115_lang")
34+
function remove(url, name) {
35+
win.cookies.remove({ 'url': url, 'name': name });
36+
}
37+
38+
//set('.115.com',"http://www.115.com","115_lang","en")
39+
function set(domain, url, name, value) {
40+
win.cookies.set({ 'domain': domain, 'url': url, 'name': name, 'value': value, 'expirationDate': 9999999999});
41+
}
42+
43+
//retrieve("http://www.115.com","115_lang")
44+
function retrieve(url, name){
45+
win.cookies.get({"url":url, "name": name},function (cookie){
46+
console.log(cookie);
47+
if (cookie != null && cookie != undefined) {
48+
results.push(true);
49+
} else {
50+
results.push(false);
51+
}
52+
});
53+
}
54+
55+
win.cookies.onChanged.addListener(function() {
56+
changed = true;
57+
});
58+
59+
if (action == '1') {
60+
win.window.onload = function() {
61+
retrieve("http://www.115.com","115_lang");//get true
62+
retrieveAll(".115.com");//getAll true
63+
results.push(changed);
64+
win.close();
65+
// true, true
66+
setTimeout(function() {
67+
var client = require('../../nw_test_app').createClient({
68+
argv: gui.App.argv,
69+
data: results
70+
});
71+
}, 1000);
72+
}
73+
} else if (action == '2') {
74+
win.close();
75+
set('.115.com',"http://www.115.com","115_lang","en");//set true
76+
win = gui.Window.get(window.open('http://115.com'));
77+
win.window.onload = function() {
78+
if (win.window.document.title != "115 Cloud drive-Your files in the cloud-Easy&Safe")
79+
results.push(false);
80+
else
81+
results.push(true);
82+
results.push(changed);
83+
win.close();
84+
setTimeout(function() {
85+
var client = require('../../nw_test_app').createClient({
86+
argv: gui.App.argv,
87+
data: results
88+
});
89+
}, 2000)
90+
}
91+
} else if (action == '3') {
92+
win.close();
93+
set('.115.com',"http://www.115.com","115_lang","zh");//set true
94+
win = gui.Window.get(window.open('http://115.com'));
95+
win.window.onload = function() {
96+
if (win.window.document.title == "115 Cloud drive-Your files in the cloud-Easy&Safe")
97+
results.push(false);
98+
else
99+
results.push(true);
100+
setTimeout(function() {
101+
win.close();
102+
results.push(changed);
103+
}, 1000);
104+
// true
105+
setTimeout(function() {
106+
var client = require('../../nw_test_app').createClient({
107+
argv: gui.App.argv,
108+
data: results
109+
});
110+
}, 3000);
111+
}
112+
} else if (action == '4') {
113+
remove("http://www.115.com","115_lang");
114+
results.push(changed);
115+
retrieve("http://www.115.com","115_lang");//get false
116+
117+
//false, true
118+
setTimeout(function() {
119+
var client = require('../../nw_test_app').createClient({
120+
argv: gui.App.argv,
121+
data: results
122+
});
123+
}, 1000);
124+
}
125+
126+
127+
</script>
128+
</body>
129+
</html>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var path = require('path');
2+
var app_test = require('./nw_test_app');
3+
var assert = require('assert');
4+
var results;
5+
var changed;
6+
describe('Window.cookies', function() {
7+
8+
describe("get, getAll", function() {
9+
before(function(done) {
10+
this.timeout(0);
11+
var child = app_test.createChildProcess({
12+
execPath: process.execPath,
13+
appPath: path.join(global.tests_dir, 'cookies_api'),
14+
args: [1],
15+
end: function(data, app) {
16+
results = data;
17+
app.kill();
18+
console.log("1");
19+
done();
20+
}
21+
});
22+
});
23+
it('should get cookies', function() {
24+
assert.equal(results[0], true);
25+
assert.equal(results[1], true);
26+
})
27+
})
28+
29+
describe("set", function() {
30+
describe("set lang en", function() {
31+
before(function(done) {
32+
this.timeout(0);
33+
var first = false;
34+
var child = app_test.createChildProcess({
35+
execPath: process.execPath,
36+
appPath: path.join(global.tests_dir, 'cookies_api'),
37+
args: [2],
38+
end: function(data, app) {
39+
results = data;
40+
changed = results[1];
41+
app.kill();
42+
console.log("2");
43+
done();
44+
}
45+
});
46+
});
47+
it('should get cookies', function() {
48+
assert.equal(results[0], true);
49+
})
50+
});
51+
52+
describe("set lang zh", function() {
53+
before(function(done) {
54+
this.timeout(0);
55+
var child = app_test.createChildProcess({
56+
execPath: process.execPath,
57+
appPath: path.join(global.tests_dir, 'cookies_api'),
58+
args: [3],
59+
end: function(data, app) {
60+
console.log("3");
61+
results = data;
62+
done();
63+
app.kill();
64+
}
65+
});
66+
});
67+
68+
it('should get cookies', function() {
69+
assert.equal(results[0], true);
70+
})
71+
});
72+
73+
});
74+
75+
describe('remove', function() {
76+
before(function(done) {
77+
this.timeout(0);
78+
var child = app_test.createChildProcess({
79+
execPath: process.execPath,
80+
appPath: path.join(global.tests_dir, 'cookies_api'),
81+
args: [4],
82+
end: function(data, app) {
83+
console.log("4");
84+
results = data;
85+
app.kill();
86+
done();
87+
}
88+
});
89+
});
90+
91+
it('should remove lang', function() {
92+
assert.equal(results[1], false);
93+
});
94+
});
95+
96+
describe('onChanged', function() {
97+
it('should be called when changed', function() {
98+
assert.equal(changed, true);
99+
})
100+
});
101+
});
102+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "nw1",
3+
"main": "index.html"
4+
}

0 commit comments

Comments
 (0)