OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 (function() { |
| 6 |
| 7 'use strict'; |
| 8 |
| 9 QUnit.module('ButterBar', { |
| 10 beforeEach: function() { |
| 11 var fixture = document.getElementById('qunit-fixture'); |
| 12 fixture.innerHTML = |
| 13 '<div id="butter-bar" hidden>' + |
| 14 ' <p>' + |
| 15 ' <span id="butter-bar-message"></span>' + |
| 16 ' <a id="butter-bar-dismiss" href="#" tabindex="0">' + |
| 17 ' <img src="icon_cross.webp" class="close-icon">' + |
| 18 ' </a>' + |
| 19 ' </p>' + |
| 20 '</div>'; |
| 21 this.butterBar = new remoting.ButterBar(); |
| 22 chrome.storage = { |
| 23 sync: { |
| 24 get: sinon.stub(), |
| 25 set: sinon.stub(), |
| 26 } |
| 27 }; |
| 28 }, |
| 29 afterEach: function() { |
| 30 if (this.clock) { |
| 31 this.clock.restore(); |
| 32 } |
| 33 } |
| 34 }); |
| 35 |
| 36 QUnit.test('should stay hidden if index==-1', function(assert) { |
| 37 this.butterBar.currentMessage_ = -1; |
| 38 return this.butterBar.init().then(() => { |
| 39 assert.ok(this.butterBar.root_.hidden == true); |
| 40 }); |
| 41 }); |
| 42 |
| 43 QUnit.test('should be shown, yellow and dismissable if index==0', |
| 44 function(assert) { |
| 45 this.butterBar.currentMessage_ = 0; |
| 46 chrome.storage.sync.get.callsArgWith(1, {}); |
| 47 return this.butterBar.init().then(() => { |
| 48 assert.ok(this.butterBar.root_.hidden == false); |
| 49 assert.ok(this.butterBar.dismiss_.hidden == false); |
| 50 assert.ok(!this.butterBar.root_.classList.contains('red')); |
| 51 }); |
| 52 }); |
| 53 |
| 54 QUnit.test('should update storage when shown', function(assert) { |
| 55 this.butterBar.currentMessage_ = 0; |
| 56 this.clock = sinon.useFakeTimers(123); |
| 57 chrome.storage.sync.get.callsArgWith(1, {}); |
| 58 return this.butterBar.init().then(() => { |
| 59 assert.deepEqual(chrome.storage.sync.set.firstCall.args, |
| 60 [{ |
| 61 "message-state": { |
| 62 "hidden": false, |
| 63 "index": 0, |
| 64 "timestamp": 123, |
| 65 } |
| 66 }]); |
| 67 }); |
| 68 }); |
| 69 |
| 70 QUnit.test( |
| 71 'should be shown and should not update local storage if it has already ' + |
| 72 'shown, the timeout has not elapsed and it has not been dismissed', |
| 73 function(assert) { |
| 74 this.butterBar.currentMessage_ = 0; |
| 75 chrome.storage.sync.get.callsArgWith(1, { |
| 76 "message-state": { |
| 77 "hidden": false, |
| 78 "index": 0, |
| 79 "timestamp": 0, |
| 80 } |
| 81 }); |
| 82 this.clock = sinon.useFakeTimers(remoting.ButterBar.kTimeout_); |
| 83 return this.butterBar.init().then(() => { |
| 84 assert.ok(this.butterBar.root_.hidden == false); |
| 85 assert.ok(!chrome.storage.sync.set.called); |
| 86 }); |
| 87 }); |
| 88 |
| 89 QUnit.test('should stay hidden if the timeout has elapsed', function(assert) { |
| 90 this.butterBar.currentMessage_ = 0; |
| 91 chrome.storage.sync.get.callsArgWith(1, { |
| 92 "message-state": { |
| 93 "hidden": false, |
| 94 "index": 0, |
| 95 "timestamp": 0, |
| 96 } |
| 97 }); |
| 98 this.clock = sinon.useFakeTimers(remoting.ButterBar.kTimeout_+ 1); |
| 99 return this.butterBar.init().then(() => { |
| 100 assert.ok(this.butterBar.root_.hidden == true); |
| 101 }); |
| 102 }); |
| 103 |
| 104 |
| 105 QUnit.test('should stay hidden if it was previously dismissed', |
| 106 function(assert) { |
| 107 this.butterBar.currentMessage_ = 0; |
| 108 chrome.storage.sync.get.callsArgWith(1, { |
| 109 "message-state": { |
| 110 "hidden": true, |
| 111 "index": 0, |
| 112 "timestamp": 0, |
| 113 } |
| 114 }); |
| 115 this.clock = sinon.useFakeTimers(0); |
| 116 return this.butterBar.init().then(() => { |
| 117 assert.ok(this.butterBar.root_.hidden == true); |
| 118 }); |
| 119 }); |
| 120 |
| 121 |
| 122 QUnit.test('should be shown if the index has increased', function(assert) { |
| 123 this.butterBar.currentMessage_ = 1; |
| 124 chrome.storage.sync.get.callsArgWith(1, { |
| 125 "message-state": { |
| 126 "hidden": true, |
| 127 "index": 0, |
| 128 "timestamp": 0, |
| 129 } |
| 130 }); |
| 131 this.clock = sinon.useFakeTimers(remoting.ButterBar.kTimeout_ + 1); |
| 132 return this.butterBar.init().then(() => { |
| 133 assert.ok(this.butterBar.root_.hidden == false); |
| 134 }); |
| 135 }); |
| 136 |
| 137 QUnit.test('should be red and not dismissable for the final message', |
| 138 function(assert) { |
| 139 this.butterBar.currentMessage_ = 3; |
| 140 chrome.storage.sync.get.callsArgWith(1, {}); |
| 141 return this.butterBar.init().then(() => { |
| 142 assert.ok(this.butterBar.root_.hidden == false); |
| 143 assert.ok(this.butterBar.dismiss_.hidden == true); |
| 144 assert.ok(this.butterBar.root_.classList.contains('red')); |
| 145 }); |
| 146 }); |
| 147 |
| 148 QUnit.test('dismiss button updates local storage', function(assert) { |
| 149 this.butterBar.currentMessage_ = 0; |
| 150 chrome.storage.sync.get.callsArgWith(1, {}); |
| 151 return this.butterBar.init().then(() => { |
| 152 this.clock = sinon.useFakeTimers(0); |
| 153 this.butterBar.dismiss_.click(); |
| 154 // The first call is in response to showing the message; the second is in |
| 155 // response to dismissing the message. |
| 156 assert.deepEqual(chrome.storage.sync.set.secondCall.args, |
| 157 [{ |
| 158 "message-state": { |
| 159 "hidden": true, |
| 160 "index": 0, |
| 161 "timestamp": 0, |
| 162 } |
| 163 }]); |
| 164 }); |
| 165 }); |
| 166 |
| 167 }()); |
OLD | NEW |