OLD | NEW |
(Empty) | |
| 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/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 "mojo/public/cpp/bindings/strong_binding.h" |
| 20 |
| 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. |
| 24 |
| 25 namespace content { |
| 26 |
| 27 namespace { |
| 28 |
| 29 typedef base::CallbackList<void(const device::BatteryStatus&)> |
| 30 BatteryUpdateCallbackList; |
| 31 typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription; |
| 32 |
| 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; |
| 39 |
| 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); |
| 44 } |
| 45 |
| 46 class FakeBatteryMonitor : public device::BatteryMonitor { |
| 47 public: |
| 48 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { |
| 49 new FakeBatteryMonitor(request.Pass()); |
| 50 } |
| 51 |
| 52 private: |
| 53 FakeBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request) |
| 54 : subscription_( |
| 55 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange, |
| 56 base::Unretained(this)))), |
| 57 binding_(this, request.Pass()) { |
| 58 DidChange(g_battery_status); |
| 59 } |
| 60 ~FakeBatteryMonitor() override {} |
| 61 |
| 62 void DidChange(const device::BatteryStatus& battery_status) { |
| 63 device::BatteryStatusPtr status(device::BatteryStatus::New()); |
| 64 *status = battery_status; |
| 65 binding_.client()->DidChange(status.Pass()); |
| 66 } |
| 67 |
| 68 scoped_ptr<BatteryUpdateSubscription> subscription_; |
| 69 mojo::StrongBinding<BatteryMonitor> binding_; |
| 70 }; |
| 71 |
| 72 // Overrides the default service implementation with the test implementation |
| 73 // declared above. |
| 74 class TestContentBrowserClient : public ContentBrowserClient { |
| 75 public: |
| 76 void OverrideRenderProcessMojoServices(ServiceRegistry* registry) override { |
| 77 registry->AddService(base::Bind(&FakeBatteryMonitor::Create)); |
| 78 } |
| 79 }; |
| 80 |
| 81 class BatteryMonitorIntegrationTest : public ContentBrowserTest { |
| 82 public: |
| 83 BatteryMonitorIntegrationTest() {} |
| 84 |
| 85 void SetUpOnMainThread() override { |
| 86 old_client_ = SetBrowserClientForTesting(&test_client_); |
| 87 } |
| 88 |
| 89 void TearDownOnMainThread() override { |
| 90 SetBrowserClientForTesting(old_client_); |
| 91 } |
| 92 |
| 93 private: |
| 94 TestContentBrowserClient test_client_; |
| 95 ContentBrowserClient* old_client_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(BatteryMonitorIntegrationTest); |
| 98 }; |
| 99 |
| 100 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, DefaultValues) { |
| 101 // From JavaScript request a promise for the battery status information and |
| 102 // once it resolves check the default values and navigate to #pass. |
| 103 UpdateBattery(device::BatteryStatus()); |
| 104 GURL test_url = |
| 105 GetTestUrl("battery_status", "battery_status_default_test.html"); |
| 106 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 107 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 108 } |
| 109 |
| 110 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, ResolvePromise) { |
| 111 // Set the fake battery monitor to return predefined battery status values. |
| 112 // From JavaScript request a promise for the battery status information and |
| 113 // once it resolves check the values and navigate to #pass. |
| 114 device::BatteryStatus status; |
| 115 status.charging = true; |
| 116 status.charging_time = 100; |
| 117 status.discharging_time = std::numeric_limits<double>::infinity(); |
| 118 status.level = 0.5; |
| 119 UpdateBattery(status); |
| 120 |
| 121 GURL test_url = GetTestUrl("battery_status", |
| 122 "battery_status_promise_resolution_test.html"); |
| 123 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 124 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 125 } |
| 126 |
| 127 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, EventListener) { |
| 128 // Set the fake battery monitor to return default battery status values. |
| 129 // From JavaScript request a promise for the battery status information. |
| 130 // Once it resolves add an event listener for battery level change. Set |
| 131 // battery level to 0.6 and invoke update. Check that the event listener |
| 132 // is invoked with the correct value for level and navigate to #pass. |
| 133 device::BatteryStatus status; |
| 134 UpdateBattery(status); |
| 135 |
| 136 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); |
| 137 GURL test_url = |
| 138 GetTestUrl("battery_status", "battery_status_event_listener_test.html"); |
| 139 shell()->LoadURL(test_url); |
| 140 same_tab_observer.Wait(); |
| 141 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 142 |
| 143 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); |
| 144 status.level = 0.6; |
| 145 UpdateBattery(status); |
| 146 same_tab_observer2.Wait(); |
| 147 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 148 } |
| 149 |
| 150 } // namespace |
| 151 |
| 152 } // namespace content |
OLD | NEW |