| 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/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/test/content_browser_test.h" | 10 #include "content/public/test/content_browser_test.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 void StopListeningBatteryChange() override {} | 42 void StopListeningBatteryChange() override {} |
| 43 | 43 |
| 44 void InvokeUpdateCallback() { | 44 void InvokeUpdateCallback() { |
| 45 // Invoke asynchronously to mimic the OS-specific battery managers. | 45 // Invoke asynchronously to mimic the OS-specific battery managers. |
| 46 base::ThreadTaskRunnerHandle::Get()->PostTask( | 46 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 47 FROM_HERE, base::Bind(callback_, status_)); | 47 FROM_HERE, base::Bind(callback_, status_)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void set_battery_status(const device::BatteryStatus& status) { | 50 void set_battery_status(const device::mojom::BatteryStatus& status) { |
| 51 status_ = status; | 51 status_ = status; |
| 52 } | 52 } |
| 53 | 53 |
| 54 void set_battery_status_available(bool value) { | 54 void set_battery_status_available(bool value) { |
| 55 battery_status_available_ = value; | 55 battery_status_available_ = value; |
| 56 } | 56 } |
| 57 | 57 |
| 58 bool started() { return started_; } | 58 bool started() { return started_; } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 device::BatteryStatusService::BatteryUpdateCallback callback_; | 61 device::BatteryStatusService::BatteryUpdateCallback callback_; |
| 62 bool battery_status_available_; | 62 bool battery_status_available_; |
| 63 bool started_; | 63 bool started_; |
| 64 device::BatteryStatus status_; | 64 device::mojom::BatteryStatus status_; |
| 65 | 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager); | 66 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 class BatteryMonitorImplTest : public ContentBrowserTest { | 69 class BatteryMonitorImplTest : public ContentBrowserTest { |
| 70 public: | 70 public: |
| 71 BatteryMonitorImplTest() : battery_manager_(NULL), battery_service_(NULL) {} | 71 BatteryMonitorImplTest() : battery_manager_(NULL), battery_service_(NULL) {} |
| 72 | 72 |
| 73 void SetUpOnMainThread() override { | 73 void SetUpOnMainThread() override { |
| 74 battery_service_ = device::BatteryStatusService::GetInstance(); | 74 battery_service_ = device::BatteryStatusService::GetInstance(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 GetTestUrl("battery_status", "battery_status_default_test.html"); | 106 GetTestUrl("battery_status", "battery_status_default_test.html"); |
| 107 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 107 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 108 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 108 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 109 EXPECT_TRUE(battery_manager()->started()); | 109 EXPECT_TRUE(battery_manager()->started()); |
| 110 } | 110 } |
| 111 | 111 |
| 112 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, BatteryManagerResolvePromise) { | 112 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, BatteryManagerResolvePromise) { |
| 113 // Set the fake battery manager to return predefined battery status values. | 113 // Set the fake battery manager to return predefined battery status values. |
| 114 // From JavaScript request a promise for the battery status information and | 114 // From JavaScript request a promise for the battery status information and |
| 115 // once it resolves check the values and navigate to #pass. | 115 // once it resolves check the values and navigate to #pass. |
| 116 device::BatteryStatus status; | 116 device::mojom::BatteryStatus status; |
| 117 status.charging = true; | 117 status.charging = true; |
| 118 status.charging_time = 100; | 118 status.charging_time = 100; |
| 119 status.discharging_time = std::numeric_limits<double>::infinity(); | 119 status.discharging_time = std::numeric_limits<double>::infinity(); |
| 120 status.level = 0.5; | 120 status.level = 0.5; |
| 121 battery_manager()->set_battery_status(status); | 121 battery_manager()->set_battery_status(status); |
| 122 | 122 |
| 123 GURL test_url = GetTestUrl("battery_status", | 123 GURL test_url = GetTestUrl("battery_status", |
| 124 "battery_status_promise_resolution_test.html"); | 124 "battery_status_promise_resolution_test.html"); |
| 125 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 125 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 126 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 126 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 127 EXPECT_TRUE(battery_manager()->started()); | 127 EXPECT_TRUE(battery_manager()->started()); |
| 128 } | 128 } |
| 129 | 129 |
| 130 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, | 130 IN_PROC_BROWSER_TEST_F(BatteryMonitorImplTest, |
| 131 BatteryManagerWithEventListener) { | 131 BatteryManagerWithEventListener) { |
| 132 // Set the fake battery manager to return default battery status values. | 132 // Set the fake battery manager to return default battery status values. |
| 133 // From JavaScript request a promise for the battery status information. | 133 // From JavaScript request a promise for the battery status information. |
| 134 // Once it resolves add an event listener for battery level change. Set | 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 | 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. | 136 // is invoked with the correct value for level and navigate to #pass. |
| 137 device::BatteryStatus status; | 137 device::mojom::BatteryStatus status; |
| 138 battery_manager()->set_battery_status(status); | 138 battery_manager()->set_battery_status(status); |
| 139 | 139 |
| 140 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | 140 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); |
| 141 GURL test_url = | 141 GURL test_url = |
| 142 GetTestUrl("battery_status", "battery_status_event_listener_test.html"); | 142 GetTestUrl("battery_status", "battery_status_event_listener_test.html"); |
| 143 shell()->LoadURL(test_url); | 143 shell()->LoadURL(test_url); |
| 144 same_tab_observer.Wait(); | 144 same_tab_observer.Wait(); |
| 145 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); | 145 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 146 | 146 |
| 147 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); | 147 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); |
| 148 status.level = 0.6; | 148 status.level = 0.6; |
| 149 battery_manager()->set_battery_status(status); | 149 battery_manager()->set_battery_status(status); |
| 150 battery_manager()->InvokeUpdateCallback(); | 150 battery_manager()->InvokeUpdateCallback(); |
| 151 same_tab_observer2.Wait(); | 151 same_tab_observer2.Wait(); |
| 152 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 152 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 153 EXPECT_TRUE(battery_manager()->started()); | 153 EXPECT_TRUE(battery_manager()->started()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 } // namespace | 156 } // namespace |
| 157 | 157 |
| 158 } // namespace content | 158 } // namespace content |
| OLD | NEW |