Chromium Code Reviews| 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 this.close = sinon.spy(); |
| 20 var close = this.close; | |
|
raymes
2016/01/28 04:36:56
Can this be inlined?
Matthew Alger
2016/01/28 05:18:02
Yup. Done.
| |
| 20 var Notification = sandbox.stub(self, 'Notification') | 21 var Notification = sandbox.stub(self, 'Notification') |
| 21 .returns({close: this.close}); | 22 .returns({close: close}); |
| 22 var requestPermission = sandbox.stub(Notification, 'requestPermission') | 23 var requestPermission = sandbox.stub(Notification, 'requestPermission') |
| 23 .callsArg(0) // Deprecated callback. | 24 .callsArg(0) // Deprecated callback. |
| 24 .returns(Promise.resolve()); | 25 .returns(Promise.resolve()); |
| 26 this.getRegistration = chrome.caterpillar.notifications.getRegistration; | |
|
raymes
2016/01/28 04:36:56
This seems unneeded
Matthew Alger
2016/01/28 05:18:02
It was so that I could restore getRegistration lat
| |
| 27 this.showNotification = sandbox.spy(function(title, opts) { | |
| 28 this.notifications.push({title: title, opts: opts, close: close}); | |
| 29 return Promise.resolve(); | |
| 30 }); | |
|
raymes
2016/01/28 04:36:56
This would be clearer if you defined:
this.registr
Matthew Alger
2016/01/28 05:18:02
Done.
| |
| 31 chrome.caterpillar.notifications.getRegistration = sinon.stub() | |
| 32 .returns(Promise.resolve({ | |
| 33 notifications: [], | |
| 34 showNotification: this.showNotification, | |
| 35 getNotifications: function() { | |
| 36 return Promise.resolve(this.notifications) | |
| 37 }, | |
| 38 })); | |
| 25 }, | 39 }, |
| 26 afterEach: function() { | 40 afterEach: function() { |
| 27 sandbox.restore(); | 41 sandbox.restore(); |
| 28 } | 42 } |
| 29 }); | 43 }); |
| 30 | 44 |
| 31 QUnit.test('creates a minimal notification', function(assert) { | 45 QUnit.test('creates a minimal notification', function(assert) { |
| 32 var done = assert.async(); | 46 var done = assert.async(); |
| 33 var opts = {'type': 'basic'} | 47 var opts = {'type': 'basic'}; |
| 48 var showNotification = this.showNotification; | |
| 34 chrome.notifications.create(opts, function() { | 49 chrome.notifications.create(opts, function() { |
| 35 assert.ok(Notification.calledOnce); | 50 assert.ok(Notification.calledOnce || showNotification.calledOnce); |
| 36 done(); | 51 done(); |
| 37 }); | 52 }); |
| 38 }); | 53 }); |
| 39 | 54 |
| 40 QUnit.test('creates a notification with correct body', function(assert) { | 55 QUnit.test('creates a notification with correct body', function(assert) { |
| 41 var done = assert.async(); | 56 var done = assert.async(); |
| 42 var opts = {'type': 'basic', 'message': 'méssage'} | 57 var opts = {'type': 'basic', 'message': 'méssage'}; |
| 58 var showNotification = this.showNotification; | |
| 43 chrome.notifications.create(opts, function() { | 59 chrome.notifications.create(opts, function() { |
| 44 assert.equal(Notification.args[0][1].body, 'méssage'); | 60 var creator = Notification.called ? Notification : showNotification; |
|
raymes
2016/01/28 04:36:56
This would be simpler if we just had the one codep
Matthew Alger
2016/01/28 05:18:02
Do you mean dropping support for new Notification?
| |
| 61 assert.equal(creator.args[0][1].body, 'méssage'); | |
| 45 done(); | 62 done(); |
| 46 }); | 63 }); |
| 47 }); | 64 }); |
| 48 | 65 |
| 49 QUnit.test('creates a notification with correct title', | 66 QUnit.test('creates a notification with correct title', |
| 50 function(assert) { | 67 function(assert) { |
| 51 var done = assert.async(); | 68 var done = assert.async(); |
| 52 var opts = {'type': 'basic', 'title': 'títle'} | 69 var opts = {'type': 'basic', 'title': 'títle'}; |
| 70 var showNotification = this.showNotification; | |
| 53 chrome.notifications.create(opts, function() { | 71 chrome.notifications.create(opts, function() { |
| 54 assert.equal(Notification.args[0][0], 'títle'); | 72 var creator = Notification.called ? Notification : showNotification; |
| 73 assert.equal(creator.args[0][0], 'títle'); | |
| 55 done(); | 74 done(); |
| 56 }); | 75 }); |
| 57 } | 76 } |
| 58 ); | 77 ); |
| 59 | 78 |
| 60 QUnit.test('creates a notification with correct ID', function(assert) { | 79 QUnit.test('creates a notification with correct ID', function(assert) { |
| 61 var done = assert.async(); | 80 var done = assert.async(); |
| 62 var opts = {'type': 'basic', 'title': 'títle'} | 81 var opts = {'type': 'basic', 'title': 'títle'}; |
| 82 var showNotification = this.showNotification; | |
| 63 chrome.notifications.create('íd', opts, function() { | 83 chrome.notifications.create('íd', opts, function() { |
| 64 assert.equal(Notification.args[0][1].tag, 'íd'); | 84 var creator = Notification.called ? Notification : showNotification; |
| 85 assert.equal(creator.args[0][1].tag, 'íd'); | |
| 65 done(); | 86 done(); |
| 66 }); | 87 }); |
| 67 }); | 88 }); |
| 68 | 89 |
| 69 QUnit.test('create closes notifications with same ID', function(assert) { | 90 QUnit.test('create closes notifications with same ID', function(assert) { |
| 70 var done = assert.async(); | 91 var done = assert.async(); |
| 71 var close = this.close; | 92 var close = this.close; |
| 72 chrome.notifications.create('íde', {'type': 'basic'}, function() { | 93 chrome.notifications.create('íde', {'type': 'basic'}, function() { |
| 73 chrome.notifications.create('íde', {'type': 'basic'}, function() { | 94 chrome.notifications.create('íde', {'type': 'basic'}, function() { |
| 74 assert.ok(close.calledOnce); | 95 assert.ok(close.calledOnce); |
| 75 done(); | 96 done(); |
| 76 }); | 97 }); |
| 77 }); | 98 }); |
| 78 }); | 99 }); |
| 79 | 100 |
| 80 QUnit.test('create generates an ID if none is provided', function(assert) { | 101 QUnit.test('create generates an ID if none is provided', function(assert) { |
| 81 var done = assert.async(); | 102 var done = assert.async(); |
| 82 var clear = sandbox.stub(chrome.notifications, 'clear'); | 103 var clear = sandbox.stub(chrome.notifications, 'clear'); |
| 104 var showNotification = this.showNotification; | |
| 83 chrome.notifications.create({'type': 'basic'}, function() { | 105 chrome.notifications.create({'type': 'basic'}, function() { |
| 84 assert.ok('tag' in Notification.args[0][1]); | 106 var creator = Notification.called ? Notification : showNotification; |
| 85 assert.equal(typeof Notification.args[0][1].tag, 'string'); | 107 assert.ok('tag' in creator.args[0][1]); |
| 108 assert.equal(typeof creator.args[0][1].tag, 'string'); | |
| 86 done(); | 109 done(); |
| 87 }); | 110 }); |
| 88 }); | 111 }); |
| 89 | 112 |
| 90 QUnit.test('create requests notification permissions', function(assert) { | 113 QUnit.test('create requests notification permissions', function(assert) { |
| 91 var done = assert.async(); | 114 var done = assert.async(); |
| 92 chrome.notifications.create({'type': 'basic'}, function() { | 115 chrome.notifications.create({'type': 'basic'}, function() { |
| 93 assert.ok(Notification.requestPermission.calledOnce); | 116 assert.ok(Notification.requestPermission.calledOnce); |
| 94 done(); | 117 done(); |
| 95 }); | 118 }); |
| 96 }); | 119 }); |
| 97 | 120 |
| 98 QUnit.test('create appends the contextMessage to the body', function(assert) { | 121 QUnit.test('create appends the contextMessage to the body', function(assert) { |
| 99 var done = assert.async(); | 122 var done = assert.async(); |
| 123 var showNotification = this.showNotification; | |
| 100 chrome.notifications.create({'type': 'basic', 'message': 'hello', | 124 chrome.notifications.create({'type': 'basic', 'message': 'hello', |
| 101 'contextMessage': 'world'}, function() { | 125 'contextMessage': 'world'}, function() { |
| 102 assert.equal(Notification.args[0][1].body, 'hello\n\nworld'); | 126 var creator = Notification.called ? Notification : showNotification; |
| 127 assert.equal(creator.args[0][1].body, 'hello\n\nworld'); | |
| 103 done(); | 128 done(); |
| 104 }); | 129 }); |
| 105 }); | 130 }); |
| 106 | 131 |
| 107 QUnit.test('create adds progress text for progress notifications', | 132 QUnit.test('create adds progress text for progress notifications', |
| 108 function(assert) { | 133 function(assert) { |
| 109 var done = assert.async(); | 134 var done = assert.async(); |
| 135 var showNotification = this.showNotification; | |
| 110 chrome.notifications.create({'type': 'progress', 'message': 'hello', | 136 chrome.notifications.create({'type': 'progress', 'message': 'hello', |
| 111 'progress': 15}, function() { | 137 'progress': 15}, function() { |
| 112 assert.equal(Notification.args[0][1].body, 'hello\n\nProgress: 15%'); | 138 var creator = Notification.called ? Notification : showNotification; |
| 139 assert.equal(creator.args[0][1].body, 'hello\n\nProgress: 15%'); | |
| 113 done(); | 140 done(); |
| 114 }); | 141 }); |
| 115 }); | 142 }); |
| 116 | 143 |
| 117 QUnit.test('create warns if an unsupported type is given', function(assert) { | 144 QUnit.test('create warns if an unsupported type is given', function(assert) { |
| 118 var done = assert.async(); | 145 var done = assert.async(); |
| 119 var warn = sandbox.stub(console, 'warn'); | 146 var warn = sandbox.stub(console, 'warn'); |
| 120 chrome.notifications.create({'type': 'faketype'}, function() { | 147 chrome.notifications.create({'type': 'faketype'}, function() { |
| 121 assert.ok(warn.calledOnce); | 148 assert.ok(warn.calledOnce); |
| 122 assert.equal(warn.args[0].join(' '), | 149 assert.equal(warn.args[0].join(' '), |
| 123 'Notification type faketype not supported. Falling back to basic.'); | 150 'Notification type faketype not supported. Falling back to basic.'); |
| 124 done(); | 151 done(); |
| 125 }); | 152 }); |
| 126 }); | 153 }); |
| 127 | 154 |
| 128 QUnit.test('creates a notification with the correct icon',function(assert) { | 155 QUnit.test('creates a notification with the correct icon',function(assert) { |
| 129 var done = assert.async(); | 156 var done = assert.async(); |
| 157 var showNotification = this.showNotification; | |
| 130 chrome.notifications.create({'type': 'basic', 'iconUrl': 'aURL'}, | 158 chrome.notifications.create({'type': 'basic', 'iconUrl': 'aURL'}, |
| 131 function() { | 159 function() { |
| 132 assert.equal(Notification.args[0][1].icon, 'aURL'); | 160 var creator = Notification.called ? Notification : showNotification; |
| 161 assert.equal(creator.args[0][1].icon, 'aURL'); | |
| 133 done(); | 162 done(); |
| 134 } | 163 } |
| 135 ); | 164 ); |
| 136 }); | 165 }); |
| 137 | 166 |
| 138 QUnit.test('passes false to callback if no notification cleared', | 167 QUnit.test('passes false to callback if no notification cleared', |
| 139 function(assert) { | 168 function(assert) { |
| 140 var done = assert.async(); | 169 var done = assert.async(); |
| 141 chrome.notifications.clear('thisiddoesnotexist', function(success) { | 170 chrome.notifications.clear('thisiddoesnotexist', function(success) { |
| 142 assert.notOk(success); | 171 assert.notOk(success); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 QUnit.test( | 223 QUnit.test( |
| 195 'passes DENIED to callback if no permission set', function(assert) { | 224 'passes DENIED to callback if no permission set', function(assert) { |
| 196 Notification.permission = 'default'; | 225 Notification.permission = 'default'; |
| 197 chrome.notifications.getPermissionLevel(function(level) { | 226 chrome.notifications.getPermissionLevel(function(level) { |
| 198 assert.equal(level, chrome.notifications.PermissionLevel.DENIED); | 227 assert.equal(level, chrome.notifications.PermissionLevel.DENIED); |
| 199 }); | 228 }); |
| 200 } | 229 } |
| 201 ); | 230 ); |
| 202 | 231 |
| 203 // TODO(alger): Add tests for onClicked event handler. | 232 // TODO(alger): Add tests for onClicked event handler. |
| OLD | NEW |