OLD | NEW |
---|---|
(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(); | |
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 : io_loop_finished_event_( | |
91 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
92 base::WaitableEvent::InitialState::NOT_SIGNALED) { | |
93 // TODO(darktears): remove when the GenericSensor feature goes stable. | |
94 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
95 cmd_line->AppendSwitchASCII(switches::kEnableFeatures, "GenericSensor"); | |
96 } | |
97 | |
98 void SetUpOnMainThread() override { | |
99 BrowserThread::PostTask( | |
100 BrowserThread::IO, FROM_HERE, | |
101 base::Bind(&GenericSensorBrowserTest::SetUpOnIOThread, | |
102 base::Unretained(this))); | |
103 io_loop_finished_event_.Wait(); | |
104 } | |
105 | |
106 void SetUpOnIOThread() { | |
107 fake_sensor_provider_ = new FakeSensorProvider(); | |
108 device::PlatformSensorProvider::SetProviderForTesting( | |
109 fake_sensor_provider_); | |
110 io_loop_finished_event_.Signal(); | |
111 } | |
112 | |
113 void TearDown() override { | |
114 device::PlatformSensorProvider::SetProviderForTesting(nullptr); | |
Mikhail
2016/10/26 17:02:12
^ this also should be done on IO thread.
Think it
| |
115 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, | |
116 fake_sensor_provider_); | |
117 } | |
118 | |
119 public: | |
120 FakeSensorProvider* fake_sensor_provider_; | |
121 base::WaitableEvent io_loop_finished_event_; | |
122 }; | |
123 | |
124 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, AmbientLightSensorTest) { | |
125 // The test page will create an AmbientLightSensor object in Javascript, | |
126 // expects to get events with fake values then navigates to #pass. | |
127 GURL test_url = | |
128 GetTestUrl("generic_sensor", "ambient_light_sensor_test.html"); | |
129 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
130 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
131 } | |
132 | |
133 } // namespace | |
134 | |
135 } // namespace content | |
OLD | NEW |