Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: third_party/WebKit/LayoutTests/battery-status/multiple-windows-page-visibility.html

Issue 1836203002: Change the battery-status layout tests to use JS mocks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3 <body> 3 <body>
4 <script src="../resources/js-test.js"></script> 4 <script src="../resources/js-test.js"></script>
5 <script src="../resources/mojo-helpers.js"></script>
6 <script src="resources/mock-battery-monitor.js"></script>
5 <script> 7 <script>
6 description("Test multiple windows with page visibility."); 8 description("Test multiple windows with page visibility.");
7 9
8 if (!window.testRunner) 10 if (!window.testRunner)
9 debug('This test cannot be run without the TestRunner'); 11 debug('This test cannot be run without the TestRunner');
10 if (!window.internals)
11 debug('This test cannot be run without the window.internals');
12 12
13 // Clean-up any unused battery manager objects from previous tests. 13 // Clean-up any unused battery manager objects from previous tests.
14 gc(); 14 gc();
15 jsTestIsAsync = true; 15 jsTestIsAsync = true;
16 testRunner.waitUntilDone(); 16 testRunner.waitUntilDone();
17 testRunner.setCanOpenWindows(); 17 testRunner.setCanOpenWindows();
18 testRunner.setCloseRemainingWindowsWhenComplete(true); 18 testRunner.setCloseRemainingWindowsWhenComplete(true);
19 19
20 var mockBatteryInfo;
21 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, leve l) {
22 mockBatteryInfo = { charging: charging,
23 chargingTime: chargingTime,
24 dischargingTime: dischargingTime,
25 level: level };
26 window.internals.updateBatteryStatus(charging, chargingTime, dischargingTime , level);
27 }
28
29 // compare obtained battery values with the mock values
30 function checkBatteryInfo(batteryManager) {
31 batteryInfo = batteryManager;
32 shouldBeDefined("batteryInfo");
33 shouldBeDefined("mockBatteryInfo");
34 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
35 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
36 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
37 shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
38 }
39
40 function batteryStatusFailure() {
41 testFailed('failed to successfully resolve the promise');
42 setTimeout(finishJSTest, 0);
43 }
44
45 var battery; 20 var battery;
46 function batteryStatusSuccess(batteryManager) { 21 function batteryStatusSuccess(batteryManager) {
47 battery = batteryManager; 22 battery = batteryManager;
48 debug('resolution in window'); 23 debug('resolution in window');
49 checkBatteryInfo(battery); 24 testIfBatteryStatusIsUpToDate(battery);
50 battery.addEventListener('levelchange', failAndFinish); 25 battery.addEventListener('levelchange', failAndFinish);
51 26
52 firstWindow = window; 27 firstWindow = window;
53 secondWindow = window.open(""); 28 secondWindow = window.open("");
54 secondWindow.navigator.getBattery().then( 29 secondWindow.navigator.getBattery().then(
55 function(battery2) { 30 function(battery2) {
56 debug('resolution in secondWindow'); 31 debug('resolution in secondWindow');
57 checkBatteryInfo(battery2); 32 testIfBatteryStatusIsUpToDate(battery2);
58 setTimeout(fireNewMockLevel, 0); 33 setTimeout(fireNewMockLevel, 0);
59 }, batteryStatusFailure); 34 }, batteryStatusFailure);
60 35
61 window.testRunner.setPageVisibility("hidden"); 36 window.testRunner.setPageVisibility("hidden");
62 debug("first window: page is hidden"); 37 debug("first window: page is hidden");
63 } 38 }
64 39
65 function fireNewMockLevel() { 40 function fireNewMockLevel() {
66 setAndFireMockBatteryInfo(false, 10, 20, 0.6); 41 setAndFireMockBatteryInfo(false, 10, 20, 0.6);
67 proceedToVisible(); 42 proceedToVisible();
68 } 43 }
69 44
70 function proceedToVisible() { 45 function proceedToVisible() {
71 battery.removeEventListener('levelchange', failAndFinish); 46 battery.removeEventListener('levelchange', failAndFinish);
72 battery.addEventListener('levelchange', onLevelChange); 47 battery.addEventListener('levelchange', onLevelChange);
73 testRunner.setPageVisibility("visible"); 48 testRunner.setPageVisibility("visible");
74 debug("first window: page is visible"); 49 debug("first window: page is visible");
75 } 50 }
76 51
77 function onLevelChange() { 52 function onLevelChange() {
78 checkBatteryInfo(battery); 53 testIfBatteryStatusIsUpToDate(battery);
79 battery.removeEventListener('levelchange', onLevelChange); 54 battery.removeEventListener('levelchange', onLevelChange);
80 setTimeout(finishJSTest, 0); 55 setTimeout(finishJSTest, 0);
81 } 56 }
82 57
83 function failAndFinish() { 58 function failAndFinish() {
84 testFailed('received event while the page was hidden'); 59 testFailed('received event while the page was hidden');
85 setTimeout(finishJSTest, 0); 60 setTimeout(finishJSTest, 0);
86 } 61 }
87 62
88 debug("first window: page is visible"); 63 debug("first window: page is visible");
89 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure); 64 mockBatteryMonitorReady.then(() => {
90 setAndFireMockBatteryInfo(false, 10, 20, 0.5); 65 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
66 setAndFireMockBatteryInfo(false, 10, 20, 0.5);
67 });
91 </script> 68 </script>
92 </body> 69 </body>
93 </html> 70 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698