<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1132743
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1132743</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript;version=1.7" src="unprefixing_service_utils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1132743">Mozilla Bug 1132743</a>
<div id="display">
  <iframe id="testIframe"></iframe>
</div>
<pre id="test">
<script type="application/javascript;version=1.7">
"use strict";
SimpleTest.waitForExplicitFinish();

/**
 * This test checks that our CSS unprefixing prefs are effective.
 *
 * We do this using an iframe, in which we load a test file at a test domain
 * (whose whitelist-status depends on a pref), and we have the iframe report
 * back to us (using postMessage) about whether unprefixing is working.
 *
 * High-level overview of the process here (starting with begin()):
 *  - First, we ensure that the pref...
 *      "layout.css.unprefixing-service.include-test-domains"
 *    ...is *unset* by default. (No point exposing it in about:config).
 *  - Then, we test that (as a result of this pref being unset) the
 *    unprefixing service is *inactive* at our test-domain, by default.
 *  - Then, via a series of calls to "startNextTest()"/"testHost()", we re-test
 *    the same test-domain with a variety of pref configurations, to ensure
 *     that unprefixing only happens there when we've preffed on the service
 *     *and* we've enabled the testing entries in the whiteslist.
 */

const IFRAME_TESTFILE = "unprefixing_service_iframe.html";

// Just test the first host in our known-whitelisted-hosts list.
const WHITELISTED_TEST_HOST = gWhitelistedHosts[0];

// Configurations of our prefs to test.
// Each is a 3-entry array, whose entries mean:
// (1) should we enable the CSS Unprefixing Service pref?
// (2) should we enable the "include test domains in whitelist" pref?
// (3) in this pref-configuration, should we expect to see unprefixing active
//     on our whitelisted test-domain?
//
// As you can see, the only configuration which should produce unprefixing
// activity is when *both* prefs are enabled.
let gTestConfigs = [
 [false, false, false],
 [false, true,  false],
 [true,  false, false],
 [true,  true,  true],
];

// Test that a particular configuration of prefs will activate or inactivate
// the CSS unprefixing service, for styles loaded from WHITELISTED_TEST_HOST.
// aTestConfig is described above, in documentation for gTestConfigs.
function testConfig(aTestConfig)
{
  if (aTestConfig.length != 3) {
    ok(false, "bug in test; need 3 entries. see gTestConfigs documentation");
  }

  info("Verifying that CSS Unprefixing Service is " +
       (aTestConfig[2] ? "active" : "inactive") +
       " at test host, with prefs: " +
       PREF_UNPREFIXING_SERVICE + "=" + aTestConfig[0] + ", " +
       PREF_INCLUDE_TEST_DOMAINS + "=" + aTestConfig[1]);

  SpecialPowers.pushPrefEnv(
    { set:
        [[PREF_UNPREFIXING_SERVICE,  aTestConfig[0]],
         [PREF_INCLUDE_TEST_DOMAINS, aTestConfig[1]]]
    },
    function() {
      testHost(WHITELISTED_TEST_HOST, aTestConfig[2]);
    });
}

// This function gets invoked when our iframe finishes a given round of testing.
function startNextTest()
{
  if (gTestConfigs.length > 0) {
    // Grab the next test-config, and kick off a test for it.
    testConfig(gTestConfigs.pop());
    return;
  }

  // Array empty --> we're done.
  SimpleTest.finish();
}

function begin()
{
  // First, check that PREF_INCLUDE_TEST_DOMAINS is unset:
  try {
    let val = SpecialPowers.getBoolPref(PREF_INCLUDE_TEST_DOMAINS);
    ok(false, "The test pref '" + PREF_INCLUDE_TEST_DOMAINS +
               "' should be unspecified by default");
  } catch(e) { /* Good, we threw; pref is unset. */ }

  // Before we start loading things in iframes, set up postMessage handler.
  registerPostMessageListener(startNextTest);

  // To kick things off, we don't set any prefs; we just test the default state
  // (which should have the "include test domains" pref implicitly disabled, &
  // hence unprefixing should end up being disabled in our iframe). Subsequent
  // tests are kicked off via postMessage-triggered calls to startNextTest(),
  // which will tweak prefs and re-test.
  info("Verifying that CSS Unprefixing Service is inactive at test host, " +
       "with default pref configuration");
  testHost(WHITELISTED_TEST_HOST, false);
}

// Before we start, make sure *native* -webkit prefix support is turned off.
// It's not whitelist-restricted (and behaves slightly differently), so if we
// left it enabled, it'd prevent us from being able to detect
// CSSUnprefixingService's domain whitelisting in this test.
SpecialPowers.pushPrefEnv({ set: [["layout.css.prefixes.webkit", false]]},
                          begin);
</script>
</pre>
</body>
</html>