| 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 <utility> | |
| 6 | |
| 7 #include "base/callback_list.h" | |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/threading/thread_task_runner_handle.h" | |
| 12 #include "build/build_config.h" | |
| 13 #include "content/public/browser/content_browser_client.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "content/public/common/content_client.h" | |
| 16 #include "content/public/test/content_browser_test.h" | |
| 17 #include "content/public/test/content_browser_test_utils.h" | |
| 18 #include "content/public/test/test_navigation_observer.h" | |
| 19 #include "content/public/test/test_utils.h" | |
| 20 #include "content/shell/browser/shell.h" | |
| 21 #include "content/shell/browser/shell_content_browser_client.h" | |
| 22 #include "device/battery/battery_monitor.mojom.h" | |
| 23 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 24 #include "services/service_manager/public/cpp/interface_registry.h" | |
| 25 | |
| 26 // These tests run against a dummy implementation of the BatteryMonitor service. | |
| 27 // That is, they verify that the service implementation is correctly exposed to | |
| 28 // the renderer, whatever the implementation is. | |
| 29 | |
| 30 namespace content { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 typedef base::CallbackList<void(const device::BatteryStatus&)> | |
| 35 BatteryUpdateCallbackList; | |
| 36 typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription; | |
| 37 | |
| 38 // Global battery state used in the tests. | |
| 39 device::BatteryStatus g_battery_status; | |
| 40 // Global list of test battery monitors to notify when |g_battery_status| | |
| 41 // changes. | |
| 42 base::LazyInstance<BatteryUpdateCallbackList> g_callback_list = | |
| 43 LAZY_INSTANCE_INITIALIZER; | |
| 44 | |
| 45 // Updates the global battery state and notifies existing test monitors. | |
| 46 void UpdateBattery(const device::BatteryStatus& battery_status) { | |
| 47 g_battery_status = battery_status; | |
| 48 g_callback_list.Get().Notify(battery_status); | |
| 49 } | |
| 50 | |
| 51 class FakeBatteryMonitor : public device::BatteryMonitor { | |
| 52 public: | |
| 53 FakeBatteryMonitor() {} | |
| 54 ~FakeBatteryMonitor() override {} | |
| 55 | |
| 56 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { | |
| 57 mojo::MakeStrongBinding(base::MakeUnique<FakeBatteryMonitor>(), | |
| 58 std::move(request)); | |
| 59 } | |
| 60 | |
| 61 private: | |
| 62 void QueryNextStatus(const QueryNextStatusCallback& callback) override { | |
| 63 // We don't expect overlapped calls to QueryNextStatus. | |
| 64 DCHECK(callback_.is_null()); | |
| 65 | |
| 66 callback_ = callback; | |
| 67 | |
| 68 if (!subscription_) { | |
| 69 subscription_ = | |
| 70 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange, | |
| 71 base::Unretained(this))); | |
| 72 // Report initial value. | |
| 73 DidChange(g_battery_status); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void DidChange(const device::BatteryStatus& battery_status) { | |
| 78 if (!callback_.is_null()) { | |
| 79 callback_.Run(battery_status.Clone()); | |
| 80 callback_.Reset(); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 std::unique_ptr<BatteryUpdateSubscription> subscription_; | |
| 85 QueryNextStatusCallback callback_; | |
| 86 }; | |
| 87 | |
| 88 // Overrides the default service implementation with the test implementation | |
| 89 // declared above. | |
| 90 class TestContentBrowserClient : public ContentBrowserClient { | |
| 91 public: | |
| 92 void ExposeInterfacesToRenderer( | |
| 93 service_manager::InterfaceRegistry* registry, | |
| 94 RenderProcessHost* render_process_host) override { | |
| 95 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner = | |
| 96 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI); | |
| 97 registry->AddInterface(base::Bind(&FakeBatteryMonitor::Create), | |
| 98 ui_task_runner); | |
| 99 } | |
| 100 | |
| 101 void AppendExtraCommandLineSwitches(base::CommandLine* command_line, | |
| 102 int child_process_id) override { | |
| 103 // Necessary for passing kIsolateSitesForTesting flag to the renderer. | |
| 104 ShellContentBrowserClient::Get()->AppendExtraCommandLineSwitches( | |
| 105 command_line, child_process_id); | |
| 106 } | |
| 107 | |
| 108 #if defined(OS_ANDROID) | |
| 109 void GetAdditionalMappedFilesForChildProcess( | |
| 110 const base::CommandLine& command_line, | |
| 111 int child_process_id, | |
| 112 FileDescriptorInfo* mappings) override { | |
| 113 ShellContentBrowserClient::Get()->GetAdditionalMappedFilesForChildProcess( | |
| 114 command_line, child_process_id, mappings); | |
| 115 } | |
| 116 #endif // defined(OS_ANDROID) | |
| 117 }; | |
| 118 | |
| 119 class BatteryMonitorIntegrationTest : public ContentBrowserTest { | |
| 120 public: | |
| 121 BatteryMonitorIntegrationTest() {} | |
| 122 | |
| 123 void SetUpOnMainThread() override { | |
| 124 old_client_ = SetBrowserClientForTesting(&test_client_); | |
| 125 } | |
| 126 | |
| 127 void TearDownOnMainThread() override { | |
| 128 SetBrowserClientForTesting(old_client_); | |
| 129 } | |
| 130 | |
| 131 private: | |
| 132 TestContentBrowserClient test_client_; | |
| 133 ContentBrowserClient* old_client_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(BatteryMonitorIntegrationTest); | |
| 136 }; | |
| 137 | |
| 138 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, DefaultValues) { | |
| 139 // From JavaScript request a promise for the battery status information and | |
| 140 // once it resolves check the default values and navigate to #pass. | |
| 141 UpdateBattery(device::BatteryStatus()); | |
| 142 GURL test_url = | |
| 143 GetTestUrl("battery_status", "battery_status_default_test.html"); | |
| 144 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
| 145 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 146 } | |
| 147 | |
| 148 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, ResolvePromise) { | |
| 149 // Set the fake battery monitor to return predefined battery status values. | |
| 150 // From JavaScript request a promise for the battery status information and | |
| 151 // once it resolves check the values and navigate to #pass. | |
| 152 device::BatteryStatus status; | |
| 153 status.charging = true; | |
| 154 status.charging_time = 100; | |
| 155 status.discharging_time = std::numeric_limits<double>::infinity(); | |
| 156 status.level = 0.5; | |
| 157 UpdateBattery(status); | |
| 158 | |
| 159 GURL test_url = GetTestUrl("battery_status", | |
| 160 "battery_status_promise_resolution_test.html"); | |
| 161 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
| 162 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 163 } | |
| 164 | |
| 165 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest, EventListener) { | |
| 166 // Set the fake battery monitor to return default battery status values. | |
| 167 // From JavaScript request a promise for the battery status information. | |
| 168 // Once it resolves add an event listener for battery level change. Set | |
| 169 // battery level to 0.6 and invoke update. Check that the event listener | |
| 170 // is invoked with the correct value for level and navigate to #pass. | |
| 171 device::BatteryStatus status; | |
| 172 UpdateBattery(status); | |
| 173 | |
| 174 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | |
| 175 GURL test_url = | |
| 176 GetTestUrl("battery_status", "battery_status_event_listener_test.html"); | |
| 177 shell()->LoadURL(test_url); | |
| 178 same_tab_observer.Wait(); | |
| 179 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 180 | |
| 181 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); | |
| 182 status.level = 0.6; | |
| 183 UpdateBattery(status); | |
| 184 same_tab_observer2.Wait(); | |
| 185 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 186 } | |
| 187 | |
| 188 } // namespace | |
| 189 | |
| 190 } // namespace content | |
| OLD | NEW |