<!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 logs an error in non-secure contexts.");
jsTestIsAsync = true;

// localhost is secure by default.
internals.markContextAsInsecure();

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

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

    lastConsoleMessage = null;
    debug("");
    debug("* Registering device motion listener");
    addEventListener("devicemotion", function() { });
    internals.postTask(() => {
        shouldBeEqualToString("lastConsoleMessage", "No device motion events will be fired, reason: Browsing context is not secure.");
        finishJSTest();
    });
}

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

    lastConsoleMessage = null;
    debug("* Registering device orientation listener");
    addEventListener("deviceorientation", function() { });
    internals.postTask(() => {
        shouldBeEqualToString("lastConsoleMessage", "No device orientation events will be fired, reason: Browsing context is not secure.");
        runDeviceMotionTest();
    });
}

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