chromium/third_party/blink/web_tests/fast/canvas/canvas-save-restore.html

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

function hex(number)
{
    var hexDigits = "0123456789abcdef";
    return hexDigits[number >> 4] + hexDigits[number & 0xF];
}

function pixel()
{
    var imageData = context.getImageData(0, 0, 1, 1);
    return "#" + hex(imageData.data[0]) + hex(imageData.data[1]) + hex(imageData.data[2]);
}

var black="#000000";
var red = "#ff0000";
var green = "#008000";

test(function(t) {
    // (save set restore)
    context.fillStyle = "black";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.save();
    context.fillStyle = "red";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    // (save (save set restore) restore)
    context.fillStyle = "black";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.save();
    
    context.save();
    context.fillStyle = "red";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    // (save (save restore) set restore)
    context.fillStyle = "black";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.save();
    context.restore();
    
    context.save();
    context.fillStyle = "red";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    // (save (save (save set restore) set (save set restore) restore) restore)
    context.fillStyle = "black";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.save();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.save();
    context.fillStyle = "red";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.fillStyle = "green";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), green);
    
    context.save();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), green);
    
    context.fillStyle = "red";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), green);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    // (save (save set (save (save set restore) restore) set restore) restore)
    context.fillStyle = "black";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.save();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.save();
    context.fillStyle = "red";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.save();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.save();
    context.fillStyle = "green";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), green);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), red);
    
    context.fillStyle = "green";
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), green);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
    
    context.restore();
    context.fillRect(0, 0, 1, 1);
    assert_equals(pixel(), black);
}, "Verfiy canvas save and restore correct behavior.");
</script>
</body>
</html>