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/memory/singleton.h" |
| 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "base/threading/platform_thread.h" |
| 10 #include "build/build_config.h" |
| 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/common/content_switches.h" |
| 14 #include "content/public/test/content_browser_test.h" |
| 15 #include "content/public/test/content_browser_test_utils.h" |
| 16 #include "content/public/test/test_navigation_observer.h" |
| 17 #include "content/public/test/test_utils.h" |
| 18 #include "content/shell/browser/shell.h" |
| 19 #include "content/shell/browser/shell_javascript_dialog_manager.h" |
| 20 #include "device/generic_sensor/platform_sensor.h" |
| 21 #include "device/generic_sensor/platform_sensor_provider.h" |
| 22 #include "device/generic_sensor/sensor_provider_impl.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 |
| 28 class FakeAmbientLightSensor : public device::PlatformSensor { |
| 29 public: |
| 30 FakeAmbientLightSensor(device::mojom::SensorType type, |
| 31 mojo::ScopedSharedBufferMapping mapping, |
| 32 device::PlatformSensorProvider* provider) |
| 33 : PlatformSensor(type, std::move(mapping), provider) {} |
| 34 |
| 35 device::mojom::ReportingMode GetReportingMode() override { |
| 36 return device::mojom::ReportingMode::ON_CHANGE; |
| 37 } |
| 38 |
| 39 bool StartSensor( |
| 40 const device::PlatformSensorConfiguration& configuration) override { |
| 41 device::SensorReading reading; |
| 42 reading.timestamp = |
| 43 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 44 reading.values[0] = 50; |
| 45 UpdateSensorReading(reading, true); |
| 46 return true; |
| 47 } |
| 48 |
| 49 void StopSensor() override{}; |
| 50 |
| 51 protected: |
| 52 ~FakeAmbientLightSensor() override = default; |
| 53 bool CheckSensorConfiguration( |
| 54 const device::PlatformSensorConfiguration& configuration) override { |
| 55 return true; |
| 56 } |
| 57 device::PlatformSensorConfiguration GetDefaultConfiguration() override { |
| 58 device::PlatformSensorConfiguration default_configuration; |
| 59 default_configuration.set_frequency(60); |
| 60 return default_configuration; |
| 61 } |
| 62 }; |
| 63 |
| 64 class FakeSensorProvider : public device::PlatformSensorProvider { |
| 65 public: |
| 66 static FakeSensorProvider* GetInstance() { |
| 67 return base::Singleton<FakeSensorProvider, base::LeakySingletonTraits< |
| 68 FakeSensorProvider>>::get(); |
| 69 } |
| 70 FakeSensorProvider() = default; |
| 71 ~FakeSensorProvider() override = default; |
| 72 |
| 73 protected: |
| 74 void CreateSensorInternal(device::mojom::SensorType type, |
| 75 mojo::ScopedSharedBufferMapping mapping, |
| 76 const CreateSensorCallback& callback) override { |
| 77 // Create Sensors here. |
| 78 switch (type) { |
| 79 case device::mojom::SensorType::AMBIENT_LIGHT: { |
| 80 scoped_refptr<device::PlatformSensor> sensor = |
| 81 new FakeAmbientLightSensor(type, std::move(mapping), this); |
| 82 callback.Run(std::move(sensor)); |
| 83 break; |
| 84 } |
| 85 default: |
| 86 NOTIMPLEMENTED(); |
| 87 callback.Run(nullptr); |
| 88 } |
| 89 } |
| 90 }; |
| 91 |
| 92 class GenericSensorBrowserTest : public ContentBrowserTest { |
| 93 public: |
| 94 GenericSensorBrowserTest() |
| 95 : io_loop_finished_event_( |
| 96 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 97 base::WaitableEvent::InitialState::NOT_SIGNALED) { |
| 98 // TODO(darktears): remove when the GenericSensor feature goes stable. |
| 99 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
| 100 cmd_line->AppendSwitchASCII(switches::kEnableFeatures, "GenericSensor"); |
| 101 } |
| 102 |
| 103 void SetUpOnMainThread() override { |
| 104 BrowserThread::PostTask( |
| 105 BrowserThread::IO, FROM_HERE, |
| 106 base::Bind(&GenericSensorBrowserTest::SetUpOnIOThread, |
| 107 base::Unretained(this))); |
| 108 io_loop_finished_event_.Wait(); |
| 109 } |
| 110 |
| 111 void SetUpOnIOThread() { |
| 112 device::PlatformSensorProvider::SetProviderForTesting( |
| 113 FakeSensorProvider::GetInstance()); |
| 114 io_loop_finished_event_.Signal(); |
| 115 } |
| 116 |
| 117 void TearDown() override { |
| 118 device::PlatformSensorProvider::SetProviderForTesting(nullptr); |
| 119 } |
| 120 |
| 121 public: |
| 122 base::WaitableEvent io_loop_finished_event_; |
| 123 }; |
| 124 |
| 125 IN_PROC_BROWSER_TEST_F(GenericSensorBrowserTest, AmbientLightSensorTest) { |
| 126 // The test page will create an AmbientLightSensor object in Javascript, |
| 127 // expects to get events with fake values then navigates to #pass. |
| 128 GURL test_url = |
| 129 GetTestUrl("generic_sensor", "ambient_light_sensor_test.html"); |
| 130 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| 131 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| 132 } |
| 133 |
| 134 } // namespace |
| 135 |
| 136 } // namespace content |
OLD | NEW |