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

Unified Diff: third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js

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, 9 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js
diff --git a/third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js b/third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js
new file mode 100644
index 0000000000000000000000000000000000000000..0f7ed9ca31a7d5866222d86cb0ccced95b12279a
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/battery-status/resources/mock-battery-monitor.js
@@ -0,0 +1,86 @@
+"use strict";
+
+let mockBatteryMonitor = loadMojoModules(
+ 'mockBatteryMonitor',
+ ['device/battery/battery_monitor.mojom',
+ 'device/battery/battery_status.mojom',
+ 'mojo/public/js/router',
+ ]).then(mojo => {
+ let [batteryMonitor, batteryStatus, router] = mojo.modules;
+
+ class MockBatteryMonitor extends batteryMonitor.BatteryMonitor.stubClass {
+ constructor(serviceRegistry) {
+ super();
+ serviceRegistry.addServiceOverrideForTesting(
+ batteryMonitor.BatteryMonitor.name,
+ handle => this.connect_(handle));
+
+ this.serviceRegistry_ = serviceRegistry;
+ this.pendingRequests_ = [];
+ this.status_ = null;
+ }
+
+ connect_(handle) {
+ this.router_ = new router.Router(handle);
+ this.router_.setIncomingReceiver(this);
+ }
+
+ queryNextStatus() {
+ let result = new Promise(resolve => this.pendingRequests_.push(resolve));
+ this.runCallbacks_();
+ return result;
+ }
+
+ updateBatteryStatus(charging, chargingTime, dischargingTime, level) {
+ this.status_ = new batteryStatus.BatteryStatus();
+ this.status_.charging = charging;
+ this.status_.charging_time = chargingTime;
+ this.status_.discharging_time = dischargingTime;
+ this.status_.level = level;
+ this.runCallbacks_();
+ }
+
+ runCallbacks_() {
+ if (!this.status_ || !this.pendingRequests_.length)
+ return;
+
+ while (this.pendingRequests_.length) {
+ this.pendingRequests_.pop()({status: this.status_});
+ }
+ this.status_ = null;
+ }
+ }
+ return new MockBatteryMonitor(mojo.serviceRegistry);
+});
+
+let batteryInfo;
+let lastSetMockBatteryInfo;
+
+function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime,
+ level) {
+ lastSetMockBatteryInfo = { charging: charging,
+ chargingTime: chargingTime,
+ dischargingTime: dischargingTime,
+ level: level };
+ mockBatteryMonitor.then(mock => mock.updateBatteryStatus(
+ charging, chargingTime, dischargingTime, level));
+}
+
+// compare obtained battery values with the mock values
+function testIfBatteryStatusIsUpToDate(batteryManager) {
+ batteryInfo = batteryManager;
+ shouldBeDefined("batteryInfo");
+ shouldBeDefined("lastSetMockBatteryInfo");
+ shouldBe('batteryInfo.charging', 'lastSetMockBatteryInfo.charging');
+ shouldBe('batteryInfo.chargingTime', 'lastSetMockBatteryInfo.chargingTime');
+ shouldBe('batteryInfo.dischargingTime',
+ 'lastSetMockBatteryInfo.dischargingTime');
+ shouldBe('batteryInfo.level', 'lastSetMockBatteryInfo.level');
+}
+
+function batteryStatusFailure() {
+ testFailed('failed to successfully resolve the promise');
+ setTimeout(finishJSTest, 0);
+}
+
+var mockBatteryMonitorReady = mockBatteryMonitor.then();

Powered by Google App Engine
This is Rietveld 408576698