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