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