Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / battery_status / battery_monitor_impl_browsertest.cc
blob51fe83db8e1bf1f69c0fc7aaebfc5b9bc5bc4f0f
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.
5 #include "base/thread_task_runner_handle.h"
6 #include "content/public/browser/web_contents.h"
7 #include "content/public/test/content_browser_test.h"
8 #include "content/public/test/content_browser_test_utils.h"
9 #include "content/public/test/test_navigation_observer.h"
10 #include "content/public/test/test_utils.h"
11 #include "content/shell/browser/shell.h"
12 #include "device/battery/battery_status_manager.h"
13 #include "device/battery/battery_status_service.h"
15 // These tests run against the default implementation of the BatteryMonitor
16 // service, with a dummy BatteryManager set as a source of the battery
17 // information. They can be run only on platforms that use the default service
18 // implementation, ie. on the platforms where BatteryStatusService is used.
20 namespace content {
22 namespace {
24 class FakeBatteryManager : public device::BatteryStatusManager {
25 public:
26 explicit FakeBatteryManager(
27 const device::BatteryStatusService::BatteryUpdateCallback& callback)
28 : callback_(callback), battery_status_available_(true), started_(false) {}
29 ~FakeBatteryManager() override {}
31 // Methods from BatteryStatusManager.
32 bool StartListeningBatteryChange() override {
33 started_ = true;
34 if (battery_status_available_)
35 InvokeUpdateCallback();
36 return battery_status_available_;
39 void StopListeningBatteryChange() override {}
41 void InvokeUpdateCallback() {
42 // Invoke asynchronously to mimic the OS-specific battery managers.
43 base::ThreadTaskRunnerHandle::Get()->PostTask(
44 FROM_HERE, base::Bind(callback_, status_));
47 void set_battery_status(const device::BatteryStatus& status) {
48 status_ = status;
51 void set_battery_status_available(bool value) {
52 battery_status_available_ = value;
55 bool started() { return started_; }
57 private:
58 device::BatteryStatusService::BatteryUpdateCallback callback_;
59 bool battery_status_available_;
60 bool started_;
61 device::BatteryStatus status_;
63 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager);
66 class BatteryMonitorImplTest : public ContentBrowserTest {
67 public:
68 BatteryMonitorImplTest() : battery_manager_(NULL), battery_service_(NULL) {}
70 void SetUpOnMainThread() override {
71 battery_service_ = device::BatteryStatusService::GetInstance();
73 // We keep a raw pointer to the FakeBatteryManager, which we expect to
74 // remain valid for the lifetime of the BatteryStatusService.
75 scoped_ptr<FakeBatteryManager> battery_manager(new FakeBatteryManager(
76 battery_service_->GetUpdateCallbackForTesting()));
77 battery_manager_ = battery_manager.get();
79 battery_service_->SetBatteryManagerForTesting(battery_manager.Pass());
82 void TearDown() override {
83 battery_service_->SetBatteryManagerForTesting(
84 scoped_ptr<device::BatteryStatusManager>());
85 battery_manager_ = NULL;
88 FakeBatteryManager* battery_manager() { return battery_manager_; }
90 private:
91 FakeBatteryManager* battery_manager_;
92 device::BatteryStatusService* battery_service_;
94 DISALLOW_COPY_AND_ASSIGN(BatteryMonitorImplTest);
97 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, BatteryManagerDefaultValues) {
98 // Set the fake battery manager to return false on start. From JavaScript
99 // request a promise for the battery status information and once it resolves
100 // check the default values and navigate to #pass.
101 battery_manager()->set_battery_status_available(false);
102 GURL test_url =
103 GetTestUrl("battery_status", "battery_status_default_test.html");
104 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
105 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
106 EXPECT_TRUE(battery_manager()->started());
109 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, BatteryManagerResolvePromise) {
110 // Set the fake battery manager to return predefined battery status values.
111 // From JavaScript request a promise for the battery status information and
112 // once it resolves check the values and navigate to #pass.
113 device::BatteryStatus status;
114 status.charging = true;
115 status.charging_time = 100;
116 status.discharging_time = std::numeric_limits<double>::infinity();
117 status.level = 0.5;
118 battery_manager()->set_battery_status(status);
120 GURL test_url = GetTestUrl("battery_status",
121 "battery_status_promise_resolution_test.html");
122 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
123 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
124 EXPECT_TRUE(battery_manager()->started());
127 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest,
128 BatteryManagerWithEventListener) {
129 // Set the fake battery manager to return default battery status values.
130 // From JavaScript request a promise for the battery status information.
131 // Once it resolves add an event listener for battery level change. Set
132 // battery level to 0.6 and invoke update. Check that the event listener
133 // is invoked with the correct value for level and navigate to #pass.
134 device::BatteryStatus status;
135 battery_manager()->set_battery_status(status);
137 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
138 GURL test_url =
139 GetTestUrl("battery_status", "battery_status_event_listener_test.html");
140 shell()->LoadURL(test_url);
141 same_tab_observer.Wait();
142 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref());
144 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1);
145 status.level = 0.6;
146 battery_manager()->set_battery_status(status);
147 battery_manager()->InvokeUpdateCallback();
148 same_tab_observer2.Wait();
149 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
150 EXPECT_TRUE(battery_manager()->started());
153 } // namespace
155 } // namespace content