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 "base/command_line.h" |
| 6 #include "base/synchronization/waitable_event.h" |
| 7 #include "content/browser/battery_status/battery_status_manager.h" |
| 8 #include "content/browser/battery_status/battery_status_service.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/common/content_switches.h" |
| 11 #include "content/public/test/content_browser_test.h" |
| 12 #include "content/public/test/content_browser_test_utils.h" |
| 13 #include "content/public/test/test_navigation_observer.h" |
| 14 #include "content/public/test/test_utils.h" |
| 15 #include "content/shell/browser/shell.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class FakeBatteryManager : public BatteryStatusManager { |
| 22 public: |
| 23 explicit FakeBatteryManager( |
| 24 const BatteryStatusService::BatteryUpdateCallback& callback) |
| 25 : battery_status_available_(true), |
| 26 started_(false) { |
| 27 callback_ = callback; |
| 28 } |
| 29 virtual ~FakeBatteryManager() { } |
| 30 |
| 31 // Methods from BatteryStatusManager. |
| 32 virtual bool StartListeningBatteryChange() OVERRIDE { |
| 33 started_ = true; |
| 34 if (battery_status_available_) |
| 35 InvokeUpdateCallback(); |
| 36 return battery_status_available_; |
| 37 } |
| 38 |
| 39 virtual void StopListeningBatteryChange() OVERRIDE { } |
| 40 |
| 41 void InvokeUpdateCallback() { |
| 42 callback_.Run(status_); |
| 43 } |
| 44 |
| 45 void set_battery_status(const blink::WebBatteryStatus& status) { |
| 46 status_ = status; |
| 47 } |
| 48 |
| 49 void set_battery_status_available(bool value) { |
| 50 battery_status_available_ = value; |
| 51 } |
| 52 |
| 53 bool started() { |
| 54 return started_; |
| 55 } |
| 56 |
| 57 private: |
| 58 bool battery_status_available_; |
| 59 bool started_; |
| 60 blink::WebBatteryStatus status_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(FakeBatteryManager); |
| 63 }; |
| 64 |
| 65 class BatteryStatusBrowserTest : public ContentBrowserTest { |
| 66 public: |
| 67 BatteryStatusBrowserTest() |
| 68 : battery_manager_(0), |
| 69 battery_service_(0), |
| 70 io_loop_finished_event_(false, false) { |
| 71 } |
| 72 |
| 73 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 74 command_line->AppendSwitch( |
| 75 switches::kEnableExperimentalWebPlatformFeatures); |
| 76 } |
| 77 |
| 78 virtual void SetUpOnMainThread() OVERRIDE { |
| 79 BrowserThread::PostTask( |
| 80 BrowserThread::IO, FROM_HERE, |
| 81 base::Bind(&BatteryStatusBrowserTest::SetUpOnIOThread, this)); |
| 82 io_loop_finished_event_.Wait(); |
| 83 } |
| 84 |
| 85 void SetUpOnIOThread() { |
| 86 battery_service_ = BatteryStatusService::GetInstance(); |
| 87 battery_manager_ = new FakeBatteryManager( |
| 88 battery_service_->GetUpdateCallbackForTesting()); |
| 89 battery_service_->SetBatteryManagerForTesting(battery_manager_); |
| 90 io_loop_finished_event_.Signal(); |
| 91 } |
| 92 |
| 93 virtual void TearDown() OVERRIDE { |
| 94 battery_service_->SetBatteryManagerForTesting(0); |
| 95 } |
| 96 |
| 97 FakeBatteryManager* battery_manager() { |
| 98 return battery_manager_; |
| 99 } |
| 100 |
| 101 private: |
| 102 FakeBatteryManager* battery_manager_; |
| 103 BatteryStatusService* battery_service_; |
| 104 base::WaitableEvent io_loop_finished_event_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(BatteryStatusBrowserTest); |
| 107 }; |
| 108 |
| 109 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerDefaultValues) { |
| 110 // 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 |
| 112 // check the default values and navigate to #pass. |
| 113 battery_manager()->set_battery_status_available(false); |
| 114 GURL test_url = GetTestUrl( |
| 115 "battery_status", "battery_status_default_test.html"); |
| 116 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 117 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 118 EXPECT_TRUE(battery_manager()->started()); |
| 119 } |
| 120 |
| 121 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, BatteryManagerResolvePromise) { |
| 122 // Set the fake battery manager to return predefined battery status values. |
| 123 // From JavaScript request a promise for the battery status information and |
| 124 // once it resolves check the values and navigate to #pass. |
| 125 blink::WebBatteryStatus status; |
| 126 status.charging = true; |
| 127 status.chargingTime = 100; |
| 128 status.dischargingTime = std::numeric_limits<double>::infinity(); |
| 129 status.level = 0.5; |
| 130 battery_manager()->set_battery_status(status); |
| 131 |
| 132 GURL test_url = GetTestUrl( |
| 133 "battery_status", "battery_status_promise_resolution_test.html"); |
| 134 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 135 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 136 EXPECT_TRUE(battery_manager()->started()); |
| 137 } |
| 138 |
| 139 IN_PROC_BROWSER_TEST_F(BatteryStatusBrowserTest, |
| 140 BatteryManagerWithEventListener) { |
| 141 // Set the fake battery manager to return default battery status values. |
| 142 // From JavaScript request a promise for the battery status information. |
| 143 // 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 |
| 145 // is invoked with the correct value for level and navigate to #pass. |
| 146 blink::WebBatteryStatus status; |
| 147 battery_manager()->set_battery_status(status); |
| 148 |
| 149 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); |
| 150 GURL test_url = GetTestUrl( |
| 151 "battery_status", "battery_status_event_listener_test.html"); |
| 152 shell()->LoadURL(test_url); |
| 153 same_tab_observer.Wait(); |
| 154 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 155 |
| 156 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1); |
| 157 status.level = 0.6; |
| 158 battery_manager()->set_battery_status(status); |
| 159 battery_manager()->InvokeUpdateCallback(); |
| 160 same_tab_observer2.Wait(); |
| 161 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 162 EXPECT_TRUE(battery_manager()->started()); |
| 163 } |
| 164 |
| 165 } // namespace |
| 166 |
| 167 } // namespace content |
OLD | NEW |