<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1816151
-->
<head>
  <meta charset="utf-8">
  <title>Tests for Telemetry in GeckoView Actors</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="head.js" type="application/javascript"></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=1816151">Mozilla Bug 1816151 for Window.Print() Telemetry</a>
<script class="testbody" type="text/javascript">

  const printScript = SpecialPowers.loadChromeScript(_ => {
    /* eslint-env mozilla/chrome-script */
    addMessageListener("print",() => {
      const navigator = Services.wm.getMostRecentWindow("navigator:geckoview");
      const printActor = navigator.window.moduleManager.getActor("GeckoViewPrintDelegate");
      printActor.telemetryDotPrintRequested();
    });
    addMessageListener("completed",(message) => {
      const navigator = Services.wm.getMostRecentWindow("navigator:geckoview");
      const printActor = navigator.window.moduleManager.getActor("GeckoViewPrintDelegate");
      printActor.telemetryDotPrintPdfCompleted(message);
    });
  });

  add_task(async function test_windowDotPrintTelemetry() {
    const telemetryStart = await GleanTest.dotprint.requested.testGetValue() ?? 0;

    // Using the print actor directly because
    // if window.print() is requested Android exits the TestRunnerActivity and starts a PrintActivity,
    // which causes the test harness to stop unexpectedly
    await printScript.sendAsyncMessage("print");

    const requestPrintOnce = await GleanTest.dotprint.requested.testGetValue() ?? 0;
    is(requestPrintOnce - telemetryStart, 1, "GeckoView Dot Print Telemetry Incremented Once");

    await printScript.sendAsyncMessage("print");
    const requestPrintTwice = await GleanTest.dotprint.requested.testGetValue() ?? 0;
    is(requestPrintTwice - telemetryStart, 2, "GeckoView Dot Print Telemetry Incremented Twice");
  });

  add_task(async function test_windowDotPrintDialogOpenedTelemetry() {
    const success = {isPdfSuccessful: true}
    const telemetryStart = await GleanTest.dotprint.androidDialogRequested.testGetValue() ?? 0;
    await printScript.sendAsyncMessage("completed", success);
    const dialogSuccessOnce = await GleanTest.dotprint.androidDialogRequested.testGetValue() ?? 0;
    is(dialogSuccessOnce - telemetryStart, 1, "GeckoView Dot Print Telemetry for Android Dialog Incremented Once");
    await printScript.sendAsyncMessage("completed", success);
    const dialogSuccessTwice = await GleanTest.dotprint.androidDialogRequested.testGetValue() ?? 0;
    is(dialogSuccessTwice - telemetryStart, 2, "GeckoView Dot Print Telemetry for Android Dialog Incremented Twice");
  });

  add_task(async function test_windowDotPrintFailureTelemetry() {
    // UNKNOWN Failure
    const failureStart = await GleanTest.dotprint.failure.testGetValueLabel("unknown") ?? 0;
    await printScript.sendAsyncMessage("completed", {isPdfSuccessful: false});
    const printFailure = await GleanTest.dotprint.failure.testGetValueLabel("unknown") ?? 0;
    is(printFailure - failureStart, 1, "GeckoView Dot Print Telemetry Fail Without a Specified Reason");

    await printScript.sendAsyncMessage("completed", {});
    const printFailureEmpty = await GleanTest.dotprint.failure.testGetValueLabel("unknown") ?? 0;
    is(printFailureEmpty - failureStart, 2, "GeckoView Dot Print Telemetry Fail When Empty");

    await printScript.sendAsyncMessage("completed", {isPdfSuccessful: false, errorReason: -10});
    const printFailureUnk = await GleanTest.dotprint.failure.testGetValueLabel("unknown") ?? 0;
    is(printFailureUnk - failureStart, 3, "GeckoView Dot Print Telemetry Fail With An Unknown Code");

    // ERROR_PRINT_SETTINGS_SERVICE_NOT_AVAILABLE -1 Failure
    const failure1Start = await GleanTest.dotprint.failure.testGetValueLabel("no_settings_service") ?? 0;
    await printScript.sendAsyncMessage("completed", {isPdfSuccessful: false, errorReason: -1});
    const failure1End = await GleanTest.dotprint.failure.testGetValueLabel("no_settings_service") ?? 0;
    is(failure1End - failure1Start, 1, "GeckoView Dot Print Telemetry Fail With No Settings Service");

    // ERROR_UNABLE_TO_CREATE_PRINT_SETTINGS -2 Failure
    const failure2Start = await GleanTest.dotprint.failure.testGetValueLabel("no_settings") ?? 0;
    await printScript.sendAsyncMessage("completed", {isPdfSuccessful: false, errorReason: -2});
    const failure2End = await GleanTest.dotprint.failure.testGetValueLabel("no_settings") ?? 0;
    is(failure2End - failure2Start, 1, "GeckoView Dot Print Telemetry Fail With No Settings");

    // ERROR_UNABLE_TO_RETRIEVE_CANONICAL_BROWSING_CONTEXT -3 Failure
    const failure3Start = await GleanTest.dotprint.failure.testGetValueLabel("no_canonical_context") ?? 0;
    await printScript.sendAsyncMessage("completed", {isPdfSuccessful: false, errorReason: -3});
    const failure3End = await GleanTest.dotprint.failure.testGetValueLabel("no_canonical_context") ?? 0;
    is(failure3End - failure3Start, 1, "GeckoView Dot Print Telemetry Fail With No Canonical Context");

    // ERROR_NO_ACTIVITY_CONTEXT_DELEGATE -4 Failure
    const failure4Start = await GleanTest.dotprint.failure.testGetValueLabel("no_activity_context_delegate") ?? 0;
    await printScript.sendAsyncMessage("completed", {isPdfSuccessful: false, errorReason: -4});
    const failure4End = await GleanTest.dotprint.failure.testGetValueLabel("no_activity_context_delegate") ?? 0;
    is(failure4End - failure4Start, 1, "GeckoView Dot Print Telemetry Fail With No Activity Context Delegate");

    // ERROR_NO_ACTIVITY_CONTEXT -5 Failure
    const failure5Start = await GleanTest.dotprint.failure.testGetValueLabel("no_activity_context") ?? 0;
    await printScript.sendAsyncMessage("completed", {isPdfSuccessful: false, errorReason: -5});
    const failure5End = await GleanTest.dotprint.failure.testGetValueLabel("no_activity_context") ?? 0;
    is(failure5End - failure5Start, 1, "GeckoView Dot Print Telemetry Fail With No Activity Context");
  });

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