Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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" | |
| 8 #include "content/public/browser/render_frame_host.h" | |
| 9 #include "content/public/browser/render_process_host.h" | |
| 10 #include "content/public/browser/web_contents.h" | |
| 11 #include "content/public/common/service_names.mojom.h" | |
| 12 #include "content/public/test/browser_test_utils.h" | |
| 13 #include "content/public/test/content_browser_test.h" | |
| 14 #include "content/public/test/content_browser_test_utils.h" | |
| 15 #include "content/public/test/power_monitor_test.mojom.h" | |
| 16 #include "content/shell/browser/shell.h" | |
| 17 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 18 #include "mojo/public/cpp/bindings/interface_ptr_set.h" | |
| 19 #include "services/device/public/interfaces/constants.mojom.h" | |
| 20 #include "services/device/public/interfaces/power_monitor.mojom.h" | |
| 21 #include "services/service_manager/public/cpp/service_context.h" | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 void VerifyPowerStateInChildProcess(mojom::PowerMonitorTest* power_monitor_test, | |
| 28 bool expected_state) { | |
| 29 base::RunLoop run_loop; | |
| 30 power_monitor_test->QueryNextState(base::Bind( | |
| 31 [](const base::Closure& quit, bool expected_state, | |
| 32 bool on_battery_power) { | |
| 33 EXPECT_EQ(expected_state, on_battery_power); | |
| 34 quit.Run(); | |
| 35 }, | |
| 36 run_loop.QuitClosure(), expected_state)); | |
| 37 run_loop.Run(); | |
| 38 } | |
| 39 | |
| 40 class MockPowerMonitorMessageBroadcaster : public device::mojom::PowerMonitor { | |
| 41 public: | |
| 42 MockPowerMonitorMessageBroadcaster() = default; | |
| 43 ~MockPowerMonitorMessageBroadcaster() override = default; | |
| 44 | |
| 45 void Bind(device::mojom::PowerMonitorRequest request) { | |
| 46 bindings_.AddBinding(this, std::move(request)); | |
| 47 } | |
| 48 | |
| 49 // device::mojom::PowerMonitor: | |
| 50 void AddClient( | |
| 51 device::mojom::PowerMonitorClientPtr power_monitor_client) override { | |
| 52 power_monitor_client->PowerStateChange(on_battery_power_); | |
| 53 clients_.AddPtr(std::move(power_monitor_client)); | |
| 54 } | |
| 55 | |
| 56 void OnPowerStateChange(bool on_battery_power) { | |
| 57 on_battery_power_ = on_battery_power; | |
| 58 clients_.ForAllPtrs( | |
| 59 [&on_battery_power](device::mojom::PowerMonitorClient* client) { | |
| 60 client->PowerStateChange(on_battery_power); | |
| 61 }); | |
| 62 } | |
| 63 | |
| 64 private: | |
| 65 bool on_battery_power_ = false; | |
| 66 | |
| 67 mojo::BindingSet<device::mojom::PowerMonitor> bindings_; | |
| 68 mojo::InterfacePtrSet<device::mojom::PowerMonitorClient> clients_; | |
| 69 | |
| 70 DISALLOW_COPY_AND_ASSIGN(MockPowerMonitorMessageBroadcaster); | |
| 71 }; | |
| 72 | |
| 73 class PowerMonitorTest : public ContentBrowserTest { | |
| 74 public: | |
| 75 PowerMonitorTest() = default; | |
| 76 | |
| 77 void SetUpOnMainThread() override { | |
| 78 // Because Device Service also runs in this process(browser process), we can | |
| 79 // set our binder to intercept requests for PowerMonitor interface to it. | |
| 80 service_manager::ServiceContext::SetGlobalBinderForTesting( | |
| 81 device::mojom::kServiceName, device::mojom::PowerMonitor::Name_, | |
| 82 base::Bind(&PowerMonitorTest::BindPowerMonitor, | |
| 83 base::Unretained(this))); | |
| 84 } | |
| 85 | |
| 86 void BindPowerMonitor(const service_manager::BindSourceInfo& source_info, | |
| 87 const std::string& interface_name, | |
| 88 mojo::ScopedMessagePipeHandle handle) { | |
| 89 if (source_info.identity.name() == mojom::kRendererServiceName) | |
| 90 ++request_count_from_renderer_; | |
| 91 | |
| 92 power_monitor_message_broadcaster_.Bind( | |
| 93 device::mojom::PowerMonitorRequest(std::move(handle))); | |
| 94 } | |
| 95 | |
| 96 protected: | |
| 97 int request_count_from_renderer() { return request_count_from_renderer_; } | |
| 98 | |
| 99 void SimulatePowerStateChange(bool on_battery_power) { | |
| 100 power_monitor_message_broadcaster_.OnPowerStateChange(on_battery_power); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 int request_count_from_renderer_ = 0; | |
| 105 | |
| 106 MockPowerMonitorMessageBroadcaster power_monitor_message_broadcaster_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest); | |
| 109 }; | |
| 110 | |
| 111 IN_PROC_BROWSER_TEST_F(PowerMonitorTest, RequestOnceFromOneRendererProcess) { | |
|
blundell
2017/05/16 13:28:42
John, would we able to set up a similar test for t
leonhsl(Using Gerrit)
2017/05/22 12:11:57
Friendly ping John, how could we start up GPU/util
jam
2017/06/06 22:20:28
Since the GPU process is always running, content/s
leonhsl(Using Gerrit)
2017/06/09 07:05:53
Thanks! I'd like to land this CL(only for renderer
| |
| 112 ASSERT_EQ(0, request_count_from_renderer()); | |
| 113 ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(".", "simple_page.html"))); | |
| 114 EXPECT_EQ(1, request_count_from_renderer()); | |
| 115 | |
| 116 mojom::PowerMonitorTestPtr power_monitor_renderer; | |
| 117 RenderProcessHost* rph = | |
| 118 shell()->web_contents()->GetMainFrame()->GetProcess(); | |
| 119 BindInterface(rph, &power_monitor_renderer); | |
| 120 | |
| 121 SimulatePowerStateChange(true); | |
| 122 // Verify renderer process on_battery_power changed to true. | |
| 123 VerifyPowerStateInChildProcess(power_monitor_renderer.get(), true); | |
| 124 | |
| 125 SimulatePowerStateChange(false); | |
| 126 // Verify renderer process on_battery_power changed to false. | |
| 127 VerifyPowerStateInChildProcess(power_monitor_renderer.get(), false); | |
| 128 } | |
| 129 | |
| 130 } // namespace | |
| 131 | |
| 132 } // namespace content | |
| OLD | NEW |