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/callback_list.h"
6 #include "base/lazy_instance.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "content/public/browser/content_browser_client.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/content_client.h"
11 #include "content/public/common/service_registry.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/public/test/test_navigation_observer.h"
15 #include "content/public/test/test_utils.h"
16 #include "content/shell/browser/shell.h"
17 #include "content/shell/browser/shell_content_browser_client.h"
18 #include "device/battery/battery_monitor.mojom.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/strong_binding.h"
21 // These tests run against a dummy implementation of the BatteryMonitor service.
22 // That is, they verify that the service implementation is correctly exposed to
23 // the renderer, whatever the implementation is.
29 typedef base::CallbackList
<void(const device::BatteryStatus
&)>
30 BatteryUpdateCallbackList
;
31 typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription
;
33 // Global battery state used in the tests.
34 device::BatteryStatus g_battery_status
;
35 // Global list of test battery monitors to notify when |g_battery_status|
37 base::LazyInstance
<BatteryUpdateCallbackList
> g_callback_list
=
38 LAZY_INSTANCE_INITIALIZER
;
40 // Updates the global battery state and notifies existing test monitors.
41 void UpdateBattery(const device::BatteryStatus
& battery_status
) {
42 g_battery_status
= battery_status
;
43 g_callback_list
.Get().Notify(battery_status
);
46 class FakeBatteryMonitor
: public device::BatteryMonitor
{
48 static void Create(mojo::InterfaceRequest
<BatteryMonitor
> request
) {
49 new FakeBatteryMonitor(request
.Pass());
53 typedef mojo::Callback
<void(device::BatteryStatusPtr
)> BatteryStatusCallback
;
55 FakeBatteryMonitor(mojo::InterfaceRequest
<BatteryMonitor
> request
)
56 : binding_(this, request
.Pass()) {
58 ~FakeBatteryMonitor() override
{}
60 void QueryNextStatus(const BatteryStatusCallback
& callback
) override
{
61 // We don't expect overlapped calls to QueryNextStatus.
62 DCHECK(callback_
.is_null());
68 g_callback_list
.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange
,
69 base::Unretained(this)));
70 // Report initial value.
71 DidChange(g_battery_status
);
75 void DidChange(const device::BatteryStatus
& battery_status
) {
76 if (!callback_
.is_null()) {
77 callback_
.Run(battery_status
.Clone());
82 scoped_ptr
<BatteryUpdateSubscription
> subscription_
;
83 mojo::StrongBinding
<BatteryMonitor
> binding_
;
84 BatteryStatusCallback callback_
;
87 // Overrides the default service implementation with the test implementation
89 class TestContentBrowserClient
: public ContentBrowserClient
{
91 void OverrideRenderProcessMojoServices(ServiceRegistry
* registry
) override
{
92 registry
->AddService(base::Bind(&FakeBatteryMonitor::Create
));
95 #if defined(OS_ANDROID)
96 void GetAdditionalMappedFilesForChildProcess(
97 const base::CommandLine
& command_line
,
99 FileDescriptorInfo
* mappings
) override
{
100 ShellContentBrowserClient::Get()->GetAdditionalMappedFilesForChildProcess(
101 command_line
, child_process_id
, mappings
);
103 #endif // defined(OS_ANDROID)
106 class BatteryMonitorIntegrationTest
: public ContentBrowserTest
{
108 BatteryMonitorIntegrationTest() {}
110 void SetUpOnMainThread() override
{
111 old_client_
= SetBrowserClientForTesting(&test_client_
);
114 void TearDownOnMainThread() override
{
115 SetBrowserClientForTesting(old_client_
);
119 TestContentBrowserClient test_client_
;
120 ContentBrowserClient
* old_client_
;
122 DISALLOW_COPY_AND_ASSIGN(BatteryMonitorIntegrationTest
);
125 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest
, DefaultValues
) {
126 // From JavaScript request a promise for the battery status information and
127 // once it resolves check the default values and navigate to #pass.
128 UpdateBattery(device::BatteryStatus());
130 GetTestUrl("battery_status", "battery_status_default_test.html");
131 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url
, 2);
132 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
135 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest
, ResolvePromise
) {
136 // Set the fake battery monitor to return predefined battery status values.
137 // From JavaScript request a promise for the battery status information and
138 // once it resolves check the values and navigate to #pass.
139 device::BatteryStatus status
;
140 status
.charging
= true;
141 status
.charging_time
= 100;
142 status
.discharging_time
= std::numeric_limits
<double>::infinity();
144 UpdateBattery(status
);
146 GURL test_url
= GetTestUrl("battery_status",
147 "battery_status_promise_resolution_test.html");
148 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url
, 2);
149 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
152 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest
, EventListener
) {
153 // Set the fake battery monitor to return default battery status values.
154 // From JavaScript request a promise for the battery status information.
155 // Once it resolves add an event listener for battery level change. Set
156 // battery level to 0.6 and invoke update. Check that the event listener
157 // is invoked with the correct value for level and navigate to #pass.
158 device::BatteryStatus status
;
159 UpdateBattery(status
);
161 TestNavigationObserver
same_tab_observer(shell()->web_contents(), 2);
163 GetTestUrl("battery_status", "battery_status_event_listener_test.html");
164 shell()->LoadURL(test_url
);
165 same_tab_observer
.Wait();
166 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref());
168 TestNavigationObserver
same_tab_observer2(shell()->web_contents(), 1);
170 UpdateBattery(status
);
171 same_tab_observer2
.Wait();
172 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
177 } // namespace content