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.
4
5 #include "base/command_line.h"
6 #include "base/synchronization/waitable_event.h"
7 #include "content/browser/battery_status/battery_status_manager.h"
8 #include "content/browser/battery_status/battery_status_service.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/public/test/test_navigation_observer.h"
14 #include "content/public/test/test_utils.h"
15 #include "content/shell/browser/shell.h"
16
17 namespace content {
18
19 namespace {
20
21 class FakeBatteryManager : public BatteryStatusManager {
22 public:
FakeBatteryManager(const BatteryStatusService::BatteryUpdateCallback & callback)23 explicit FakeBatteryManager(
24 const BatteryStatusService::BatteryUpdateCallback& callback)
25 : callback_(callback), battery_status_available_(true), started_(false) {}
~FakeBatteryManager()26 virtual ~FakeBatteryManager() { }
27
28 // Methods from BatteryStatusManager.
StartListeningBatteryChange()29 virtual bool StartListeningBatteryChange() OVERRIDE {
30 started_ = true;
31 if (battery_status_available_)
32 InvokeUpdateCallback();
33 return battery_status_available_;
34 }
35
StopListeningBatteryChange()36 virtual void StopListeningBatteryChange() OVERRIDE { }
37
InvokeUpdateCallback()38 void InvokeUpdateCallback() {
39 callback_.Run(status_);
40 }
41
set_battery_status(const blink::WebBatteryStatus & status)42 void set_battery_status(const blink::WebBatteryStatus& status) {
43 status_ = status;
44 }
45
set_battery_status_available(bool value)46 void set_battery_status_available(bool value) {
47 battery_status_available_ = value;
48 }
49
started()50 bool started() {
51 return started_;
52 }
53
54 private:
55 BatteryStatusService::BatteryUpdateCallback callback_;
56 bool battery_status_available_;
57 bool started_;
58 blink::WebBatteryStatus status_;
59
60 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager);
61 };
62
63 class BatteryStatusBrowserTest : public ContentBrowserTest {
64 public:
BatteryStatusBrowserTest()65 BatteryStatusBrowserTest()
66 : battery_manager_(0),
67 battery_service_(0),
68 io_loop_finished_event_(false, false) {
69 }
70
SetUpCommandLine(CommandLine * command_line)71 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
72 command_line->AppendSwitch(
73 switches::kEnableExperimentalWebPlatformFeatures);
74 }
75
SetUpOnMainThread()76 virtual void SetUpOnMainThread() OVERRIDE {
77 BrowserThread::PostTask(
78 BrowserThread::IO, FROM_HERE,
79 base::Bind(&BatteryStatusBrowserTest::SetUpOnIOThread, this));
80 io_loop_finished_event_.Wait();
81 }
82
SetUpOnIOThread()83 void SetUpOnIOThread() {
84 battery_service_ = BatteryStatusService::GetInstance();
85 battery_manager_ = new FakeBatteryManager(
86 battery_service_->GetUpdateCallbackForTesting());
87 battery_service_->SetBatteryManagerForTesting(battery_manager_);
88 io_loop_finished_event_.Signal();
89 }
90
TearDown()91 virtual void TearDown() OVERRIDE {
92 battery_service_->SetBatteryManagerForTesting(0);
93 }
94
battery_manager()95 FakeBatteryManager* battery_manager() {
96 return battery_manager_;
97 }
98
99 private:
100 FakeBatteryManager* battery_manager_;
101 BatteryStatusService* battery_service_;
102 base::WaitableEvent io_loop_finished_event_;
103
104 DISALLOW_COPY_AND_ASSIGN(BatteryStatusBrowserTest);
105 };
106
IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest,BatteryManagerDefaultValues)107 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerDefaultValues) {
108 // Set the fake battery manager to return false on start. From JavaScript
109 // request a promise for the battery status information and once it resolves
110 // check the default values and navigate to #pass.
111 battery_manager()->set_battery_status_available(false);
112 GURL test_url = GetTestUrl(
113 "battery_status", "battery_status_default_test.html");
114 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
115 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
116 EXPECT_TRUE(battery_manager()->started());
117 }
118
IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest,BatteryManagerResolvePromise)119 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerResolvePromise) {
120 // Set the fake battery manager to return predefined battery status values.
121 // From JavaScript request a promise for the battery status information and
122 // once it resolves check the values and navigate to #pass.
123 blink::WebBatteryStatus status;
124 status.charging = true;
125 status.chargingTime = 100;
126 status.dischargingTime = std::numeric_limits<double>::infinity();
127 status.level = 0.5;
128 battery_manager()->set_battery_status(status);
129
130 GURL test_url = GetTestUrl(
131 "battery_status", "battery_status_promise_resolution_test.html");
132 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
133 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
134 EXPECT_TRUE(battery_manager()->started());
135 }
136
IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest,BatteryManagerWithEventListener)137 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest,
138 BatteryManagerWithEventListener) {
139 // Set the fake battery manager to return default battery status values.
140 // From JavaScript request a promise for the battery status information.
141 // Once it resolves add an event listener for battery level change. Set
142 // battery level to 0.6 and invoke update. Check that the event listener
143 // is invoked with the correct value for level and navigate to #pass.
144 blink::WebBatteryStatus status;
145 battery_manager()->set_battery_status(status);
146
147 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
148 GURL test_url = GetTestUrl(
149 "battery_status", "battery_status_event_listener_test.html");
150 shell()->LoadURL(test_url);
151 same_tab_observer.Wait();
152 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref());
153
154 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1);
155 status.level = 0.6;
156 battery_manager()->set_battery_status(status);
157 battery_manager()->InvokeUpdateCallback();
158 same_tab_observer2.Wait();
159 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
160 EXPECT_TRUE(battery_manager()->started());
161 }
162
163 } // namespace
164
165 } // namespace content
166