Chromium Code Reviews| Index: content/browser/device_sensors/device_sensor_browsertest.cc |
| diff --git a/content/browser/device_sensors/device_sensor_browsertest.cc b/content/browser/device_sensors/device_sensor_browsertest.cc |
| index f2201c1f76dda30b5c220e258abbe4a6c7f11bad..7082f9f2761bdb476808533db35500fd0c8902f0 100644 |
| --- a/content/browser/device_sensors/device_sensor_browsertest.cc |
| +++ b/content/browser/device_sensors/device_sensor_browsertest.cc |
| @@ -28,22 +28,35 @@ namespace { |
| class FakeDataFetcher : public DataFetcherSharedMemory { |
| public: |
| - FakeDataFetcher() |
| - : started_orientation_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| - base::WaitableEvent::InitialState::NOT_SIGNALED), |
| - stopped_orientation_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| - base::WaitableEvent::InitialState::NOT_SIGNALED), |
| - started_motion_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| - base::WaitableEvent::InitialState::NOT_SIGNALED), |
| - stopped_motion_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| - base::WaitableEvent::InitialState::NOT_SIGNALED), |
| - started_light_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| - base::WaitableEvent::InitialState::NOT_SIGNALED), |
| - stopped_light_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| - base::WaitableEvent::InitialState::NOT_SIGNALED), |
| - sensor_data_available_(true) {} |
| + FakeDataFetcher() : sensor_data_available_(true) {} |
| ~FakeDataFetcher() override {} |
| + void SetMotionStartedCallback(base::Closure motion_started_callback) { |
| + motion_started_callback_ = motion_started_callback; |
| + } |
| + |
| + void SetMotionStoppedCallback(base::Closure motion_stopped_callback) { |
| + motion_stopped_callback_ = motion_stopped_callback; |
| + } |
| + |
| + void SetLightStartedCallback(base::Closure light_started_callback) { |
| + light_started_callback_ = light_started_callback; |
| + } |
| + |
| + void SetLightStoppedCallback(base::Closure light_stopped_callback) { |
| + light_stopped_callback_ = light_stopped_callback; |
| + } |
| + |
| + void SetOrientationStartedCallback( |
| + base::Closure orientation_started_callback) { |
| + orientation_started_callback_ = orientation_started_callback; |
| + } |
| + |
| + void SetOrientationStoppedCallback( |
| + base::Closure orientation_stopped_callback) { |
| + orientation_stopped_callback_ = orientation_stopped_callback; |
| + } |
| + |
| bool Start(ConsumerType consumer_type, void* buffer) override { |
| EXPECT_TRUE(buffer); |
| @@ -54,7 +67,8 @@ class FakeDataFetcher : public DataFetcherSharedMemory { |
| if (sensor_data_available_) |
| UpdateMotion(motion_buffer); |
| SetMotionBufferReady(motion_buffer); |
| - started_motion_.Signal(); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + motion_started_callback_); |
| } break; |
| case CONSUMER_TYPE_ORIENTATION: { |
| DeviceOrientationHardwareBuffer* orientation_buffer = |
| @@ -62,7 +76,8 @@ class FakeDataFetcher : public DataFetcherSharedMemory { |
| if (sensor_data_available_) |
| UpdateOrientation(orientation_buffer); |
| SetOrientationBufferReady(orientation_buffer); |
| - started_orientation_.Signal(); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + orientation_started_callback_); |
| } break; |
| case CONSUMER_TYPE_LIGHT: { |
| DeviceLightHardwareBuffer* light_buffer = |
| @@ -71,7 +86,8 @@ class FakeDataFetcher : public DataFetcherSharedMemory { |
| sensor_data_available_ |
| ? 100 |
| : std::numeric_limits<double>::infinity()); |
| - started_light_.Signal(); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + light_started_callback_); |
| } break; |
| default: |
| return false; |
| @@ -82,13 +98,16 @@ class FakeDataFetcher : public DataFetcherSharedMemory { |
| bool Stop(ConsumerType consumer_type) override { |
| switch (consumer_type) { |
| case CONSUMER_TYPE_MOTION: |
| - stopped_motion_.Signal(); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + motion_stopped_callback_); |
| break; |
| case CONSUMER_TYPE_ORIENTATION: |
| - stopped_orientation_.Signal(); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + orientation_stopped_callback_); |
| break; |
| case CONSUMER_TYPE_LIGHT: |
| - stopped_light_.Signal(); |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + light_stopped_callback_); |
| break; |
| default: |
| return false; |
| @@ -164,12 +183,13 @@ class FakeDataFetcher : public DataFetcherSharedMemory { |
| buffer->seqlock.WriteEnd(); |
| } |
| - base::WaitableEvent started_orientation_; |
| - base::WaitableEvent stopped_orientation_; |
| - base::WaitableEvent started_motion_; |
| - base::WaitableEvent stopped_motion_; |
| - base::WaitableEvent started_light_; |
| - base::WaitableEvent stopped_light_; |
| + // The below callbacks should be run on the UI thread. |
| + base::Closure motion_started_callback_; |
| + base::Closure orientation_started_callback_; |
| + base::Closure light_started_callback_; |
| + base::Closure motion_stopped_callback_; |
| + base::Closure orientation_stopped_callback_; |
| + base::Closure light_stopped_callback_; |
| bool sensor_data_available_; |
| private: |
| @@ -185,16 +205,26 @@ class DeviceSensorBrowserTest : public ContentBrowserTest { |
| base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
| void SetUpOnMainThread() override { |
| +#if defined(OS_ANDROID) |
| + // On Android, the DeviceSensorService lives on the UI thread. |
| + SetUpFetcher(); |
| +#else |
| + // On all other platforms, the DeviceSensorService lives on the IO thread. |
| BrowserThread::PostTask( |
| BrowserThread::IO, FROM_HERE, |
| base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, |
| base::Unretained(this))); |
| io_loop_finished_event_.Wait(); |
| +#endif |
| } |
| - void SetUpOnIOThread() { |
| + void SetUpFetcher() { |
| fetcher_ = new FakeDataFetcher(); |
| DeviceSensorService::GetInstance()->SetDataFetcherForTesting(fetcher_); |
| + } |
| + |
| + void SetUpOnIOThread() { |
| + SetUpFetcher(); |
| io_loop_finished_event_.Signal(); |
| } |
| @@ -229,6 +259,13 @@ class DeviceSensorBrowserTest : public ContentBrowserTest { |
| }; |
| IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { |
| + base::RunLoop orientation_started_runloop; |
| + fetcher_->SetOrientationStartedCallback( |
| + orientation_started_runloop.QuitClosure()); |
| + base::RunLoop orientation_stopped_runloop; |
| + fetcher_->SetOrientationStoppedCallback( |
| + orientation_stopped_runloop.QuitClosure()); |
| + |
| // The test page will register an event handler for orientation events, |
| // expects to get an event with fake values, then removes the event |
| // handler and navigates to #pass. |
| @@ -236,11 +273,16 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { |
| NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| - fetcher_->started_orientation_.Wait(); |
| - fetcher_->stopped_orientation_.Wait(); |
| + orientation_started_runloop.Run(); |
| + orientation_stopped_runloop.Run(); |
| } |
| IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, LightTest) { |
| + base::RunLoop light_started_runloop; |
| + fetcher_->SetLightStartedCallback(light_started_runloop.QuitClosure()); |
| + base::RunLoop light_stopped_runloop; |
| + fetcher_->SetLightStoppedCallback(light_stopped_runloop.QuitClosure()); |
| + |
| // The test page will register an event handler for light events, |
| // expects to get an event with fake values, then removes the event |
| // handler and navigates to #pass. |
| @@ -249,11 +291,16 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, LightTest) { |
| NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| - fetcher_->started_light_.Wait(); |
| - fetcher_->stopped_light_.Wait(); |
| + light_started_runloop.Run(); |
| + light_stopped_runloop.Run(); |
| } |
| IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { |
| + base::RunLoop motion_started_runloop; |
| + fetcher_->SetMotionStartedCallback(motion_started_runloop.QuitClosure()); |
| + base::RunLoop motion_stopped_runloop; |
| + fetcher_->SetMotionStoppedCallback(motion_stopped_runloop.QuitClosure()); |
| + |
| // The test page will register an event handler for motion events, |
| // expects to get an event with fake values, then removes the event |
| // handler and navigates to #pass. |
| @@ -261,11 +308,16 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { |
| NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| - fetcher_->started_motion_.Wait(); |
| - fetcher_->stopped_motion_.Wait(); |
| + motion_started_runloop.Run(); |
| + motion_stopped_runloop.Run(); |
| } |
| IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, LightOneOffInfintyTest) { |
| + base::RunLoop light_started_runloop; |
| + fetcher_->SetLightStartedCallback(light_started_runloop.QuitClosure()); |
| + base::RunLoop light_stopped_runloop; |
| + fetcher_->SetLightStoppedCallback(light_stopped_runloop.QuitClosure()); |
| + |
| // The test page registers an event handler for light events and expects |
| // to get an event with value equal to infinity, because no sensor data can |
| // be provided. |
| @@ -276,11 +328,18 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, LightOneOffInfintyTest) { |
| NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| - fetcher_->started_light_.Wait(); |
| - fetcher_->stopped_light_.Wait(); |
| + light_started_runloop.Run(); |
| + light_stopped_runloop.Run(); |
| } |
| IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { |
| + base::RunLoop orientation_started_runloop; |
| + fetcher_->SetOrientationStartedCallback( |
| + orientation_started_runloop.QuitClosure()); |
| + base::RunLoop orientation_stopped_runloop; |
| + fetcher_->SetOrientationStoppedCallback( |
| + orientation_stopped_runloop.QuitClosure()); |
| + |
| // The test page registers an event handler for orientation events and |
| // expects to get an event with null values, because no sensor data can be |
| // provided. |
| @@ -290,11 +349,16 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { |
| NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| - fetcher_->started_orientation_.Wait(); |
| - fetcher_->stopped_orientation_.Wait(); |
| + orientation_started_runloop.Run(); |
| + orientation_stopped_runloop.Run(); |
| } |
| IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { |
| + base::RunLoop motion_started_runloop; |
| + fetcher_->SetMotionStartedCallback(motion_started_runloop.QuitClosure()); |
| + base::RunLoop motion_stopped_runloop; |
| + fetcher_->SetMotionStoppedCallback(motion_stopped_runloop.QuitClosure()); |
| + |
| // The test page registers an event handler for motion events and |
| // expects to get an event with null values, because no sensor data can be |
| // provided. |
| @@ -303,12 +367,24 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { |
| NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
| EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| - fetcher_->started_motion_.Wait(); |
| - fetcher_->stopped_motion_.Wait(); |
| + motion_started_runloop.Run(); |
| + motion_stopped_runloop.Run(); |
| } |
| IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { |
| - // The test page registers an event handlers for motion/orientation events |
| + base::RunLoop motion_started_runloop; |
|
timvolodine
2016/10/31 15:51:44
would it be simpler to just declare run loops in t
blundell
2016/11/09 15:07:43
Good idea, done.
|
| + fetcher_->SetMotionStartedCallback(motion_started_runloop.QuitClosure()); |
| + base::RunLoop motion_stopped_runloop; |
| + fetcher_->SetMotionStoppedCallback(motion_stopped_runloop.QuitClosure()); |
| + base::RunLoop orientation_started_runloop; |
| + fetcher_->SetOrientationStartedCallback( |
| + orientation_started_runloop.QuitClosure()); |
| + base::RunLoop orientation_stopped_runloop; |
| + fetcher_->SetOrientationStoppedCallback( |
| + orientation_stopped_runloop.QuitClosure()); |
| + |
| + // The test page registers an event handlers for orientation/orientation |
| + // events |
| // and expects to get events with null values. The test raises a modal alert |
| // dialog with a delay to test that the one-off null-events still propagate |
| // to window after the alert is dismissed and the callbacks are invoked which |
| @@ -324,10 +400,10 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { |
| // delay, crbug.com/360044. |
| WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); |
| - fetcher_->started_motion_.Wait(); |
| - fetcher_->stopped_motion_.Wait(); |
| - fetcher_->started_orientation_.Wait(); |
| - fetcher_->stopped_orientation_.Wait(); |
| + motion_started_runloop.Run(); |
| + motion_stopped_runloop.Run(); |
| + orientation_started_runloop.Run(); |
| + orientation_stopped_runloop.Run(); |
| same_tab_observer.Wait(); |
| EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
| } |