<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
var scope = "/workers/service/serviceworkerclients-claim.https.html";
var activeWorker;

promise_test(async (test) => {
    var registration = await navigator.serviceWorker.getRegistration(scope);
    if (registration && registration.scope === scope)
        await registration.unregister();

    var registration = await navigator.serviceWorker.register("serviceworkerclients-matchAll-worker.js", { scope : scope });
    activeWorker = registration.active;
    if (activeWorker)
        return;
    activeWorker = registration.installing;
    return new Promise(resolve => {
        activeWorker.addEventListener('statechange', () => {
            if (activeWorker.state === "activated")
                resolve();
        });
    });
}, "Setup worker");

promise_test(async (test) => {
    var promise = new Promise((resolve, reject) => {
        navigator.serviceWorker.addEventListener("message", test.step_func((event) => {
            assert_equals(event.data, "PASS");
            resolve();
        }));
    });

    activeWorker.postMessage({test: "checkNewClientObject"});
    await promise;
}, "Test self.clients.matchAll create new client object every time");

promise_test(async (test) => {
    const serviceWorkerClientIdentifier = internals.serviceWorkerClientInternalIdentifier(document);
    var promise = new Promise((resolve, reject) => {
        navigator.serviceWorker.addEventListener("message", test.step_func((event) => {
            assert_equals(event.data, "PASS");
            resolve();
        }));
    });

    activeWorker.postMessage({test: "checkClientIsUncontrolled", identifier: serviceWorkerClientIdentifier });
    await promise;
}, "Test self.clients.matchAll");
</script>
</body>
</html>
