OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/callback_list.h" | 7 #include "base/callback_list.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 g_callback_list.Get().Notify(battery_status); | 47 g_callback_list.Get().Notify(battery_status); |
48 } | 48 } |
49 | 49 |
50 class FakeBatteryMonitor : public device::BatteryMonitor { | 50 class FakeBatteryMonitor : public device::BatteryMonitor { |
51 public: | 51 public: |
52 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { | 52 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { |
53 new FakeBatteryMonitor(std::move(request)); | 53 new FakeBatteryMonitor(std::move(request)); |
54 } | 54 } |
55 | 55 |
56 private: | 56 private: |
| 57 typedef mojo::Callback<void(device::BatteryStatusPtr)> BatteryStatusCallback; |
| 58 |
57 FakeBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request) | 59 FakeBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request) |
58 : binding_(this, std::move(request)) {} | 60 : binding_(this, std::move(request)) {} |
59 ~FakeBatteryMonitor() override {} | 61 ~FakeBatteryMonitor() override {} |
60 | 62 |
61 void QueryNextStatus(const QueryNextStatusCallback& callback) override { | 63 void QueryNextStatus(const BatteryStatusCallback& callback) override { |
62 // We don't expect overlapped calls to QueryNextStatus. | 64 // We don't expect overlapped calls to QueryNextStatus. |
63 DCHECK(callback_.is_null()); | 65 DCHECK(callback_.is_null()); |
64 | 66 |
65 callback_ = callback; | 67 callback_ = callback; |
66 | 68 |
67 if (!subscription_) { | 69 if (!subscription_) { |
68 subscription_ = | 70 subscription_ = |
69 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange, | 71 g_callback_list.Get().Add(base::Bind(&FakeBatteryMonitor::DidChange, |
70 base::Unretained(this))); | 72 base::Unretained(this))); |
71 // Report initial value. | 73 // Report initial value. |
72 DidChange(g_battery_status); | 74 DidChange(g_battery_status); |
73 } | 75 } |
74 } | 76 } |
75 | 77 |
76 void DidChange(const device::BatteryStatus& battery_status) { | 78 void DidChange(const device::BatteryStatus& battery_status) { |
77 if (!callback_.is_null()) { | 79 if (!callback_.is_null()) { |
78 callback_.Run(battery_status.Clone()); | 80 callback_.Run(battery_status.Clone()); |
79 callback_.Reset(); | 81 callback_.Reset(); |
80 } | 82 } |
81 } | 83 } |
82 | 84 |
83 std::unique_ptr<BatteryUpdateSubscription> subscription_; | 85 std::unique_ptr<BatteryUpdateSubscription> subscription_; |
84 mojo::StrongBinding<BatteryMonitor> binding_; | 86 mojo::StrongBinding<BatteryMonitor> binding_; |
85 QueryNextStatusCallback callback_; | 87 BatteryStatusCallback callback_; |
86 }; | 88 }; |
87 | 89 |
88 // Overrides the default service implementation with the test implementation | 90 // Overrides the default service implementation with the test implementation |
89 // declared above. | 91 // declared above. |
90 class TestContentBrowserClient : public ContentBrowserClient { | 92 class TestContentBrowserClient : public ContentBrowserClient { |
91 public: | 93 public: |
92 void RegisterRenderProcessMojoServices( | 94 void RegisterRenderProcessMojoServices( |
93 ServiceRegistry* registry, | 95 ServiceRegistry* registry, |
94 RenderProcessHost* render_process_host) override { | 96 RenderProcessHost* render_process_host) override { |
95 registry->AddService(base::Bind(&FakeBatteryMonitor::Create)); | 97 registry->AddService(base::Bind(&FakeBatteryMonitor::Create)); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); | 181 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); |
180 status.level = 0.6; | 182 status.level = 0.6; |
181 UpdateBattery(status); | 183 UpdateBattery(status); |
182 same_tab_observer2.Wait(); | 184 same_tab_observer2.Wait(); |
183 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 185 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
184 } | 186 } |
185 | 187 |
186 } // namespace | 188 } // namespace |
187 | 189 |
188 } // namespace content | 190 } // namespace content |
OLD | NEW |