test_TimerFeed.js (5315B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 const lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(this, { 8 actionTypes: "resource://newtab/common/Actions.mjs", 9 TimerFeed: "resource://newtab/lib/Widgets/TimerFeed.sys.mjs", 10 sinon: "resource://testing-common/Sinon.sys.mjs", 11 }); 12 13 ChromeUtils.defineLazyGetter(lazy, "gNewTabStrings", () => { 14 return new Localization(["browser/newtab/newtab.ftl"], true); 15 }); 16 17 const PREF_TIMER_ENABLED = "widgets.timer.enabled"; 18 const PREF_SYSTEM_TIMER_ENABLED = "widgets.system.timer.enabled"; 19 const PREF_TIMER_SHOW_NOTIFICATIONS = 20 "widgets.focusTimer.showSystemNotifications"; 21 22 add_task(async function test_construction() { 23 let feed = new TimerFeed(); 24 25 feed.store = { 26 getState() { 27 return this.state; 28 }, 29 dispatch: sinon.spy(), 30 state: { 31 Prefs: { 32 values: { 33 [PREF_SYSTEM_TIMER_ENABLED]: false, 34 }, 35 }, 36 }, 37 }; 38 39 info("TimerFeed constructor should create initial values"); 40 41 Assert.ok(feed, "Could construct a TimerFeed"); 42 Assert.ok(!feed.loaded, "TimerFeed is not loaded"); 43 Assert.ok(!feed.enabled); 44 }); 45 46 add_task(async function test_onAction_INIT() { 47 let feed = new TimerFeed(); 48 49 feed.store = { 50 getState() { 51 return this.state; 52 }, 53 dispatch: sinon.spy(), 54 state: { 55 Prefs: { 56 values: { 57 [PREF_TIMER_ENABLED]: true, 58 [PREF_SYSTEM_TIMER_ENABLED]: true, 59 }, 60 }, 61 }, 62 }; 63 64 info("TimerFeed.onAction INIT should set initialized"); 65 66 await feed.onAction({ 67 type: actionTypes.INIT, 68 }); 69 70 Assert.ok(feed.initialized); 71 }); 72 73 add_task(async function test_isEnabled() { 74 let feed = new TimerFeed(); 75 76 feed.store = { 77 getState() { 78 return this.state; 79 }, 80 dispatch: sinon.spy(), 81 state: { 82 Prefs: { 83 values: { 84 [PREF_TIMER_ENABLED]: true, 85 [PREF_SYSTEM_TIMER_ENABLED]: true, 86 }, 87 }, 88 }, 89 }; 90 91 info("TimerFeed should be enabled via system pref"); 92 Assert.ok(feed.enabled); 93 }); 94 95 add_task(async function test_isEnabled() { 96 let feed = new TimerFeed(); 97 98 feed.store = { 99 getState() { 100 return this.state; 101 }, 102 dispatch: sinon.spy(), 103 state: { 104 Prefs: { 105 values: { 106 [PREF_TIMER_ENABLED]: true, 107 trainhopConfig: { 108 widgets: { 109 enabled: true, 110 listsEnabled: false, 111 timerEnabled: true, 112 }, 113 }, 114 }, 115 }, 116 }, 117 }; 118 119 info("TimerFeed should be enabled via trainhopConfig"); 120 Assert.ok(feed.enabled); 121 }); 122 123 add_task(async function test_isEnabled() { 124 let feed = new TimerFeed(); 125 126 feed.store = { 127 getState() { 128 return this.state; 129 }, 130 dispatch: sinon.spy(), 131 state: { 132 Prefs: { 133 values: { 134 [PREF_TIMER_ENABLED]: true, 135 widgetsConfig: { 136 enabled: true, 137 listsEnabled: false, 138 timerEnabled: true, 139 }, 140 }, 141 }, 142 }, 143 }; 144 145 info("TimerFeed should be enabled via trainhowidgetsConfigpConfig"); 146 Assert.ok(feed.enabled); 147 }); 148 149 add_task(async function test_system_notification_on_focus_end() { 150 const [titleMessage, bodyMessage] = await lazy.gNewTabStrings.formatMessages([ 151 { id: "newtab-widget-timer-notification-title" }, 152 { id: "newtab-widget-timer-notification-focus" }, 153 ]); 154 155 const feed = new TimerFeed(); 156 157 feed.store = { 158 getState() { 159 return this.state; 160 }, 161 dispatch: sinon.spy(), 162 state: { 163 Prefs: { 164 values: { 165 [PREF_TIMER_ENABLED]: true, 166 [PREF_SYSTEM_TIMER_ENABLED]: true, 167 [PREF_TIMER_SHOW_NOTIFICATIONS]: true, 168 }, 169 }, 170 TimerWidget: {}, 171 }, 172 }; 173 174 feed.showSystemNotification = sinon.spy(); 175 176 await feed.onAction({ 177 type: actionTypes.WIDGETS_TIMER_END, 178 data: { duration: 1500, timerType: "focus" }, 179 }); 180 181 Assert.ok( 182 feed.showSystemNotification.calledOnce, 183 "TimerFeed WIDGETS_TIMER_END event should show notification" 184 ); 185 186 const [title, text] = feed.showSystemNotification.firstCall.args; 187 Assert.equal(title, titleMessage?.value); 188 Assert.equal(text, bodyMessage?.value); 189 }); 190 191 add_task(async function test_system_notification_on_break_end() { 192 const [titleMessage, bodyMessage] = await lazy.gNewTabStrings.formatMessages([ 193 { id: "newtab-widget-timer-notification-title" }, 194 { id: "newtab-widget-timer-notification-break" }, 195 ]); 196 197 const feed = new TimerFeed(); 198 199 feed.store = { 200 getState() { 201 return this.state; 202 }, 203 dispatch: sinon.spy(), 204 state: { 205 Prefs: { 206 values: { 207 [PREF_TIMER_ENABLED]: true, 208 [PREF_SYSTEM_TIMER_ENABLED]: true, 209 [PREF_TIMER_SHOW_NOTIFICATIONS]: true, 210 }, 211 }, 212 TimerWidget: {}, 213 }, 214 }; 215 216 feed.showSystemNotification = sinon.spy(); 217 218 await feed.onAction({ 219 type: actionTypes.WIDGETS_TIMER_END, 220 data: { duration: 1500, timerType: "break" }, 221 }); 222 223 Assert.ok( 224 feed.showSystemNotification.calledOnce, 225 "TimerFeed WIDGETS_TIMER_END event should show notification" 226 ); 227 228 const [title, text] = feed.showSystemNotification.firstCall.args; 229 Assert.equal(title, titleMessage?.value); 230 Assert.equal(text, bodyMessage?.value); 231 });