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

Side by Side Diff: content/browser/generic_sensor_browsertest.cc

Issue 2434563005: [sensors] Add a browser test to sanity check ambient light sensor. (Closed)
Patch Set: Fix Win Clang Created 4 years, 2 months 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
« no previous file with comments | « no previous file | content/test/BUILD.gn » ('j') | device/generic_sensor/platform_sensor_provider.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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/macros.h"
7 #include "base/synchronization/waitable_event.h"
8 #include "base/threading/platform_thread.h"
9 #include "build/build_config.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/web_contents.h"
12 #include "content/public/common/content_switches.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_javascript_dialog_manager.h"
19 #include "device/generic_sensor/platform_sensor.h"
20 #include "device/generic_sensor/platform_sensor_provider.h"
21 #include "device/generic_sensor/sensor_provider_impl.h"
22
23 namespace content {
24
25 namespace {
26
27 class FakeAmbientLightSensor : public device::PlatformSensor {
28 public:
29 FakeAmbientLightSensor(device::mojom::SensorType type,
30 mojo::ScopedSharedBufferMapping mapping,
31 device::PlatformSensorProvider* provider)
32 : PlatformSensor(type, std::move(mapping), provider) {}
33
34 device::mojom::ReportingMode GetReportingMode() override {
35 return device::mojom::ReportingMode::ON_CHANGE;
36 }
37
38 bool StartSensor(
39 const device::PlatformSensorConfiguration& configuration) override {
40 device::SensorReading reading;
41 reading.timestamp =
42 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
Reilly Grant (use Gerrit) 2016/10/22 02:31:36 base::TimeTicks() is 0 so why not base::TimeTicks:
Mikhail 2016/10/24 06:41:55 TimeTicks does not contain 'InSecondsF' or similar
43 reading.values[0] = 50;
44 UpdateSensorReading(reading, true);
45 return true;
46 }
47
48 void StopSensor() override{};
49
50 protected:
51 ~FakeAmbientLightSensor() override = default;
52 bool CheckSensorConfiguration(
53 const device::PlatformSensorConfiguration& configuration) override {
54 return true;
55 }
56 device::PlatformSensorConfiguration GetDefaultConfiguration() override {
57 device::PlatformSensorConfiguration default_configuration;
58 default_configuration.set_frequency(60);
59 return default_configuration;
60 }
61 };
62
63 class FakeSensorProvider : public device::PlatformSensorProvider {
64 public:
65 FakeSensorProvider() = default;
66 ~FakeSensorProvider() override = default;
67
68 protected:
69 void CreateSensorInternal(device::mojom::SensorType type,
70 mojo::ScopedSharedBufferMapping mapping,
71 const CreateSensorCallback& callback) override {
72 // Create Sensors here.
73 switch (type) {
74 case device::mojom::SensorType::AMBIENT_LIGHT: {
75 scoped_refptr<device::PlatformSensor> sensor =
76 new FakeAmbientLightSensor(type, std::move(mapping), this);
77 callback.Run(std::move(sensor));
78 break;
79 }
80 default:
81 NOTIMPLEMENTED();
82 callback.Run(nullptr);
83 }
84 }
85 };
86
87 class GenericSensorBrowserTest : public ContentBrowserTest {
88 public:
89 GenericSensorBrowserTest()
90 : fake_sensor_provider_(nullptr),
Reilly Grant (use Gerrit) 2016/10/22 02:31:36 nit: This initialization is unnecessary.
91 io_loop_finished_event_(
92 base::WaitableEvent::ResetPolicy::AUTOMATIC,
93 base::WaitableEvent::InitialState::NOT_SIGNALED) {
94 // TODO(darktears): remove when the GenericSensor feature goes stable.
95 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
96 cmd_line->AppendSwitchASCII(switches::kEnableFeatures, "GenericSensor");
97 }
98
99 void SetUpOnMainThread() override {
100 BrowserThread::PostTask(
101 BrowserThread::IO, FROM_HERE,
102 base::Bind(&GenericSensorBrowserTest::SetUpOnIOThread,
103 base::Unretained(this)));
104 io_loop_finished_event_.Wait();
105 }
106
107 void SetUpOnIOThread() {
108 fake_sensor_provider_ = new FakeSensorProvider();
109 device::PlatformSensorProvider::SetProviderForTesting(
110 fake_sensor_provider_);
Reilly Grant (use Gerrit) 2016/10/22 02:31:37 Set the testing provider back to null when cleanin
111 io_loop_finished_event_.Signal();
112 }
113
114 public:
115 FakeSensorProvider* fake_sensor_provider_;
Mikhail 2016/10/24 06:41:55 unique_ptr ?
116 base::WaitableEvent io_loop_finished_event_;
117 };
118
119 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, AmbientLightSensorTest) {
120 // The test page will create an AmbientLightSensor object in Javascript,
121 // expects to get events with fake values then navigates to #pass.
122 GURL test_url =
123 GetTestUrl("generic_sensor", "ambient_light_sensor_test.html");
124 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
125 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
126 }
127
128 } // namespace
129
130 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/test/BUILD.gn » ('j') | device/generic_sensor/platform_sensor_provider.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698