leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 1 | // Copyright 2017 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/bind.h" |
| 6 | #include "base/macros.h" |
| 7 | #include "base/run_loop.h" |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 9 | #include "base/task/post_task.h" |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 10 | #include "content/browser/renderer_host/render_process_host_impl.h" |
Jay Civelli | 14aced2a | 2018-03-14 23:44:33 | [diff] [blame] | 11 | #include "content/browser/utility_process_host.h" |
| 12 | #include "content/browser/utility_process_host_client.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 13 | #include "content/public/browser/browser_task_traits.h" |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 15 | #include "content/public/browser/gpu_service_registry.h" |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 16 | #include "content/public/browser/render_frame_host.h" |
| 17 | #include "content/public/browser/render_process_host.h" |
| 18 | #include "content/public/browser/web_contents.h" |
| 19 | #include "content/public/common/service_names.mojom.h" |
| 20 | #include "content/public/test/browser_test_utils.h" |
| 21 | #include "content/public/test/content_browser_test.h" |
| 22 | #include "content/public/test/content_browser_test_utils.h" |
| 23 | #include "content/shell/browser/shell.h" |
| 24 | #include "content/shell/common/power_monitor_test.mojom.h" |
| 25 | #include "mojo/public/cpp/bindings/binding_set.h" |
| 26 | #include "mojo/public/cpp/bindings/interface_ptr_set.h" |
Ke He | 31d0bb0 | 2018-02-24 07:16:24 | [diff] [blame] | 27 | #include "services/device/public/mojom/constants.mojom.h" |
| 28 | #include "services/device/public/mojom/power_monitor.mojom.h" |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 29 | #include "services/service_manager/public/cpp/service_context.h" |
| 30 | |
| 31 | namespace content { |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | void VerifyPowerStateInChildProcess(mojom::PowerMonitorTest* power_monitor_test, |
| 36 | bool expected_state) { |
| 37 | base::RunLoop run_loop; |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame] | 38 | power_monitor_test->QueryNextState(base::BindOnce( |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 39 | [](const base::Closure& quit, bool expected_state, |
| 40 | bool on_battery_power) { |
| 41 | EXPECT_EQ(expected_state, on_battery_power); |
| 42 | quit.Run(); |
| 43 | }, |
| 44 | run_loop.QuitClosure(), expected_state)); |
| 45 | run_loop.Run(); |
| 46 | } |
| 47 | |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 48 | void StartUtilityProcessOnIOThread(mojom::PowerMonitorTestRequest request) { |
Jay Civelli | 14aced2a | 2018-03-14 23:44:33 | [diff] [blame] | 49 | UtilityProcessHost* host = |
| 50 | new UtilityProcessHost(/*client=*/nullptr, |
| 51 | /*client_task_runner=*/nullptr); |
Will Harris | edb011f | 2018-06-01 20:28:44 | [diff] [blame] | 52 | host->SetMetricsName("test_process"); |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 53 | host->SetName(base::ASCIIToUTF16("TestProcess")); |
| 54 | EXPECT_TRUE(host->Start()); |
| 55 | |
| 56 | BindInterface(host, std::move(request)); |
| 57 | } |
| 58 | |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 59 | void BindInterfaceForGpuOnIOThread(mojom::PowerMonitorTestRequest request) { |
| 60 | BindInterfaceInGpuProcess(std::move(request)); |
| 61 | } |
| 62 | |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 63 | class MockPowerMonitorMessageBroadcaster : public device::mojom::PowerMonitor { |
| 64 | public: |
| 65 | MockPowerMonitorMessageBroadcaster() = default; |
| 66 | ~MockPowerMonitorMessageBroadcaster() override = default; |
| 67 | |
| 68 | void Bind(device::mojom::PowerMonitorRequest request) { |
| 69 | bindings_.AddBinding(this, std::move(request)); |
| 70 | } |
| 71 | |
| 72 | // device::mojom::PowerMonitor: |
| 73 | void AddClient( |
| 74 | device::mojom::PowerMonitorClientPtr power_monitor_client) override { |
| 75 | power_monitor_client->PowerStateChange(on_battery_power_); |
| 76 | clients_.AddPtr(std::move(power_monitor_client)); |
| 77 | } |
| 78 | |
| 79 | void OnPowerStateChange(bool on_battery_power) { |
| 80 | on_battery_power_ = on_battery_power; |
| 81 | clients_.ForAllPtrs( |
| 82 | [&on_battery_power](device::mojom::PowerMonitorClient* client) { |
| 83 | client->PowerStateChange(on_battery_power); |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | bool on_battery_power_ = false; |
| 89 | |
| 90 | mojo::BindingSet<device::mojom::PowerMonitor> bindings_; |
| 91 | mojo::InterfacePtrSet<device::mojom::PowerMonitorClient> clients_; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(MockPowerMonitorMessageBroadcaster); |
| 94 | }; |
| 95 | |
| 96 | class PowerMonitorTest : public ContentBrowserTest { |
| 97 | public: |
Reilly Grant | 63316560 | 2018-07-11 00:48:03 | [diff] [blame] | 98 | PowerMonitorTest() { |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 99 | // Because Device Service also runs in this process(browser process), we can |
| 100 | // set our binder to intercept requests for PowerMonitor interface to it. |
| 101 | service_manager::ServiceContext::SetGlobalBinderForTesting( |
| 102 | device::mojom::kServiceName, device::mojom::PowerMonitor::Name_, |
| 103 | base::Bind(&PowerMonitorTest::BindPowerMonitor, |
| 104 | base::Unretained(this))); |
Reilly Grant | 63316560 | 2018-07-11 00:48:03 | [diff] [blame] | 105 | } |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 106 | |
Reilly Grant | 63316560 | 2018-07-11 00:48:03 | [diff] [blame] | 107 | ~PowerMonitorTest() override { |
| 108 | service_manager::ServiceContext::ClearGlobalBindersForTesting( |
| 109 | device::mojom::kServiceName); |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 110 | } |
| 111 | |
Ben Goodger | 21ada1e | 2017-07-19 14:53:01 | [diff] [blame] | 112 | void BindPowerMonitor(const std::string& interface_name, |
| 113 | mojo::ScopedMessagePipeHandle handle, |
| 114 | const service_manager::BindSourceInfo& source_info) { |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 115 | if (source_info.identity.name() == mojom::kRendererServiceName) { |
Lukasz Anforowicz | 2b269973 | 2018-04-12 18:49:10 | [diff] [blame] | 116 | // We can receive binding requests for the spare RenderProcessHost - this |
| 117 | // might happen before the test has provided the |
| 118 | // |renderer_bound_closure_|. |
| 119 | if (renderer_bound_closure_) { |
| 120 | ++request_count_from_renderer_; |
| 121 | std::move(renderer_bound_closure_).Run(); |
| 122 | } else { |
| 123 | DCHECK(RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); |
| 124 | } |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 125 | } else if (source_info.identity.name() == mojom::kUtilityServiceName) { |
John Abd-El-Malek | ef7a3da7 | 2018-04-25 13:40:08 | [diff] [blame] | 126 | // If the network service is enabled, it will create utility processes |
| 127 | // without a utility closure. |
| 128 | if (utility_bound_closure_) { |
| 129 | ++request_count_from_utility_; |
| 130 | std::move(utility_bound_closure_).Run(); |
| 131 | } |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 132 | } else if (source_info.identity.name() == mojom::kGpuServiceName) { |
| 133 | ++request_count_from_gpu_; |
| 134 | |
| 135 | // We ignore null gpu_bound_closure_ here for two possible scenarios: |
| 136 | // - TestRendererProcess and TestUtilityProcess also result in spinning |
| 137 | // up GPU processes as a side effect, but they do not set valid |
| 138 | // gpu_bound_closure_. |
| 139 | // - As GPU process is started during setup of browser test suite, so |
| 140 | // it's possible that TestGpuProcess execution may have not started |
| 141 | // yet when the PowerMonitor bind request comes here, in such case |
| 142 | // gpu_bound_closure_ will also be null. |
| 143 | if (gpu_bound_closure_) |
| 144 | std::move(gpu_bound_closure_).Run(); |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 145 | } |
| 146 | |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 147 | power_monitor_message_broadcaster_.Bind( |
| 148 | device::mojom::PowerMonitorRequest(std::move(handle))); |
| 149 | } |
| 150 | |
| 151 | protected: |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 152 | void StartUtilityProcess(mojom::PowerMonitorTestPtr* power_monitor_test, |
| 153 | base::Closure utility_bound_closure) { |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 154 | utility_bound_closure_ = std::move(utility_bound_closure); |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 155 | base::PostTaskWithTraits( |
| 156 | FROM_HERE, {BrowserThread::IO}, |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 157 | base::BindOnce(&StartUtilityProcessOnIOThread, |
| 158 | mojo::MakeRequest(power_monitor_test))); |
| 159 | } |
| 160 | |
| 161 | void set_renderer_bound_closure(base::Closure closure) { |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 162 | renderer_bound_closure_ = std::move(closure); |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 163 | } |
| 164 | |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 165 | void set_gpu_bound_closure(base::Closure closure) { |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 166 | gpu_bound_closure_ = std::move(closure); |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 167 | } |
| 168 | |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 169 | int request_count_from_renderer() { return request_count_from_renderer_; } |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 170 | int request_count_from_utility() { return request_count_from_utility_; } |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 171 | int request_count_from_gpu() { return request_count_from_gpu_; } |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 172 | |
| 173 | void SimulatePowerStateChange(bool on_battery_power) { |
| 174 | power_monitor_message_broadcaster_.OnPowerStateChange(on_battery_power); |
| 175 | } |
| 176 | |
| 177 | private: |
| 178 | int request_count_from_renderer_ = 0; |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 179 | int request_count_from_utility_ = 0; |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 180 | int request_count_from_gpu_ = 0; |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 181 | base::OnceClosure renderer_bound_closure_; |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 182 | base::OnceClosure gpu_bound_closure_; |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 183 | base::OnceClosure utility_bound_closure_; |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 184 | |
| 185 | MockPowerMonitorMessageBroadcaster power_monitor_message_broadcaster_; |
| 186 | |
| 187 | DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); |
| 188 | }; |
| 189 | |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 190 | IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestRendererProcess) { |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 191 | ASSERT_EQ(0, request_count_from_renderer()); |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 192 | base::RunLoop run_loop; |
| 193 | set_renderer_bound_closure(run_loop.QuitClosure()); |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 194 | ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 195 | run_loop.Run(); |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 196 | EXPECT_EQ(1, request_count_from_renderer()); |
| 197 | |
| 198 | mojom::PowerMonitorTestPtr power_monitor_renderer; |
| 199 | RenderProcessHost* rph = |
| 200 | shell()->web_contents()->GetMainFrame()->GetProcess(); |
| 201 | BindInterface(rph, &power_monitor_renderer); |
| 202 | |
| 203 | SimulatePowerStateChange(true); |
| 204 | // Verify renderer process on_battery_power changed to true. |
| 205 | VerifyPowerStateInChildProcess(power_monitor_renderer.get(), true); |
| 206 | |
| 207 | SimulatePowerStateChange(false); |
| 208 | // Verify renderer process on_battery_power changed to false. |
| 209 | VerifyPowerStateInChildProcess(power_monitor_renderer.get(), false); |
| 210 | } |
| 211 | |
Han Leon | 18ea620 | 2017-06-19 03:34:49 | [diff] [blame] | 212 | IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestUtilityProcess) { |
| 213 | mojom::PowerMonitorTestPtr power_monitor_utility; |
| 214 | |
| 215 | ASSERT_EQ(0, request_count_from_utility()); |
| 216 | base::RunLoop run_loop; |
| 217 | StartUtilityProcess(&power_monitor_utility, run_loop.QuitClosure()); |
| 218 | run_loop.Run(); |
| 219 | EXPECT_EQ(1, request_count_from_utility()); |
| 220 | |
| 221 | SimulatePowerStateChange(true); |
| 222 | // Verify utility process on_battery_power changed to true. |
| 223 | VerifyPowerStateInChildProcess(power_monitor_utility.get(), true); |
| 224 | |
| 225 | SimulatePowerStateChange(false); |
| 226 | // Verify utility process on_battery_power changed to false. |
| 227 | VerifyPowerStateInChildProcess(power_monitor_utility.get(), false); |
| 228 | } |
| 229 | |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 230 | IN_PROC_BROWSER_TEST_F(PowerMonitorTest, TestGpuProcess) { |
| 231 | // As gpu process is started automatically during the setup period of browser |
| 232 | // test suite, it may have already started and bound PowerMonitor interface to |
| 233 | // Device Service before execution of this TestGpuProcess test. So here we |
| 234 | // do not wait for the connection if we found it has already been established. |
| 235 | if (request_count_from_gpu() != 1) { |
| 236 | ASSERT_EQ(0, request_count_from_gpu()); |
| 237 | base::RunLoop run_loop; |
| 238 | set_gpu_bound_closure(run_loop.QuitClosure()); |
| 239 | // Wait for the connection from gpu process. |
| 240 | run_loop.Run(); |
| 241 | } |
| 242 | EXPECT_EQ(1, request_count_from_gpu()); |
| 243 | |
| 244 | mojom::PowerMonitorTestPtr power_monitor_gpu; |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 245 | base::PostTaskWithTraits( |
| 246 | FROM_HERE, {BrowserThread::IO}, |
Han Leon | 43528ec | 2017-07-07 02:54:57 | [diff] [blame] | 247 | base::BindOnce(&BindInterfaceForGpuOnIOThread, |
| 248 | mojo::MakeRequest(&power_monitor_gpu))); |
| 249 | |
| 250 | SimulatePowerStateChange(true); |
| 251 | // Verify gpu process on_battery_power changed to true. |
| 252 | VerifyPowerStateInChildProcess(power_monitor_gpu.get(), true); |
| 253 | |
| 254 | SimulatePowerStateChange(false); |
| 255 | // Verify gpu process on_battery_power changed to false. |
| 256 | VerifyPowerStateInChildProcess(power_monitor_gpu.get(), false); |
| 257 | } |
| 258 | |
leon.han | a0c5fa3 | 2017-06-13 08:14:11 | [diff] [blame] | 259 | } // namespace |
| 260 | |
| 261 | } // namespace content |