Commit 8cb0391a authored by timvolodine's avatar timvolodine Committed by Commit bot

Ensure complete end-to-end browsertest coverage for absolute device orientation.

Add end-to-end browser tests for the 'deviceorientationabsolute' event
to have full event coverage in device_sensors/.

BUG=702170

Review-Url: https://codereview.chromium.org/2744243006
Cr-Commit-Position: refs/heads/master@{#457449}
parent 1ab02803
......@@ -57,6 +57,18 @@ class FakeDataFetcher : public device::DataFetcherSharedMemory {
orientation_stopped_callback_ = orientation_stopped_callback;
}
void SetOrientationAbsoluteStartedCallback(
base::Closure orientation_absolute_started_callback) {
orientation_absolute_started_callback_ =
orientation_absolute_started_callback;
}
void SetOrientationAbsoluteStoppedCallback(
base::Closure orientation_absolute_stopped_callback) {
orientation_absolute_stopped_callback_ =
orientation_absolute_stopped_callback;
}
bool Start(device::ConsumerType consumer_type, void* buffer) override {
EXPECT_TRUE(buffer);
......@@ -79,6 +91,15 @@ class FakeDataFetcher : public device::DataFetcherSharedMemory {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
orientation_started_callback_);
} break;
case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: {
device::DeviceOrientationHardwareBuffer* orientation_buffer =
static_cast<device::DeviceOrientationHardwareBuffer*>(buffer);
if (sensor_data_available_)
UpdateOrientationAbsolute(orientation_buffer);
SetOrientationBufferReady(orientation_buffer);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
orientation_absolute_started_callback_);
} break;
case device::CONSUMER_TYPE_LIGHT: {
device::DeviceLightHardwareBuffer* light_buffer =
static_cast<device::DeviceLightHardwareBuffer*>(buffer);
......@@ -105,6 +126,10 @@ class FakeDataFetcher : public device::DataFetcherSharedMemory {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
orientation_stopped_callback_);
break;
case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE:
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
orientation_absolute_stopped_callback_);
break;
case device::CONSUMER_TYPE_LIGHT:
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
light_stopped_callback_);
......@@ -178,6 +203,20 @@ class FakeDataFetcher : public device::DataFetcherSharedMemory {
buffer->seqlock.WriteEnd();
}
void UpdateOrientationAbsolute(
device::DeviceOrientationHardwareBuffer* buffer) {
buffer->seqlock.WriteBegin();
buffer->data.alpha = 4;
buffer->data.hasAlpha = true;
buffer->data.beta = 5;
buffer->data.hasBeta = true;
buffer->data.gamma = 6;
buffer->data.hasGamma = true;
buffer->data.absolute = true;
buffer->data.allAvailableSensorsAreActive = true;
buffer->seqlock.WriteEnd();
}
void UpdateLight(device::DeviceLightHardwareBuffer* buffer, double lux) {
buffer->seqlock.WriteBegin();
buffer->data.value = lux;
......@@ -187,9 +226,11 @@ class FakeDataFetcher : public device::DataFetcherSharedMemory {
// The below callbacks should be run on the UI thread.
base::Closure motion_started_callback_;
base::Closure orientation_started_callback_;
base::Closure orientation_absolute_started_callback_;
base::Closure light_started_callback_;
base::Closure motion_stopped_callback_;
base::Closure orientation_stopped_callback_;
base::Closure orientation_absolute_stopped_callback_;
base::Closure light_stopped_callback_;
bool sensor_data_available_;
......@@ -213,6 +254,8 @@ class DeviceSensorBrowserTest : public ContentBrowserTest {
motion_stopped_runloop_.reset(new base::RunLoop());
orientation_started_runloop_.reset(new base::RunLoop());
orientation_stopped_runloop_.reset(new base::RunLoop());
orientation_absolute_started_runloop_.reset(new base::RunLoop());
orientation_absolute_stopped_runloop_.reset(new base::RunLoop());
#if defined(OS_ANDROID)
// On Android, the DeviceSensorService lives on the UI thread.
SetUpFetcher();
......@@ -236,6 +279,10 @@ class DeviceSensorBrowserTest : public ContentBrowserTest {
orientation_started_runloop_->QuitClosure());
fetcher_->SetOrientationStoppedCallback(
orientation_stopped_runloop_->QuitClosure());
fetcher_->SetOrientationAbsoluteStartedCallback(
orientation_absolute_started_runloop_->QuitClosure());
fetcher_->SetOrientationAbsoluteStoppedCallback(
orientation_absolute_stopped_runloop_->QuitClosure());
device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting(
fetcher_);
}
......@@ -279,6 +326,8 @@ class DeviceSensorBrowserTest : public ContentBrowserTest {
std::unique_ptr<base::RunLoop> motion_stopped_runloop_;
std::unique_ptr<base::RunLoop> orientation_started_runloop_;
std::unique_ptr<base::RunLoop> orientation_stopped_runloop_;
std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_;
std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_;
private:
base::WaitableEvent io_loop_finished_event_;
......@@ -296,6 +345,19 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) {
orientation_stopped_runloop_->Run();
}
IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationAbsoluteTest) {
// The test page will register an event handler for absolute orientation
// events, expects to get an event with fake values, then removes the event
// handler and navigates to #pass.
GURL test_url =
GetTestUrl("device_sensors", "device_orientation_absolute_test.html");
NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
orientation_absolute_started_runloop_->Run();
orientation_absolute_stopped_runloop_->Run();
}
IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, LightTest) {
// The test page will register an event handler for light events,
// expects to get an event with fake values, then removes the event
......@@ -350,6 +412,20 @@ IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) {
orientation_stopped_runloop_->Run();
}
IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationAbsoluteNullTest) {
// The test page registers an event handler for absolute orientation events
// and expects to get an event with null values, because no sensor data can be
// provided.
fetcher_->SetSensorDataAvailable(false);
GURL test_url = GetTestUrl("device_sensors",
"device_orientation_absolute_null_test.html");
NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
orientation_absolute_started_runloop_->Run();
orientation_absolute_stopped_runloop_->Run();
}
IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) {
// 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
......
<html>
<head>
<title>DeviceOrientationAbsolute all-null event test</title>
</head>
<body>
<div id="status">FAIL</div>
</body>
<script type="text/javascript">
function checkOrientationAbsoluteEvent(event) {
return event.alpha == null &&
event.beta == null &&
event.gamma == null;
}
function onOrientationAbsolute(event) {
window.removeEventListener('deviceorientationabsolute', onOrientationAbsolute);
checkOrientationAbsoluteEvent(event) ? pass() : fail();
}
function pass() {
document.getElementById('status').innerHTML = 'PASS';
document.location = '#pass';
}
function fail() {
document.location = '#fail';
}
window.addEventListener('deviceorientationabsolute', onOrientationAbsolute);
</script>
</html>
<html>
<head>
<title>DeviceOrientationAbsolute test</title>
<script type="text/javascript">
function checkOrientationAbsoluteEvent(event) {
// Return true iff the orientation is close enough to (4, 5, 6).
return Math.abs(event.alpha - 4) < 0.01 &&
Math.abs(event.beta - 5) < 0.01 &&
Math.abs(event.gamma - 6) < 0.01 &&
event.absolute == true;
}
function onOrientationAbsolute(event) {
if (checkOrientationAbsoluteEvent(event)) {
window.removeEventListener('deviceorientationabsolute', onOrientationAbsolute);
pass();
} else {
fail();
}
}
function pass() {
document.getElementById('status').innerHTML = 'PASS';
document.location = '#pass';
}
function fail() {
document.location = '#fail';
}
</script>
</head>
<body onLoad="window.addEventListener('deviceorientationabsolute', onOrientationAbsolute)">
<div id="status">FAIL</div>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment