Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / chromeos / power_overlay_browsertest.js
blobce1b01a64596998684640a8f7f4d30fb586d0da2
1 // Copyright 2015 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.
5 function PowerOverlayWebUITest() {}
7 PowerOverlayWebUITest.prototype = {
8 __proto__: testing.Test.prototype,
10 browsePreload: 'chrome://settings-frame/',
12 commandLineSwitches: [{
13 switchName: 'enable-power-overlay',
14 }],
16 /** @override */
17 preLoad: function() {
18 this.makeAndRegisterMockHandler([
19 'updatePowerStatus',
20 'setPowerSource',
21 ]);
22 this.mockHandler.expects(atLeastOnce()).updatePowerStatus();
25 /**
26 * Sets power sources using a deep copy of |sources|.
27 * @param {Array<Object>} sources
28 * @param {string} sourceId
29 * @param {bool} isUsbCharger
30 * @param {bool} isCalculating
32 setPowerSources: function(sources, sourceId, isUsbCharger, isCalculating) {
33 var sourcesCopy = sources.map(function(source) {
34 return Object.assign({}, source);
35 });
36 options.PowerOverlay.setPowerSources(
37 sourcesCopy, sourceId, isUsbCharger, isCalculating);
40 /**
41 * Simulates the user selecting a power source, verifying that the overlay
42 * calls setPowerSource.
43 * @param {string} sourceId
45 selectPowerSource: function(sourceId) {
46 this.mockHandler.expects(once()).setPowerSource(eq(sourceId));
47 $('power-source-dropdown').value = sourceId;
48 expectTrue(cr.dispatchSimpleEvent($('power-source-dropdown'), 'change'));
51 /**
52 * Checks that the sources dropdown is visible.
53 * @param {string} sourceId The ID of the source that should be selected.
55 checkSource: function(sourceId) {
56 expectTrue($('power-source-charger').hidden);
57 expectFalse($('power-sources').hidden);
58 expectEquals(sourceId, $('power-source-dropdown').value);
61 checkNoSources: function() {
62 expectTrue($('power-source-charger').hidden);
63 expectTrue($('power-sources').hidden);
66 checkDedicatedCharger: function() {
67 expectFalse($('power-source-charger').hidden);
68 expectTrue($('power-sources').hidden);
72 TEST_F('PowerOverlayWebUITest', 'testNoPowerSources', function() {
73 assertEquals(this.browsePreload, document.location.href);
74 this.mockHandler.expects(never()).setPowerSource();
75 $('power-settings-link').click();
77 // This should be the initial state.
78 this.checkNoSources();
80 // Setting an empty sources list shouldn't change the state.
81 this.setPowerSources([], '', false, false);
82 this.checkNoSources();
83 });
85 TEST_F('PowerOverlayWebUITest', 'testDedicatedCharger', function() {
86 assertEquals(this.browsePreload, document.location.href);
87 this.mockHandler.expects(never()).setPowerSource();
88 $('power-settings-link').click();
90 var fakeSources = [{
91 id: 'source1',
92 description: 'Left port',
93 type: options.PowerStatusDeviceType.DEDICATED_CHARGER,
94 }];
96 this.setPowerSources(fakeSources, 'source1', false, false);
97 this.checkDedicatedCharger();
99 // Remove the charger.
100 this.setPowerSources([], '');
101 this.checkNoSources();
103 // Set a low-powered charger.
104 this.setPowerSources(fakeSources, 'source1', true, false);
105 this.checkDedicatedCharger();
108 TEST_F('PowerOverlayWebUITest', 'testSingleSource', function() {
109 assertEquals(this.browsePreload, document.location.href);
110 $('power-settings-link').click();
112 var fakeSources = [{
113 id: 'source1',
114 description: 'Left port',
115 type: options.PowerStatusDeviceType.DUAL_ROLE_USB,
118 this.setPowerSources(fakeSources, '', false, false);
119 this.checkSource('');
121 this.selectPowerSource('source1');
122 this.checkSource('source1');
124 // Remove the device.
125 this.setPowerSources([], '', false, false);
126 this.checkNoSources();
129 TEST_F('PowerOverlayWebUITest', 'testMultipleSources', function() {
130 assertEquals(this.browsePreload, document.location.href);
131 $('power-settings-link').click();
133 var fakeSources = [{
134 id: 'source1',
135 description: 'Left port',
136 type: options.PowerStatusDeviceType.DUAL_ROLE_USB,
137 }, {
138 id: 'source2',
139 description: 'Right port',
140 type: options.PowerStatusDeviceType.DUAL_ROLE_USB,
141 }, {
142 id: 'source3',
143 description: 'Front port',
144 type: options.PowerStatusDeviceType.DUAL_ROLE_USB,
145 }, {
146 id: 'source4',
147 description: 'Rear port',
148 type: options.PowerStatusDeviceType.DUAL_ROLE_USB,
151 // Use a dual-role device.
152 this.setPowerSources(fakeSources, 'source2', false, false);
153 this.checkSource('source2');
155 // Use a USB charger.
156 this.setPowerSources(fakeSources, 'source3', true, false);
157 this.checkSource('source3');
159 // Remove the currently used device.
160 fakeSources.splice(2, 1);
161 this.setPowerSources(fakeSources, 'source4', false, false);
162 this.checkSource('source4');
164 // Do not charge (use battery).
165 this.setPowerSources(fakeSources, '', false, false);
166 this.checkSource('');
168 // The user selects a device.
169 this.selectPowerSource('source1');
171 // The user selects the battery.
172 this.selectPowerSource('');