<!DOCTYPE html>
<html>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script>
description("Tests that trying to set an event listener for deviceorientation and deviceorientation does not log an error in secure contexts.");
jsTestIsAsync = true;

if (window.testRunner) {
    if (testRunner.setShouldAllowDeviceOrientationAndMotionAccess)
        testRunner.setShouldAllowDeviceOrientationAndMotionAccess(true);
}

// localhost is secure by default.

let lastConsoleMessage = null;
internals.setConsoleMessageListener((message) => {
    lastConsoleMessage = message;
});

function runDeviceMotionTest()
{
    debug("");
    if (!window.DeviceMotionEvent || !DeviceMotionEvent.requestPermission) {
        console.log("Device Motion API is not supported");
        finishJSTest();
        return;
    }

    debug("* Requesting device motion access...");
    internals.withUserGesture(() => {
        DeviceMotionEvent.requestPermission().then((_result) => {
            result = _result;
            shouldBeEqualToString("result", "granted");

            lastConsoleMessage = null;
            debug("* Registering device motion listener");
            addEventListener("devicemotion", function() { });
            internals.postTask(() => {
                shouldBeNull("lastConsoleMessage");
                finishJSTest();
            });
        });
    });
}

function runDeviceOrientationTest()
{
    if (!window.DeviceOrientationEvent || !DeviceOrientationEvent.requestPermission) {
        console.log("Device Orientation API is not supported");
        runDeviceMotionTest();
        return;
    }

    debug("* Requesting device orientation access...");
    internals.withUserGesture(() => {
        DeviceOrientationEvent.requestPermission().then((_result) => {
            result = _result;
            shouldBeEqualToString("result", "granted");

            lastConsoleMessage = null;
            debug("* Registering device orientation listener");
            addEventListener("deviceorientation", function() { });
            internals.postTask(() => {
                shouldBeNull("lastConsoleMessage");
                runDeviceMotionTest();
            });
        });
    });
}

onload = runDeviceOrientationTest;
</script>
</body>
</html>
