Chromium Code Reviews| 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/macros.h" | |
| 8 #include "base/threading/thread_task_runner_handle.h" | |
| 9 #include "content/public/browser/web_contents.h" | |
| 10 #include "content/public/test/content_browser_test.h" | |
| 11 #include "content/public/test/content_browser_test_utils.h" | |
| 12 #include "content/public/test/test_navigation_observer.h" | |
| 13 #include "content/public/test/test_utils.h" | |
| 14 #include "content/shell/browser/shell.h" | |
| 15 #include "device/battery/battery_status_manager.h" | |
| 16 #include "device/battery/battery_status_service.h" | |
| 17 | |
| 18 // These tests run against the default implementation of the BatteryMonitor | |
| 19 // service, with a dummy BatteryManager set as a source of the battery | |
| 20 // information. They can be run only on platforms that use the default service | |
| 21 // implementation, ie. on the platforms where BatteryStatusService is used. | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class FakeBatteryManager : public device::BatteryStatusManager { | |
| 28 public: | |
| 29 explicit FakeBatteryManager( | |
| 30 const device::BatteryStatusService::BatteryUpdateCallback& callback) | |
| 31 : callback_(callback), battery_status_available_(true), started_(false) {} | |
| 32 ~FakeBatteryManager() override {} | |
| 33 | |
| 34 // Methods from BatteryStatusManager. | |
| 35 bool StartListeningBatteryChange() override { | |
| 36 started_ = true; | |
| 37 if (battery_status_available_) | |
| 38 InvokeUpdateCallback(); | |
| 39 return battery_status_available_; | |
| 40 } | |
| 41 | |
| 42 void StopListeningBatteryChange() override {} | |
| 43 | |
| 44 void InvokeUpdateCallback() { | |
| 45 // Invoke asynchronously to mimic the OS-specific battery managers. | |
| 46 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 47 FROM_HERE, base::Bind(callback_, status_)); | |
| 48 } | |
| 49 | |
| 50 void set_battery_status(const device::mojom::BatteryStatus& status) { | |
| 51 status_ = status; | |
| 52 } | |
| 53 | |
| 54 void set_battery_status_available(bool value) { | |
| 55 battery_status_available_ = value; | |
| 56 } | |
| 57 | |
| 58 bool started() { return started_; } | |
| 59 | |
| 60 private: | |
| 61 device::BatteryStatusService::BatteryUpdateCallback callback_; | |
| 62 bool battery_status_available_; | |
| 63 bool started_; | |
| 64 device::mojom::BatteryStatus status_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager); | |
| 67 }; | |
| 68 | |
| 69 class BatteryMonitorImplTest : public ContentBrowserTest { | |
| 70 public: | |
| 71 BatteryMonitorImplTest() : battery_manager_(NULL), battery_service_(NULL) {} | |
| 72 | |
| 73 void SetUpOnMainThread() override { | |
| 74 battery_service_ = device::BatteryStatusService::GetInstance(); | |
| 75 | |
| 76 // We keep a raw pointer to the FakeBatteryManager, which we expect to | |
| 77 // remain valid for the lifetime of the BatteryStatusService. | |
| 78 std::unique_ptr<FakeBatteryManager> battery_manager(new FakeBatteryManager( | |
| 79 battery_service_->GetUpdateCallbackForTesting())); | |
| 80 battery_manager_ = battery_manager.get(); | |
| 81 | |
| 82 battery_service_->SetBatteryManagerForTesting(std::move(battery_manager)); | |
| 83 } | |
| 84 | |
| 85 void TearDown() override { | |
| 86 battery_service_->SetBatteryManagerForTesting( | |
| 87 std::unique_ptr<device::BatteryStatusManager>()); | |
| 88 battery_manager_ = NULL; | |
| 89 } | |
| 90 | |
| 91 FakeBatteryManager* battery_manager() { return battery_manager_; } | |
| 92 | |
| 93 private: | |
| 94 FakeBatteryManager* battery_manager_; | |
| 95 device::BatteryStatusService* battery_service_; | |
| 96 | |
| 97 DISALLOW_COPY_AND_ASSIGN(BatteryMonitorImplTest); | |
| 98 }; | |
| 99 | |
| 100 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, BatteryManagerDefaultValues) { | |
| 101 // Set the fake battery manager to return false on start. From JavaScript | |
| 102 // request a promise for the battery status information and once it resolves | |
| 103 // check the default values and navigate to #pass. | |
| 104 battery_manager()->set_battery_status_available(false); | |
| 105 GURL test_url = | |
| 106 GetTestUrl("battery_status", "battery_status_default_test.html"); | |
|
blundell
2017/04/18 13:25:10
Can we remove all these html files as part of this
leonhsl(Using Gerrit)
2017/04/19 03:20:08
I forgot this.. Done and thanks!
| |
| 107 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
| 108 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 109 EXPECT_TRUE(battery_manager()->started()); | |
| 110 } | |
| 111 | |
| 112 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, BatteryManagerResolvePromise) { | |
| 113 // Set the fake battery manager to return predefined battery status values. | |
| 114 // From JavaScript request a promise for the battery status information and | |
| 115 // once it resolves check the values and navigate to #pass. | |
| 116 device::mojom::BatteryStatus status; | |
| 117 status.charging = true; | |
| 118 status.charging_time = 100; | |
| 119 status.discharging_time = std::numeric_limits<double>::infinity(); | |
| 120 status.level = 0.5; | |
| 121 battery_manager()->set_battery_status(status); | |
| 122 | |
| 123 GURL test_url = GetTestUrl("battery_status", | |
| 124 "battery_status_promise_resolution_test.html"); | |
| 125 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
| 126 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 127 EXPECT_TRUE(battery_manager()->started()); | |
| 128 } | |
| 129 | |
| 130 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, | |
| 131 BatteryManagerWithEventListener) { | |
| 132 // Set the fake battery manager to return default battery status values. | |
| 133 // From JavaScript request a promise for the battery status information. | |
| 134 // Once it resolves add an event listener for battery level change. Set | |
| 135 // battery level to 0.6 and invoke update. Check that the event listener | |
| 136 // is invoked with the correct value for level and navigate to #pass. | |
| 137 device::mojom::BatteryStatus status; | |
| 138 battery_manager()->set_battery_status(status); | |
| 139 | |
| 140 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | |
| 141 GURL test_url = | |
| 142 GetTestUrl("battery_status", "battery_status_event_listener_test.html"); | |
| 143 shell()->LoadURL(test_url); | |
| 144 same_tab_observer.Wait(); | |
| 145 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 146 | |
| 147 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); | |
| 148 status.level = 0.6; | |
| 149 battery_manager()->set_battery_status(status); | |
| 150 battery_manager()->InvokeUpdateCallback(); | |
| 151 same_tab_observer2.Wait(); | |
| 152 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
| 153 EXPECT_TRUE(battery_manager()->started()); | |
| 154 } | |
| 155 | |
| 156 } // namespace | |
| 157 | |
| 158 } // namespace content | |
| OLD | NEW |