File: test_model_optin.html

package info (click to toggle)
firefox 141.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,550,588 kB
  • sloc: cpp: 7,426,506; javascript: 6,367,238; ansic: 3,707,351; python: 1,369,002; xml: 623,983; asm: 426,918; java: 184,324; sh: 64,488; makefile: 19,203; objc: 13,059; perl: 12,955; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,071; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (126 lines) | stat: -rw-r--r-- 5,054 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8" />
  <title>ModelOptin Tests</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <script type="module" src="chrome://global/content/elements/moz-button.mjs"></script>
  <script type="module" src="chrome://global/content/elements/moz-button-group.mjs"></script>
  <script type="module" src="chrome://browser/content/genai/content/model-optin.mjs"></script>
  <link rel="stylesheet" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
  <link rel="stylesheet" href="chrome://global/skin/design-system/tokens-brand.css">
  <link rel="stylesheet" href="chrome://global/skin/design-system/text-and-typography.css">

  <script>
    // Utility: wait for Lit to finish rendering
    async function waitForUpdateComplete(element) {
      if (element && typeof element.updateComplete === "object") {
        await element.updateComplete;
      }
    }

    /**
     * Verify a ModelOptin's default state.
     */
    add_task(async function test_model_optin_default_state() {
      const mo = document.querySelector("#model-optin");
      ok(mo, "Found the ModelOptin element in the DOM.");

      await waitForUpdateComplete(mo);

      // Defaults from your constructor:
      is(mo.isLoading, false, "ModelOptin should not be loading by default.");
      is(mo.isHidden, false, "ModelOptin should be visible by default.");

      // If you want to confirm the <section> is not hidden in the shadow DOM:
      const section = mo.shadowRoot.querySelector("section.optin-wrapper");
      ok(section, "Found the main <section> in the shadow DOM.");
      ok(!section.hasAttribute("hidden"), "Section should not be hidden initially.");

      // Now hide it to see if it properly hides
      mo.isHidden = true;
      await waitForUpdateComplete(mo);
      ok(section.hasAttribute("hidden"), "Section is now hidden after setting isHidden = true.");
    });

    /**
     * Verify that clicking the confirm/deny buttons dispatches the correct events.
     */
     add_task(async function test_model_optin_confirm_deny() {
      const mo = document.querySelector("#model-optin");

      // Make sure it's visible and not loading
      mo.isHidden = false;
      mo.isLoading = false;
      await waitForUpdateComplete(mo);

      let eventsFired = [];
      function onConfirm() {
        eventsFired.push("MlModelOptinConfirm");
      }
      function onDeny() {
        eventsFired.push("MlModelOptinDeny");
      }

      mo.addEventListener("MlModelOptinConfirm", onConfirm);
      mo.addEventListener("MlModelOptinDeny", onDeny);

      // Grab the buttons by their IDs inside the component's shadow root
      const confirmBtn = mo.shadowRoot.querySelector("#optin-confirm-button");
      const denyBtn = mo.shadowRoot.querySelector("#optin-deny-button");
      ok(confirmBtn, "Found the confirm button with an ID");
      ok(denyBtn, "Found the deny button with an ID");

      // Synthesize a click on the confirm button
      synthesizeMouseAtCenter(confirmBtn, {});
      is(eventsFired.length, 1, "One event fired after confirm click.");
      is(eventsFired[0], "MlModelOptinConfirm", "Confirm event was fired.");

      // Synthesize a click on the deny button
      synthesizeMouseAtCenter(denyBtn, {});
      is(eventsFired.length, 2, "Second event fired after deny click.");
      is(eventsFired[1], "MlModelOptinDeny", "Deny event was fired.");
    });

    /**
     * Test loading mode: check that we see the inline <progress> bar & can cancel.
     */
    add_task(async function test_model_optin_loading_mode() {
      const mo = document.querySelector("#model-optin");
      mo.isHidden = false;
      mo.isLoading = true;
      mo.progressStatus = 30; // Show partial progress
      await waitForUpdateComplete(mo);

      // The cancel button plus a <progress> element should appear in loading mode
      const cancelBtn = mo.shadowRoot.querySelector("moz-button");
      ok(cancelBtn, "Found the cancel button in loading mode.");

      const progressEl = mo.shadowRoot.querySelector("progress.optin-progress-bar");
      ok(progressEl, "Found the inline <progress> in loading mode.");
      is(progressEl.getAttribute("value"), "30", "<progress> should have value='30'.");

      // Listen for the MlModelOptinCancelDownload event
      let cancelEventFired = false;
      mo.addEventListener("MlModelOptinCancelDownload", () => {
        cancelEventFired = true;
      });

      // Click the cancel button
      synthesizeMouseAtCenter(cancelBtn, {});
      is(cancelEventFired, true, "Should dispatch 'MlModelOptinCancelDownload' event on click.");
      is(mo.isLoading, false, "After canceling, isLoading should be false.");
      is(mo.progressStatus, undefined, "progressStatus is cleared after canceling.");
    });
  </script>
</head>

<body>
  <model-optin
    id="model-optin"
    class="genai-model-optin"
  >
  </model-optin>
</body>
</html>