<!DOCTYPE html>
<html>
<body>
<script>
if (window.testRunner) {
  testRunner.dumpAsText();
  testRunner.waitUntilDone();

  // World id 0 is never saved, so this should print 'undefined'.
  testRunner.evaluateScriptInIsolatedWorld(0, "var foo = 'foo';");
  testRunner.evaluateScriptInIsolatedWorld(0,
    "document.body.appendChild(document.createTextNode('Expecting undefined: ' + window.foo));" +
    "document.body.appendChild(document.createElement('br'));");

  // Since these two run in the same world, this should print 'bar'.
  testRunner.evaluateScriptInIsolatedWorld(1, "var bar = 'bar';");
  testRunner.evaluateScriptInIsolatedWorld(1,
    "document.body.appendChild(document.createTextNode('Expecting bar: ' + window.bar));" +
    "document.body.appendChild(document.createElement('br'));");

  // Since these two are in different worlds, this should print 'undefined'.
  testRunner.evaluateScriptInIsolatedWorld(2, "var baz = 'baz';");
  testRunner.evaluateScriptInIsolatedWorld(3,
    "document.body.appendChild(document.createTextNode('Expecting undefined: ' + window.baz));" +
    "document.body.appendChild(document.createElement('br'));");

  // Worlds should not be shared across documents, so another frame that uses the same
  // world id should not see our variables.
  var iframe = document.createElement("iframe");
  document.body.insertBefore(iframe, document.body.firstChild);
  document.body.insertBefore(document.createElement("br"), iframe.nextSibling);
  var iframeComplete = function(result) {

    // Isolated world executing in frame should be able to to access parent content.
    testRunner.evaluateScriptInIsolatedWorld(1,
      "parent.document.body.appendChild(parent.document.createTextNode('Expecting true: ' + !!parent.frames[0].document.getElementById('output')));" +
      "parent.document.body.appendChild(parent.document.createElement('br'));");

    document.body.appendChild(document.createTextNode('Expecting undefined,undefined: ' + result));
    document.body.appendChild(document.createElement('br'));
    reloadFrame();
  }

  // Isolated world executing in window should be able to to access frame content.
  testRunner.evaluateScriptInIsolatedWorld(1,
    "document.body.appendChild(document.createTextNode('Expecting true: ' + !!frames[0].document));" +
    "document.body.appendChild(document.createElement('br'));");

  iframe.src = "resources/iframe.html";
  
  // Also, navigating a single frame should not result in sharing variables.
  function reloadFrame() {
    iframeComplete = function(result) {
      document.body.appendChild(document.createTextNode('Expecting undefined,undefined: ' + result));
      document.body.appendChild(document.createElement('br'));
      testRunner.notifyDone();
    }
    iframe.contentWindow.location.reload(true);
  }
}
</script>
</body>
</html>
