Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(397)

Side by Side Diff: content/browser/battery_status/battery_monitor_integration_browsertest.cc

Issue 734123002: Mojify the battery status browsertests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Tim's comments: keep existing end-to-end tests. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/callback_list.h"
6 #include "base/lazy_instance.h"
7 #include "base/synchronization/waitable_event.h"
timvolodine 2014/11/20 16:20:08 do we need this?
8 #include "base/thread_task_runner_handle.h"
9 #include "content/public/browser/content_browser_client.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/common/content_client.h"
12 #include "content/public/common/service_registry.h"
13 #include "content/public/test/content_browser_test.h"
14 #include "content/public/test/content_browser_test_utils.h"
15 #include "content/public/test/test_navigation_observer.h"
16 #include "content/public/test/test_utils.h"
17 #include "content/shell/browser/shell.h"
18 #include "content/shell/browser/shell_content_browser_client.h"
19 #include "device/battery/battery_monitor.mojom.h"
20 #include "device/battery/battery_status_manager.h"
21 #include "device/battery/battery_status_service.h"
timvolodine 2014/11/20 16:20:08 no need for this?
ppi 2014/11/21 09:48:06 Done.
22 #include "mojo/public/cpp/bindings/strong_binding.h"
23
24 // These tests run against a dummy implementation of the BatteryMonitor service.
25 // That is, they verify that the service implementation is correctly exposed to
26 // the renderer, whatever the implementation is.
27
28 namespace content {
29
30 namespace {
31
32 typedef base::CallbackList<void(const device::BatteryStatus&)>
33 BatteryUpdateCallbackList;
34 typedef BatteryUpdateCallbackList::Subscription BatteryUpdateSubscription;
35
36 // Global battery state used in the tests.
37 device::BatteryStatus g_battery_status;
38 // Global list of test battery monitors to notify when |g_battery_status|
39 // changes.
40 base::LazyInstance<BatteryUpdateCallbackList> g_callback_list =
41 LAZY_INSTANCE_INITIALIZER;
42
43 // Updates the global battery state and notifies existing test monitors.
44 void UpdateBattery(const device::BatteryStatus& battery_status) {
45 g_battery_status = battery_status;
46 g_callback_list.Get().Notify(battery_status);
47 }
48
49 class TestBatteryMonitor : public device::BatteryMonitor {
timvolodine 2014/11/20 16:20:08 nit: I think FakeBatteryMonitor is more common.
ppi 2014/11/21 09:48:06 Done.
50 public:
51 static void Create(mojo::InterfaceRequest<BatteryMonitor> request) {
52 new TestBatteryMonitor(request.Pass());
53 }
54
55 private:
56 TestBatteryMonitor(mojo::InterfaceRequest<BatteryMonitor> request)
57 : subscription_(
58 g_callback_list.Get().Add(base::Bind(&TestBatteryMonitor::DidChange,
59 base::Unretained(this)))),
60 binding_(this, request.Pass()) {
61 DidChange(g_battery_status);
62 }
63 ~TestBatteryMonitor() override {}
64
65 void DidChange(const device::BatteryStatus& battery_status) {
66 device::BatteryStatusPtr status(device::BatteryStatus::New());
67 *status = battery_status;
68 binding_.client()->DidChange(status.Pass());
69 }
70
71 scoped_ptr<BatteryUpdateSubscription> subscription_;
72 mojo::StrongBinding<BatteryMonitor> binding_;
73 };
74
75 // Overrides the default service implementation with the test implementation
76 // declared above.
77 class TestContentBrowserClient : public ContentBrowserClient {
78 public:
79 void OverridePerProcessMojoServices(ServiceRegistry* registry) override {
80 registry->AddService(base::Bind(&TestBatteryMonitor::Create));
81 }
82 };
83
84 class BatteryMonitorIntegrationTest : public ContentBrowserTest {
85 public:
86 BatteryMonitorIntegrationTest() {}
87
88 void SetUpOnMainThread() override {
89 old_client_ = SetBrowserClientForTesting(&test_client_);
90 }
91
92 void TearDownOnMainThread() override {
93 SetBrowserClientForTesting(old_client_);
94 }
95
96 private:
97 TestContentBrowserClient test_client_;
98 ContentBrowserClient* old_client_;
99
100 DISALLOW_COPY_AND_ASSIGN(BatteryMonitorIntegrationTest);
101 };
102
103 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest,
104 BatteryManagerDefaultValues) {
105 // From JavaScript request a promise for the battery status information and
106 // once it resolves check the default values and navigate to #pass.
107 UpdateBattery(device::BatteryStatus());
108 GURL test_url =
109 GetTestUrl("battery_status", "battery_status_default_test.html");
110 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
111 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
112 }
113
114 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest,
115 BatteryManagerResolvePromise) {
116 // Set the fake battery manager to return predefined battery status values.
timvolodine 2014/11/20 16:20:08 "battery manager" -> "battery monitor"?
ppi 2014/11/21 09:48:06 Done.
117 // From JavaScript request a promise for the battery status information and
118 // once it resolves check the values and navigate to #pass.
119 device::BatteryStatus status;
120 status.charging = true;
121 status.charging_time = 100;
122 status.discharging_time = std::numeric_limits<double>::infinity();
123 status.level = 0.5;
124 UpdateBattery(status);
125
126 GURL test_url = GetTestUrl("battery_status",
127 "battery_status_promise_resolution_test.html");
128 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
129 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
130 }
131
132 IN_PROC_BROWSER_TEST_F(BatteryMonitorIntegrationTest,
133 BatteryManagerWithEventListener) {
134 // Set the fake battery manager to return default battery status values.
timvolodine 2014/11/20 16:20:09 "battery manager" -> "battery monitor"?
ppi 2014/11/21 09:48:06 Done.
135 // From JavaScript request a promise for the battery status information.
136 // Once it resolves add an event listener for battery level change. Set
137 // battery level to 0.6 and invoke update. Check that the event listener
138 // is invoked with the correct value for level and navigate to #pass.
139 device::BatteryStatus status;
140 UpdateBattery(status);
141
142 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
143 GURL test_url =
144 GetTestUrl("battery_status", "battery_status_event_listener_test.html");
145 shell()->LoadURL(test_url);
146 same_tab_observer.Wait();
147 EXPECT_EQ("resolved", shell()->web_contents()->GetLastCommittedURL().ref());
148
149 TestNavigationObserver same_tab_observer2(shell()->web_contents(), 1);
150 status.level = 0.6;
151 UpdateBattery(status);
152 same_tab_observer2.Wait();
153 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
154 }
155
156 } // namespace
157
158 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698