OLD | NEW |
---|---|
1 // Copyright 2015 Google Inc. All Rights Reserved. | 1 // Copyright 2015 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 var sandbox = sinon.sandbox.create(); | 15 var sandbox = sinon.sandbox.create(); |
16 | 16 |
17 QUnit.module('notifications', { | 17 QUnit.module('notifications', { |
18 beforeEach: function () { | 18 beforeEach: function () { |
19 this.close = sinon.spy(); | 19 var close = this.close = sinon.spy(); |
20 var Notification = sandbox.stub(self, 'Notification') | 20 var Notification = sandbox.stub(self, 'Notification') |
21 .returns({close: this.close}); | 21 .returns({close: close}); |
22 var requestPermission = sandbox.stub(Notification, 'requestPermission') | 22 var requestPermission = sandbox.stub(Notification, 'requestPermission') |
23 .callsArg(0) // Deprecated callback. | 23 .callsArg(0) // Deprecated callback. |
24 .returns(Promise.resolve()); | 24 .returns(Promise.resolve()); |
25 this.registration = { | |
26 notifications: [], | |
27 showNotification: sandbox.spy(function(title, opts) { | |
28 this.notifications.push({title: title, opts: opts, close: close}); | |
29 return Promise.resolve(); | |
30 }), | |
31 getNotifications: function() { | |
32 return Promise.resolve(this.notifications) | |
33 }, | |
34 }; | |
35 chrome.caterpillar.notifications.getRegistration = sinon.stub() | |
36 .returns(Promise.resolve(this.registration)); | |
25 }, | 37 }, |
26 afterEach: function() { | 38 afterEach: function() { |
27 sandbox.restore(); | 39 sandbox.restore(); |
28 } | 40 } |
29 }); | 41 }); |
30 | 42 |
31 QUnit.test('creates a minimal notification', function(assert) { | 43 QUnit.test('creates a minimal notification', function(assert) { |
32 var done = assert.async(); | 44 var done = assert.async(); |
33 var opts = {'type': 'basic'} | 45 var opts = {'type': 'basic'}; |
46 var showNotification = this.registration.showNotification; | |
34 chrome.notifications.create(opts, function() { | 47 chrome.notifications.create(opts, function() { |
35 assert.ok(Notification.calledOnce); | 48 assert.ok(Notification.calledOnce || showNotification.calledOnce); |
raymes
2016/02/01 00:14:23
It seems like the old Notification codepath will n
Matthew Alger
2016/02/01 00:50:42
Good idea. Done.
| |
36 done(); | 49 done(); |
37 }); | 50 }); |
38 }); | 51 }); |
39 | 52 |
40 QUnit.test('creates a notification with correct body', function(assert) { | 53 QUnit.test('creates a notification with correct body', function(assert) { |
41 var done = assert.async(); | 54 var done = assert.async(); |
42 var opts = {'type': 'basic', 'message': 'méssage'} | 55 var opts = {'type': 'basic', 'message': 'méssage'}; |
56 var showNotification = this.registration.showNotification; | |
43 chrome.notifications.create(opts, function() { | 57 chrome.notifications.create(opts, function() { |
44 assert.equal(Notification.args[0][1].body, 'méssage'); | 58 var creator = Notification.called ? Notification : showNotification; |
59 assert.equal(creator.args[0][1].body, 'méssage'); | |
45 done(); | 60 done(); |
46 }); | 61 }); |
47 }); | 62 }); |
48 | 63 |
49 QUnit.test('creates a notification with correct title', | 64 QUnit.test('creates a notification with correct title', |
50 function(assert) { | 65 function(assert) { |
51 var done = assert.async(); | 66 var done = assert.async(); |
52 var opts = {'type': 'basic', 'title': 'títle'} | 67 var opts = {'type': 'basic', 'title': 'títle'}; |
68 var showNotification = this.registration.showNotification; | |
53 chrome.notifications.create(opts, function() { | 69 chrome.notifications.create(opts, function() { |
54 assert.equal(Notification.args[0][0], 'títle'); | 70 var creator = Notification.called ? Notification : showNotification; |
71 assert.equal(creator.args[0][0], 'títle'); | |
55 done(); | 72 done(); |
56 }); | 73 }); |
57 } | 74 } |
58 ); | 75 ); |
59 | 76 |
60 QUnit.test('creates a notification with correct ID', function(assert) { | 77 QUnit.test('creates a notification with correct ID', function(assert) { |
61 var done = assert.async(); | 78 var done = assert.async(); |
62 var opts = {'type': 'basic', 'title': 'títle'} | 79 var opts = {'type': 'basic', 'title': 'títle'}; |
80 var showNotification = this.registration.showNotification; | |
63 chrome.notifications.create('íd', opts, function() { | 81 chrome.notifications.create('íd', opts, function() { |
64 assert.equal(Notification.args[0][1].tag, 'íd'); | 82 var creator = Notification.called ? Notification : showNotification; |
83 assert.equal(creator.args[0][1].tag, 'íd'); | |
65 done(); | 84 done(); |
66 }); | 85 }); |
67 }); | 86 }); |
68 | 87 |
69 QUnit.test('create closes notifications with same ID', function(assert) { | 88 QUnit.test('create closes notifications with same ID', function(assert) { |
70 var done = assert.async(); | 89 var done = assert.async(); |
71 var close = this.close; | 90 var close = this.close; |
72 chrome.notifications.create('íde', {'type': 'basic'}, function() { | 91 chrome.notifications.create('íde', {'type': 'basic'}, function() { |
73 chrome.notifications.create('íde', {'type': 'basic'}, function() { | 92 chrome.notifications.create('íde', {'type': 'basic'}, function() { |
74 assert.ok(close.calledOnce); | 93 assert.ok(close.calledOnce); |
75 done(); | 94 done(); |
76 }); | 95 }); |
77 }); | 96 }); |
78 }); | 97 }); |
79 | 98 |
80 QUnit.test('create generates an ID if none is provided', function(assert) { | 99 QUnit.test('create generates an ID if none is provided', function(assert) { |
81 var done = assert.async(); | 100 var done = assert.async(); |
82 var clear = sandbox.stub(chrome.notifications, 'clear'); | 101 var clear = sandbox.stub(chrome.notifications, 'clear'); |
102 var showNotification = this.registration.showNotification; | |
83 chrome.notifications.create({'type': 'basic'}, function() { | 103 chrome.notifications.create({'type': 'basic'}, function() { |
84 assert.ok('tag' in Notification.args[0][1]); | 104 var creator = Notification.called ? Notification : showNotification; |
85 assert.equal(typeof Notification.args[0][1].tag, 'string'); | 105 assert.ok('tag' in creator.args[0][1]); |
106 assert.equal(typeof creator.args[0][1].tag, 'string'); | |
86 done(); | 107 done(); |
87 }); | 108 }); |
88 }); | 109 }); |
89 | 110 |
90 QUnit.test('create requests notification permissions', function(assert) { | 111 QUnit.test('create requests notification permissions', function(assert) { |
91 var done = assert.async(); | 112 var done = assert.async(); |
92 chrome.notifications.create({'type': 'basic'}, function() { | 113 chrome.notifications.create({'type': 'basic'}, function() { |
93 assert.ok(Notification.requestPermission.calledOnce); | 114 assert.ok(Notification.requestPermission.calledOnce); |
94 done(); | 115 done(); |
95 }); | 116 }); |
96 }); | 117 }); |
97 | 118 |
98 QUnit.test('create appends the contextMessage to the body', function(assert) { | 119 QUnit.test('create appends the contextMessage to the body', function(assert) { |
99 var done = assert.async(); | 120 var done = assert.async(); |
121 var showNotification = this.registration.showNotification; | |
100 chrome.notifications.create({'type': 'basic', 'message': 'hello', | 122 chrome.notifications.create({'type': 'basic', 'message': 'hello', |
101 'contextMessage': 'world'}, function() { | 123 'contextMessage': 'world'}, function() { |
102 assert.equal(Notification.args[0][1].body, 'hello\n\nworld'); | 124 var creator = Notification.called ? Notification : showNotification; |
125 assert.equal(creator.args[0][1].body, 'hello\n\nworld'); | |
103 done(); | 126 done(); |
104 }); | 127 }); |
105 }); | 128 }); |
106 | 129 |
107 QUnit.test('create adds progress text for progress notifications', | 130 QUnit.test('create adds progress text for progress notifications', |
108 function(assert) { | 131 function(assert) { |
109 var done = assert.async(); | 132 var done = assert.async(); |
133 var showNotification = this.registration.showNotification; | |
110 chrome.notifications.create({'type': 'progress', 'message': 'hello', | 134 chrome.notifications.create({'type': 'progress', 'message': 'hello', |
111 'progress': 15}, function() { | 135 'progress': 15}, function() { |
112 assert.equal(Notification.args[0][1].body, 'hello\n\nProgress: 15%'); | 136 var creator = Notification.called ? Notification : showNotification; |
137 assert.equal(creator.args[0][1].body, 'hello\n\nProgress: 15%'); | |
113 done(); | 138 done(); |
114 }); | 139 }); |
115 }); | 140 }); |
116 | 141 |
117 QUnit.test('create warns if an unsupported type is given', function(assert) { | 142 QUnit.test('create warns if an unsupported type is given', function(assert) { |
118 var done = assert.async(); | 143 var done = assert.async(); |
119 var warn = sandbox.stub(console, 'warn'); | 144 var warn = sandbox.stub(console, 'warn'); |
120 chrome.notifications.create({'type': 'faketype'}, function() { | 145 chrome.notifications.create({'type': 'faketype'}, function() { |
121 assert.ok(warn.calledOnce); | 146 assert.ok(warn.calledOnce); |
122 assert.equal(warn.args[0].join(' '), | 147 assert.equal(warn.args[0].join(' '), |
123 'Notification type faketype not supported. Falling back to basic.'); | 148 'Notification type faketype not supported. Falling back to basic.'); |
124 done(); | 149 done(); |
125 }); | 150 }); |
126 }); | 151 }); |
127 | 152 |
128 QUnit.test('creates a notification with the correct icon',function(assert) { | 153 QUnit.test('creates a notification with the correct icon',function(assert) { |
129 var done = assert.async(); | 154 var done = assert.async(); |
155 var showNotification = this.registration.showNotification; | |
130 chrome.notifications.create({'type': 'basic', 'iconUrl': 'aURL'}, | 156 chrome.notifications.create({'type': 'basic', 'iconUrl': 'aURL'}, |
131 function() { | 157 function() { |
132 assert.equal(Notification.args[0][1].icon, 'aURL'); | 158 var creator = Notification.called ? Notification : showNotification; |
159 assert.equal(creator.args[0][1].icon, 'aURL'); | |
133 done(); | 160 done(); |
134 } | 161 } |
135 ); | 162 ); |
136 }); | 163 }); |
137 | 164 |
138 QUnit.test('passes false to callback if no notification cleared', | 165 QUnit.test('passes false to callback if no notification cleared', |
139 function(assert) { | 166 function(assert) { |
140 var done = assert.async(); | 167 var done = assert.async(); |
141 chrome.notifications.clear('thisiddoesnotexist', function(success) { | 168 chrome.notifications.clear('thisiddoesnotexist', function(success) { |
142 assert.notOk(success); | 169 assert.notOk(success); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 QUnit.test( | 221 QUnit.test( |
195 'passes DENIED to callback if no permission set', function(assert) { | 222 'passes DENIED to callback if no permission set', function(assert) { |
196 Notification.permission = 'default'; | 223 Notification.permission = 'default'; |
197 chrome.notifications.getPermissionLevel(function(level) { | 224 chrome.notifications.getPermissionLevel(function(level) { |
198 assert.equal(level, chrome.notifications.PermissionLevel.DENIED); | 225 assert.equal(level, chrome.notifications.PermissionLevel.DENIED); |
199 }); | 226 }); |
200 } | 227 } |
201 ); | 228 ); |
202 | 229 |
203 // TODO(alger): Add tests for onClicked event handler. | 230 // TODO(alger): Add tests for onClicked event handler. |
OLD | NEW |