Add ICU message format support
[chromium-blink-merge.git] / content / browser / battery_status / battery_monitor_integration_browsertest.cc
blob1f774a0c81bbad750b4c525695721b2119064bb7
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.
25 namespace content {
27 namespace {
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|
36 // changes.
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 {
47 public:
48 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) {
49 new FakeBatteryMonitor(request.Pass());
52 private:
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());
64 callback_ = callback;
66 if (!subscription_) {
67 subscription_ =
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());
78 callback_.reset();
82 scoped_ptr<BatteryUpdateSubscription> subscription_;
83 mojo::StrongBinding<BatteryMonitor> binding_;
84 BatteryStatusCallback callback_;
87 // Overrides the default service implementation with the test implementation
88 // declared above.
89 class TestContentBrowserClient : public ContentBrowserClient {
90 public:
91 void RegisterRenderProcessMojoServices(ServiceRegistry* registry) override {
92 registry->AddService(base::Bind(&FakeBatteryMonitor::Create));
95 #if defined(OS_ANDROID)
96 void GetAdditionalMappedFilesForChildProcess(
97 const base::CommandLine& command_line,
98 int child_process_id,
99 FileDescriptorInfo* mappings,
100 std::map<int, base::MemoryMappedFile::Region>* regions) override {
101 ShellContentBrowserClient::Get()->GetAdditionalMappedFilesForChildProcess(
102 command_line, child_process_id, mappings, regions);
104 #endif // defined(OS_ANDROID)
107 class BatteryMonitorIntegrationTest : public ContentBrowserTest {
108 public:
109 BatteryMonitorIntegrationTest() {}
111 void SetUpOnMainThread() override {
112 old_client_ = SetBrowserClientForTesting(&test_client_);
115 void TearDownOnMainThread() override {
116 SetBrowserClientForTesting(old_client_);
119 private:
120 TestContentBrowserClient test_client_;
121 ContentBrowserClient* old_client_;
123 DISALLOW_COPY_AND_ASSIGN(BatteryMonitorIntegrationTest);
126 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, DefaultValues) {
127 // From JavaScript request a promise for the battery status information and
128 // once it resolves check the default values and navigate to #pass.
129 UpdateBattery(device::BatteryStatus());
130 GURL test_url =
131 GetTestUrl("battery_status", "battery_status_default_test.html");
132 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
133 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
136 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, ResolvePromise) {
137 // Set the fake battery monitor to return predefined battery status values.
138 // From JavaScript request a promise for the battery status information and
139 // once it resolves check the values and navigate to #pass.
140 device::BatteryStatus status;
141 status.charging = true;
142 status.charging_time = 100;
143 status.discharging_time = std::numeric_limits<double>::infinity();
144 status.level = 0.5;
145 UpdateBattery(status);
147 GURL test_url = GetTestUrl("battery_status",
148 "battery_status_promise_resolution_test.html");
149 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
150 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
153 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, EventListener) {
154 // Set the fake battery monitor to return default battery status values.
155 // From JavaScript request a promise for the battery status information.
156 // Once it resolves add an event listener for battery level change. Set
157 // battery level to 0.6 and invoke update. Check that the event listener
158 // is invoked with the correct value for level and navigate to #pass.
159 device::BatteryStatus status;
160 UpdateBattery(status);
162 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
163 GURL test_url =
164 GetTestUrl("battery_status", "battery_status_event_listener_test.html");
165 shell()->LoadURL(test_url);
166 same_tab_observer.Wait();
167 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref());
169 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1);
170 status.level = 0.6;
171 UpdateBattery(status);
172 same_tab_observer2.Wait();
173 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
176 } // namespace
178 } // namespace content