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 this.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 = sandbox.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(showNotification.calledOnce); |
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 assert.equal(showNotification.args[0][1].body, 'méssage'); |
45 done(); | 59 done(); |
46 }); | 60 }); |
47 }); | 61 }); |
48 | 62 |
49 QUnit.test('creates a notification with correct title', | 63 QUnit.test('creates a notification with correct title', |
50 function(assert) { | 64 function(assert) { |
51 var done = assert.async(); | 65 var done = assert.async(); |
52 var opts = {'type': 'basic', 'title': 'títle'} | 66 var opts = {'type': 'basic', 'title': 'títle'}; |
| 67 var showNotification = this.registration.showNotification; |
53 chrome.notifications.create(opts, function() { | 68 chrome.notifications.create(opts, function() { |
54 assert.equal(Notification.args[0][0], 'títle'); | 69 assert.equal(showNotification.args[0][0], 'títle'); |
55 done(); | 70 done(); |
56 }); | 71 }); |
57 } | 72 } |
58 ); | 73 ); |
59 | 74 |
60 QUnit.test('creates a notification with correct ID', function(assert) { | 75 QUnit.test('creates a notification with correct ID', function(assert) { |
61 var done = assert.async(); | 76 var done = assert.async(); |
62 var opts = {'type': 'basic', 'title': 'títle'} | 77 var opts = {'type': 'basic', 'title': 'títle'}; |
| 78 var showNotification = this.registration.showNotification; |
63 chrome.notifications.create('íd', opts, function() { | 79 chrome.notifications.create('íd', opts, function() { |
64 assert.equal(Notification.args[0][1].tag, 'íd'); | 80 assert.equal(showNotification.args[0][1].tag, 'íd'); |
65 done(); | 81 done(); |
66 }); | 82 }); |
67 }); | 83 }); |
68 | 84 |
69 QUnit.test('create closes notifications with same ID', function(assert) { | 85 QUnit.test('create closes notifications with same ID', function(assert) { |
70 var done = assert.async(); | 86 var done = assert.async(); |
71 var close = this.close; | 87 var close = this.close; |
72 chrome.notifications.create('íde', {'type': 'basic'}, function() { | 88 chrome.notifications.create('íde', {'type': 'basic'}, function() { |
73 chrome.notifications.create('íde', {'type': 'basic'}, function() { | 89 chrome.notifications.create('íde', {'type': 'basic'}, function() { |
74 assert.ok(close.calledOnce); | 90 assert.ok(close.calledOnce); |
75 done(); | 91 done(); |
76 }); | 92 }); |
77 }); | 93 }); |
78 }); | 94 }); |
79 | 95 |
80 QUnit.test('create generates an ID if none is provided', function(assert) { | 96 QUnit.test('create generates an ID if none is provided', function(assert) { |
81 var done = assert.async(); | 97 var done = assert.async(); |
82 var clear = sandbox.stub(chrome.notifications, 'clear'); | 98 var clear = sandbox.stub(chrome.notifications, 'clear'); |
| 99 var showNotification = this.registration.showNotification; |
83 chrome.notifications.create({'type': 'basic'}, function() { | 100 chrome.notifications.create({'type': 'basic'}, function() { |
84 assert.ok('tag' in Notification.args[0][1]); | 101 assert.ok('tag' in showNotification.args[0][1]); |
85 assert.equal(typeof Notification.args[0][1].tag, 'string'); | 102 assert.equal(typeof showNotification.args[0][1].tag, 'string'); |
86 done(); | 103 done(); |
87 }); | 104 }); |
88 }); | 105 }); |
89 | 106 |
90 QUnit.test('create requests notification permissions', function(assert) { | 107 QUnit.test('create requests notification permissions', function(assert) { |
91 var done = assert.async(); | 108 var done = assert.async(); |
92 chrome.notifications.create({'type': 'basic'}, function() { | 109 chrome.notifications.create({'type': 'basic'}, function() { |
93 assert.ok(Notification.requestPermission.calledOnce); | 110 assert.ok(Notification.requestPermission.calledOnce); |
94 done(); | 111 done(); |
95 }); | 112 }); |
96 }); | 113 }); |
97 | 114 |
98 QUnit.test('create appends the contextMessage to the body', function(assert) { | 115 QUnit.test('create appends the contextMessage to the body', function(assert) { |
99 var done = assert.async(); | 116 var done = assert.async(); |
| 117 var showNotification = this.registration.showNotification; |
100 chrome.notifications.create({'type': 'basic', 'message': 'hello', | 118 chrome.notifications.create({'type': 'basic', 'message': 'hello', |
101 'contextMessage': 'world'}, function() { | 119 'contextMessage': 'world'}, function() { |
102 assert.equal(Notification.args[0][1].body, 'hello\n\nworld'); | 120 assert.equal(showNotification.args[0][1].body, 'hello\n\nworld'); |
103 done(); | 121 done(); |
104 }); | 122 }); |
105 }); | 123 }); |
106 | 124 |
107 QUnit.test('create adds progress text for progress notifications', | 125 QUnit.test('create adds progress text for progress notifications', |
108 function(assert) { | 126 function(assert) { |
109 var done = assert.async(); | 127 var done = assert.async(); |
| 128 var showNotification = this.registration.showNotification; |
110 chrome.notifications.create({'type': 'progress', 'message': 'hello', | 129 chrome.notifications.create({'type': 'progress', 'message': 'hello', |
111 'progress': 15}, function() { | 130 'progress': 15}, function() { |
112 assert.equal(Notification.args[0][1].body, 'hello\n\nProgress: 15%'); | 131 assert.equal(showNotification.args[0][1].body, 'hello\n\nProgress: 15%'); |
113 done(); | 132 done(); |
114 }); | 133 }); |
115 }); | 134 }); |
116 | 135 |
117 QUnit.test('create warns if an unsupported type is given', function(assert) { | 136 QUnit.test('create warns if an unsupported type is given', function(assert) { |
118 var done = assert.async(); | 137 var done = assert.async(); |
119 var warn = sandbox.stub(console, 'warn'); | 138 var warn = sandbox.stub(console, 'warn'); |
120 chrome.notifications.create({'type': 'faketype'}, function() { | 139 chrome.notifications.create({'type': 'faketype'}, function() { |
121 assert.ok(warn.calledOnce); | 140 assert.ok(warn.calledOnce); |
122 assert.equal(warn.args[0].join(' '), | 141 assert.equal(warn.args[0].join(' '), |
123 'Notification type faketype not supported. Falling back to basic.'); | 142 'Notification type faketype not supported. Falling back to basic.'); |
124 done(); | 143 done(); |
125 }); | 144 }); |
126 }); | 145 }); |
127 | 146 |
128 QUnit.test('creates a notification with the correct icon',function(assert) { | 147 QUnit.test('creates a notification with the correct icon',function(assert) { |
129 var done = assert.async(); | 148 var done = assert.async(); |
| 149 var showNotification = this.registration.showNotification; |
130 chrome.notifications.create({'type': 'basic', 'iconUrl': 'aURL'}, | 150 chrome.notifications.create({'type': 'basic', 'iconUrl': 'aURL'}, |
131 function() { | 151 function() { |
132 assert.equal(Notification.args[0][1].icon, 'aURL'); | 152 assert.equal(showNotification.args[0][1].icon, 'aURL'); |
133 done(); | 153 done(); |
134 } | 154 } |
135 ); | 155 ); |
136 }); | 156 }); |
137 | 157 |
138 QUnit.test('passes false to callback if no notification cleared', | 158 QUnit.test('passes false to callback if no notification cleared', |
139 function(assert) { | 159 function(assert) { |
140 var done = assert.async(); | 160 var done = assert.async(); |
141 chrome.notifications.clear('thisiddoesnotexist', function(success) { | 161 chrome.notifications.clear('thisiddoesnotexist', function(success) { |
142 assert.notOk(success); | 162 assert.notOk(success); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 QUnit.test( | 214 QUnit.test( |
195 'passes DENIED to callback if no permission set', function(assert) { | 215 'passes DENIED to callback if no permission set', function(assert) { |
196 Notification.permission = 'default'; | 216 Notification.permission = 'default'; |
197 chrome.notifications.getPermissionLevel(function(level) { | 217 chrome.notifications.getPermissionLevel(function(level) { |
198 assert.equal(level, chrome.notifications.PermissionLevel.DENIED); | 218 assert.equal(level, chrome.notifications.PermissionLevel.DENIED); |
199 }); | 219 }); |
200 } | 220 } |
201 ); | 221 ); |
202 | 222 |
203 // TODO(alger): Add tests for onClicked event handler. | 223 // TODO(alger): Add tests for onClicked event handler. |
| 224 |
| 225 QUnit.test('creates a notification with deprecated method', function(assert) { |
| 226 var done = assert.async(); |
| 227 |
| 228 // Registration is supported, but showNotification is not. |
| 229 this.registration = { |
| 230 }; |
| 231 chrome.caterpillar.notifications.getRegistration = sandbox.stub() |
| 232 .returns(Promise.resolve(this.registration)); |
| 233 |
| 234 var Notification = this.Notification; |
| 235 var opts = {'type': 'basic', 'message': 'méssage', 'title': 'tést'}; |
| 236 chrome.notifications.create(opts, function() { |
| 237 assert.equal(Notification.args[0][1].body, 'méssage'); |
| 238 assert.equal(Notification.args[0][0], 'tést'); |
| 239 done(); |
| 240 }); |
| 241 }); |
OLD | NEW |