chromium/third_party/blink/web_tests/fast/canvas-api/canvas-lose-restore-max-int-size.html

<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script>
var canvas = document.createElement('canvas')
var ctx = canvas.getContext('2d');

function verifyContextLost(shouldBeLost, message) {
    // Verify context loss experimentally as well as isContextLost()
    ctx.fillStyle = '#0f0';
    ctx.fillRect(0, 0, 1, 1);
    var contextLostTest = ctx.getImageData(0, 0, 1, 1).data[1] == 0;
    assert_equals(contextLostTest, shouldBeLost, message + ': Ability to read a write to context');
    assert_equals(ctx.isContextLost(), shouldBeLost, message + ': Context lost state');
}

test(() => {
    verifyContextLost(false, 'initial state');
}, 'Context should initially not be lost');

test(() => {
    // WebIDL defines width and height as int. 2147483647 is int max.
    var extremelyLargeNumber = 2147483647;
    canvas.width = extremelyLargeNumber;
    canvas.height = extremelyLargeNumber;
    verifyContextLost(true, 'extremely large height set');
    canvas.width = extremelyLargeNumber;
    verifyContextLost(true, 'extremely large width set');
    // Restore a sane dimension
    canvas.width = 100;
    canvas.height = 100;
    // Restoration is async.
    verifyContextLost(true, 'reasonable size set');
}, 'Context should be lost when size is set to an extremely large number and then restored when set to a reasonable number');

var lostEventHasFired = false;
promise_test(() => {
    return new Promise(resolve => {
        canvas.addEventListener('contextlost', () => {
            assert_false(lostEventHasFired, 'contextLost should only be fired once');
            lostEventHasFired = true;
            verifyContextLost(true);
            resolve();
        });
    });
}, 'contextlost event should fire after size is set to extremely large number');

promise_test(() => {
    return new Promise(resolve => {
        canvas.addEventListener('contextrestored', () => {
            assert_true(lostEventHasFired, 'Lost context event should fire before resorted context event');
            verifyContextLost(false);
            resolve();
        });
    });
}, 'contextrestored event should fire after size is restored to a reasonable number');
</script>