<!DOCTYPE html>
<html>
<script src="../../../resources/js-test.js"></script>
<head>

<style>
div#styleShouldBeHiddenByImport {
  color: red;
}
</style>
<link rel="import" href="resources/style-tree-order-hide-style.html">

<link rel="stylesheet" href="resources/style-tree-order-hide-link.css">
<link rel="import" href="resources/style-tree-order-hide-link.html">

<link rel="import" href="resources/style-tree-order-hide-import-red.html">
<link rel="import" href="resources/style-tree-order-hide-import-green.html">

<link rel="import" href="resources/style-tree-order-hide-subimport.html">


<link rel="import" href="resources/style-tree-order-hide-shared.html">
<style>
div#sharedImportShouldBeIgnored {
  color: green;
}
</style>
<link rel="import" href="resources/style-tree-order-hide-shared.html">

<style id="anchorStyle">
div#styleShouldBeHiddenByDynamicallyAddedFollower {
  color: red;
}

div#styleShouldNotBeHiddenByDynamicallyAddedLeader {
  color: green;
}
</style>

</head>
<body>
<div id="container">
<div id="styleShouldBeHiddenByImport">Hello</div>
<div id="linkShouldBeHiddenByImport">Hello</div>
<div id="importShouldBeHiddenByImport">Hello</div>
<div id="subimportShouldBeHiddenByImport">Hello</div>
<div id="sharedImportShouldBeIgnored">Hello</div>
<div id="styleShouldBeHiddenByDynamicallyAddedFollower">Hello</div>
<div id="styleShouldNotBeHiddenByDynamicallyAddedLeader">Hello</div>
</div>
<script>
window.jsTestIsAsync = true;
shouldBeEqualToString('window.getComputedStyle(styleShouldBeHiddenByImport).color', 'rgb(0, 128, 0)');
shouldBeEqualToString('window.getComputedStyle(linkShouldBeHiddenByImport).color', 'rgb(0, 128, 0)');
shouldBeEqualToString('window.getComputedStyle(importShouldBeHiddenByImport).color', 'rgb(0, 128, 0)');
shouldBeEqualToString('window.getComputedStyle(subimportShouldBeHiddenByImport).color', 'rgb(0, 128, 0)');
shouldBeEqualToString('window.getComputedStyle(sharedImportShouldBeIgnored).color', 'rgb(0, 128, 0)');

function createImportLink(url, onload)
{
    var link = document.createElement('link');
    link.setAttribute('rel', 'import');
    link.setAttribute('href', url);
    link.onload = onload;
    return link;
}

var linkCount = 0;
function testIfReady()
{
    linkCount++;
    if (linkCount < 2)
      return;

    shouldBeEqualToString('window.getComputedStyle(styleShouldBeHiddenByDynamicallyAddedFollower).color', 'rgb(0, 128, 0)');
    shouldBeEqualToString('window.getComputedStyle(styleShouldNotBeHiddenByDynamicallyAddedLeader).color', 'rgb(0, 128, 0)');

    container.style.display = 'none';
    finishJSTest();
}

// Dynamic addition.
document.head.insertBefore(createImportLink('resources/style-tree-order-dynamic-leading.html', testIfReady), anchorStyle);
document.head.appendChild(createImportLink('resources/style-tree-order-dynamic-following.html', testIfReady), anchorStyle);

</script>
</body>
</html>