Chromium Code Reviews| Index: content/browser/battery_status/battery_monitor_integration_browsertest.cc |
| diff --git a/content/browser/battery_status/battery_monitor_integration_browsertest.cc b/content/browser/battery_status/battery_monitor_integration_browsertest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..17e3eeb9694756bba329ef752b62d289f6fe7432 |
| --- /dev/null |
| +++ b/content/browser/battery_status/battery_monitor_integration_browsertest.cc |
| @@ -0,0 +1,158 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/callback_list.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/synchronization/waitable_event.h" |
|
timvolodine
2014/11/20 16:20:08
do we need this?
|
| +#include "base/thread_task_runner_handle.h" |
| +#include "content/public/browser/content_browser_client.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "content/public/common/content_client.h" |
| +#include "content/public/common/service_registry.h" |
| +#include "content/public/test/content_browser_test.h" |
| +#include "content/public/test/content_browser_test_utils.h" |
| +#include "content/public/test/test_navigation_observer.h" |
| +#include "content/public/test/test_utils.h" |
| +#include "content/shell/browser/shell.h" |
| +#include "content/shell/browser/shell_content_browser_client.h" |
| +#include "device/battery/battery_monitor.mojom.h" |
| +#include "device/battery/battery_status_manager.h" |
| +#include "device/battery/battery_status_service.h" |
|
timvolodine
2014/11/20 16:20:08
no need for this?
ppi
2014/11/21 09:48:06
Done.
|
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| + |
| +// These tests run against a dummy implementation of the BatteryMonitor service. |
| +// That is, they verify that the service implementation is correctly exposed to |
| +// the renderer, whatever the implementation is. |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +typedef base::CallbackList<void(const device::BatteryStatus&)> |
| + BatteryUpdateCallbackList; |
| +typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription; |
| + |
| +// Global battery state used in the tests. |
| +device::BatteryStatus g_battery_status; |
| +// Global list of test battery monitors to notify when |g_battery_status| |
| +// changes. |
| +base::LazyInstance<BatteryUpdateCallbackList> g_callback_list = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +// Updates the global battery state and notifies existing test monitors. |
| +void UpdateBattery(const device::BatteryStatus& battery_status) { |
| + g_battery_status = battery_status; |
| + g_callback_list.Get().Notify(battery_status); |
| +} |
| + |
| +class TestBatteryMonitor : public device::BatteryMonitor { |
|
timvolodine
2014/11/20 16:20:08
nit: I think FakeBatteryMonitor is more common.
ppi
2014/11/21 09:48:06
Done.
|
| + public: |
| + static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { |
| + new TestBatteryMonitor(request.Pass()); |
| + } |
| + |
| + private: |
| + TestBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request) |
| + : subscription_( |
| + g_callback_list.Get().Add(base::Bind(&TestBatteryMonitor::DidChange, |
| + base::Unretained(this)))), |
| + binding_(this, request.Pass()) { |
| + DidChange(g_battery_status); |
| + } |
| + ~TestBatteryMonitor() override {} |
| + |
| + void DidChange(const device::BatteryStatus& battery_status) { |
| + device::BatteryStatusPtr status(device::BatteryStatus::New()); |
| + *status = battery_status; |
| + binding_.client()->DidChange(status.Pass()); |
| + } |
| + |
| + scoped_ptr<BatteryUpdateSubscription> subscription_; |
| + mojo::StrongBinding<BatteryMonitor> binding_; |
| +}; |
| + |
| +// Overrides the default service implementation with the test implementation |
| +// declared above. |
| +class TestContentBrowserClient : public ContentBrowserClient { |
| + public: |
| + void OverridePerProcessMojoServices(ServiceRegistry* registry) override { |
| + registry->AddService(base::Bind(&TestBatteryMonitor::Create)); |
| + } |
| +}; |
| + |
| +class BatteryMonitorIntegrationTest : public ContentBrowserTest { |
| + public: |
| + BatteryMonitorIntegrationTest() {} |
| + |
| + void SetUpOnMainThread() override { |
| + old_client_ = SetBrowserClientForTesting(&test_client_); |
| + } |
| + |
| + void TearDownOnMainThread() override { |
| + SetBrowserClientForTesting(old_client_); |
| + } |
| + |
| + private: |
| + TestContentBrowserClient test_client_; |
| + ContentBrowserClient* old_client_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BatteryMonitorIntegrationTest); |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, |
| + BatteryManagerDefaultValues) { |
| + // From JavaScript request a promise for the battery status information and |
| + // once it resolves check the default values and navigate to #pass. |
| + UpdateBattery(device::BatteryStatus()); |
| + GURL test_url = |
| + GetTestUrl("battery_status", "battery_status_default_test.html"); |
| + NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| + EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, |
| + BatteryManagerResolvePromise) { |
| + // Set the fake battery manager to return predefined battery status values. |
|
timvolodine
2014/11/20 16:20:08
"battery manager" -> "battery monitor"?
ppi
2014/11/21 09:48:06
Done.
|
| + // From JavaScript request a promise for the battery status information and |
| + // once it resolves check the values and navigate to #pass. |
| + device::BatteryStatus status; |
| + status.charging = true; |
| + status.charging_time = 100; |
| + status.discharging_time = std::numeric_limits<double>::infinity(); |
| + status.level = 0.5; |
| + UpdateBattery(status); |
| + |
| + GURL test_url = GetTestUrl("battery_status", |
| + "battery_status_promise_resolution_test.html"); |
| + NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| + EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| +} |
| + |
| +IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, |
| + BatteryManagerWithEventListener) { |
| + // Set the fake battery manager to return default battery status values. |
|
timvolodine
2014/11/20 16:20:09
"battery manager" -> "battery monitor"?
ppi
2014/11/21 09:48:06
Done.
|
| + // From JavaScript request a promise for the battery status information. |
| + // Once it resolves add an event listener for battery level change. Set |
| + // battery level to 0.6 and invoke update. Check that the event listener |
| + // is invoked with the correct value for level and navigate to #pass. |
| + device::BatteryStatus status; |
| + UpdateBattery(status); |
| + |
| + TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); |
| + GURL test_url = |
| + GetTestUrl("battery_status", "battery_status_event_listener_test.html"); |
| + shell()->LoadURL(test_url); |
| + same_tab_observer.Wait(); |
| + EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); |
| + |
| + TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); |
| + status.level = 0.6; |
| + UpdateBattery(status); |
| + same_tab_observer2.Wait(); |
| + EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace content |