Chromium Code Reviews| 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 "base/callback_list.h" | |
| 5 #include "base/command_line.h" | 6 #include "base/command_line.h" |
|
timvolodine
2014/11/19 14:11:29
is this include needed?
ppi
2014/11/19 18:38:49
Done.
| |
| 7 #include "base/lazy_instance.h" | |
| 6 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
| 7 #include "base/thread_task_runner_handle.h" | 9 #include "base/thread_task_runner_handle.h" |
| 10 #include "content/public/browser/content_browser_client.h" | |
| 8 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/common/content_client.h" | |
| 9 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
|
timvolodine
2014/11/19 14:11:29
and this?
ppi
2014/11/19 18:38:49
Done.
| |
| 14 #include "content/public/common/service_registry.h" | |
| 10 #include "content/public/test/content_browser_test.h" | 15 #include "content/public/test/content_browser_test.h" |
| 11 #include "content/public/test/content_browser_test_utils.h" | 16 #include "content/public/test/content_browser_test_utils.h" |
| 12 #include "content/public/test/test_navigation_observer.h" | 17 #include "content/public/test/test_navigation_observer.h" |
| 13 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" |
| 14 #include "content/shell/browser/shell.h" | 19 #include "content/shell/browser/shell.h" |
| 20 #include "content/shell/browser/shell_content_browser_client.h" | |
| 21 #include "device/battery/battery_monitor.mojom.h" | |
| 15 #include "device/battery/battery_status_manager.h" | 22 #include "device/battery/battery_status_manager.h" |
| 16 #include "device/battery/battery_status_service.h" | 23 #include "device/battery/battery_status_service.h" |
| 17 | 24 |
| 18 namespace content { | 25 namespace content { |
| 19 | 26 |
| 20 namespace { | 27 namespace { |
| 21 | 28 |
| 22 class FakeBatteryManager : public device::BatteryStatusManager { | 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 TestBatteryMonitor : public mojo::InterfaceImpl<device::BatteryMonitor> { | |
| 23 public: | 47 public: |
| 24 explicit FakeBatteryManager( | 48 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) { |
| 25 const device::BatteryStatusService::BatteryUpdateCallback& callback) | 49 BindToRequest(new TestBatteryMonitor(), &request); |
| 26 : callback_(callback), battery_status_available_(true), started_(false) {} | |
| 27 ~FakeBatteryManager() override {} | |
| 28 | |
| 29 // Methods from BatteryStatusManager. | |
| 30 bool StartListeningBatteryChange() override { | |
| 31 started_ = true; | |
| 32 if (battery_status_available_) | |
| 33 InvokeUpdateCallback(); | |
| 34 return battery_status_available_; | |
| 35 } | |
| 36 | |
| 37 void StopListeningBatteryChange() override {} | |
| 38 | |
| 39 void InvokeUpdateCallback() { | |
| 40 // Invoke asynchronously to mimic the OS-specific battery managers. | |
| 41 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 42 FROM_HERE, | |
| 43 base::Bind(callback_, status_)); | |
| 44 } | |
| 45 | |
| 46 void set_battery_status(const device::BatteryStatus& status) { | |
| 47 status_ = status; | |
| 48 } | |
| 49 | |
| 50 void set_battery_status_available(bool value) { | |
| 51 battery_status_available_ = value; | |
| 52 } | |
| 53 | |
| 54 bool started() { | |
| 55 return started_; | |
| 56 } | 50 } |
| 57 | 51 |
| 58 private: | 52 private: |
| 59 device::BatteryStatusService::BatteryUpdateCallback callback_; | 53 TestBatteryMonitor() {} |
| 60 bool battery_status_available_; | 54 ~TestBatteryMonitor() override {} |
| 61 bool started_; | |
| 62 device::BatteryStatus status_; | |
| 63 | 55 |
| 64 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager); | 56 // mojo::InterfaceImpl<..> methods: |
| 57 void OnConnectionEstablished() override { | |
| 58 DidChange(g_battery_status); | |
| 59 subscription_ = g_callback_list.Get().Add( | |
| 60 base::Bind(&TestBatteryMonitor::DidChange, base::Unretained(this))); | |
| 61 } | |
| 62 | |
| 63 void DidChange(const device::BatteryStatus& battery_status) { | |
| 64 device::BatteryStatusPtr status(device::BatteryStatus::New()); | |
| 65 *status = battery_status; | |
| 66 client()->DidChange(status.Pass()); | |
| 67 } | |
| 68 | |
| 69 scoped_ptr<BatteryUpdateSubscription> subscription_; | |
| 70 }; | |
| 71 | |
| 72 // Overrides the default battery monitor implementation with the test | |
| 73 // implementation declared above. | |
| 74 class TestContentBrowserClient : public ContentBrowserClient { | |
| 75 public: | |
| 76 void OverridePerProcessMojoServices(ServiceRegistry* registry) override { | |
| 77 registry->AddService(base::Bind(&TestBatteryMonitor::Create)); | |
| 78 } | |
| 65 }; | 79 }; |
| 66 | 80 |
| 67 class BatteryStatusBrowserTest : public ContentBrowserTest { | 81 class BatteryStatusBrowserTest : public ContentBrowserTest { |
| 68 public: | 82 public: |
| 69 BatteryStatusBrowserTest() | 83 BatteryStatusBrowserTest() {} |
| 70 : battery_manager_(NULL), | 84 |
| 71 battery_service_(NULL) { | 85 void SetUpOnMainThread() override { |
| 86 old_client_ = SetBrowserClientForTesting(&test_client_); | |
| 72 } | 87 } |
| 73 | 88 |
| 74 void SetUpCommandLine(CommandLine* command_line) override { | 89 void TearDownOnMainThread() override { |
| 75 command_line->AppendSwitch( | 90 SetBrowserClientForTesting(old_client_); |
| 76 switches::kEnableExperimentalWebPlatformFeatures); | |
| 77 } | |
| 78 | |
| 79 void SetUpOnMainThread() override { | |
| 80 battery_service_ = device::BatteryStatusService::GetInstance(); | |
| 81 | |
| 82 // We keep a raw pointer to the FakeBatteryManager, which we expect to | |
| 83 // remain valid for the lifetime of the BatteryStatusService. | |
| 84 scoped_ptr<FakeBatteryManager> battery_manager(new FakeBatteryManager( | |
| 85 battery_service_->GetUpdateCallbackForTesting())); | |
| 86 battery_manager_ = battery_manager.get(); | |
| 87 | |
| 88 battery_service_->SetBatteryManagerForTesting( | |
| 89 battery_manager.Pass()); | |
| 90 } | |
| 91 | |
| 92 void TearDown() override { | |
| 93 battery_service_->SetBatteryManagerForTesting( | |
| 94 scoped_ptr<device::BatteryStatusManager>()); | |
| 95 battery_manager_ = NULL; | |
| 96 } | |
| 97 | |
| 98 FakeBatteryManager* battery_manager() { | |
| 99 return battery_manager_; | |
| 100 } | 91 } |
| 101 | 92 |
| 102 private: | 93 private: |
| 103 FakeBatteryManager* battery_manager_; | 94 TestContentBrowserClient test_client_; |
| 104 device::BatteryStatusService* battery_service_; | 95 ContentBrowserClient* old_client_; |
| 105 | 96 |
| 106 DISALLOW_COPY_AND_ASSIGN(BatteryStatusBrowserTest); | 97 DISALLOW_COPY_AND_ASSIGN(BatteryStatusBrowserTest); |
| 107 }; | 98 }; |
| 108 | 99 |
| 109 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerDefaultValues) { | 100 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerDefaultValues) { |
| 110 // Set the fake battery manager to return false on start. From JavaScript | 101 // From JavaScript request a promise for the battery status information and |
| 111 // request a promise for the battery status information and once it resolves | 102 // once it resolves check the default values and navigate to #pass. |
| 112 // check the default values and navigate to #pass. | 103 UpdateBattery(device::BatteryStatus()); |
| 113 battery_manager()->set_battery_status_available(false); | |
| 114 GURL test_url = GetTestUrl( | 104 GURL test_url = GetTestUrl( |
| 115 "battery_status", "battery_status_default_test.html"); | 105 "battery_status", "battery_status_default_test.html"); |
| 116 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 106 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 117 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 107 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 118 EXPECT_TRUE(battery_manager()->started()); | |
| 119 } | 108 } |
| 120 | 109 |
| 121 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerResolvePromise) { | 110 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerResolvePromise) { |
| 122 // Set the fake battery manager to return predefined battery status values. | 111 // Set the fake battery manager to return predefined battery status values. |
| 123 // From JavaScript request a promise for the battery status information and | 112 // From JavaScript request a promise for the battery status information and |
| 124 // once it resolves check the values and navigate to #pass. | 113 // once it resolves check the values and navigate to #pass. |
| 125 device::BatteryStatus status; | 114 device::BatteryStatus status; |
| 126 status.charging = true; | 115 status.charging = true; |
| 127 status.charging_time = 100; | 116 status.charging_time = 100; |
| 128 status.discharging_time = std::numeric_limits<double>::infinity(); | 117 status.discharging_time = std::numeric_limits<double>::infinity(); |
| 129 status.level = 0.5; | 118 status.level = 0.5; |
| 130 battery_manager()->set_battery_status(status); | 119 UpdateBattery(status); |
| 131 | 120 |
| 132 GURL test_url = GetTestUrl( | 121 GURL test_url = GetTestUrl( |
| 133 "battery_status", "battery_status_promise_resolution_test.html"); | 122 "battery_status", "battery_status_promise_resolution_test.html"); |
| 134 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 123 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 135 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 124 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 136 EXPECT_TRUE(battery_manager()->started()); | |
| 137 } | 125 } |
| 138 | 126 |
| 139 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, | 127 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, |
| 140 BatteryManagerWithEventListener) { | 128 BatteryManagerWithEventListener) { |
| 141 // Set the fake battery manager to return default battery status values. | 129 // Set the fake battery manager to return default battery status values. |
| 142 // From JavaScript request a promise for the battery status information. | 130 // From JavaScript request a promise for the battery status information. |
| 143 // Once it resolves add an event listener for battery level change. Set | 131 // Once it resolves add an event listener for battery level change. Set |
| 144 // battery level to 0.6 and invoke update. Check that the event listener | 132 // battery level to 0.6 and invoke update. Check that the event listener |
| 145 // is invoked with the correct value for level and navigate to #pass. | 133 // is invoked with the correct value for level and navigate to #pass. |
| 146 device::BatteryStatus status; | 134 device::BatteryStatus status; |
| 147 battery_manager()->set_battery_status(status); | 135 UpdateBattery(status); |
| 148 | 136 |
| 149 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | 137 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); |
| 150 GURL test_url = GetTestUrl( | 138 GURL test_url = GetTestUrl( |
| 151 "battery_status", "battery_status_event_listener_test.html"); | 139 "battery_status", "battery_status_event_listener_test.html"); |
| 152 shell()->LoadURL(test_url); | 140 shell()->LoadURL(test_url); |
| 153 same_tab_observer.Wait(); | 141 same_tab_observer.Wait(); |
| 154 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); | 142 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 155 | 143 |
| 156 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); | 144 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); |
| 157 status.level = 0.6; | 145 status.level = 0.6; |
| 158 battery_manager()->set_battery_status(status); | 146 UpdateBattery(status); |
| 159 battery_manager()->InvokeUpdateCallback(); | |
| 160 same_tab_observer2.Wait(); | 147 same_tab_observer2.Wait(); |
| 161 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 148 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 162 EXPECT_TRUE(battery_manager()->started()); | |
| 163 } | 149 } |
| 164 | 150 |
| 165 } // namespace | 151 } // namespace |
| 166 | 152 |
| 167 } // namespace content | 153 } // namespace content |
| OLD | NEW |