summaryrefslogtreecommitdiffstats
path: root/remote/marionette/test/xpcshell/test_modal.js
blob: c36811f7ec809780d7303c7be9bb9ebf5b48ea15 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const { modal } = ChromeUtils.importESModule(
  "chrome://remote/content/marionette/modal.sys.mjs"
);

const chromeWindow = {};

const mockModalDialog = {
  docShell: {
    chromeEventHandler: null,
  },
  opener: {
    ownerGlobal: chromeWindow,
  },
  Dialog: {
    args: {
      modalType: Services.prompt.MODAL_TYPE_WINDOW,
    },
  },
};

const mockCurBrowser = {
  window: chromeWindow,
};

add_task(function test_addCallback() {
  let observer = new modal.DialogObserver(() => mockCurBrowser);
  let cb1 = () => true;
  let cb2 = () => false;

  equal(observer.callbacks.size, 0);
  observer.add(cb1);
  equal(observer.callbacks.size, 1);
  observer.add(cb1);
  equal(observer.callbacks.size, 1);
  observer.add(cb2);
  equal(observer.callbacks.size, 2);
});

add_task(function test_removeCallback() {
  let observer = new modal.DialogObserver(() => mockCurBrowser);
  let cb1 = () => true;
  let cb2 = () => false;

  equal(observer.callbacks.size, 0);
  observer.add(cb1);
  observer.add(cb2);

  equal(observer.callbacks.size, 2);
  observer.remove(cb1);
  equal(observer.callbacks.size, 1);
  observer.remove(cb1);
  equal(observer.callbacks.size, 1);
  observer.remove(cb2);
  equal(observer.callbacks.size, 0);
});

add_task(function test_registerDialogClosedEventHandler() {
  let observer = new modal.DialogObserver(() => mockCurBrowser);
  let mockChromeWindow = {
    addEventListener(event, cb) {
      equal(
        event,
        "DOMModalDialogClosed",
        "registered event for closing modal"
      );
      equal(cb, observer, "set itself as handler");
    },
  };

  observer.observe(mockChromeWindow, "domwindowopened");
});

add_task(function test_handleCallbackOpenModalDialog() {
  let observer = new modal.DialogObserver(() => mockCurBrowser);

  observer.add((action, dialog) => {
    equal(action, modal.ACTION_OPENED, "'opened' action has been passed");
    equal(dialog, mockModalDialog, "dialog has been passed");
  });
  observer.observe(mockModalDialog, "common-dialog-loaded");
});

add_task(function test_handleCallbackCloseModalDialog() {
  let observer = new modal.DialogObserver(() => mockCurBrowser);

  observer.add((action, dialog) => {
    equal(action, modal.ACTION_CLOSED, "'closed' action has been passed");
    equal(dialog, mockModalDialog, "dialog has been passed");
  });
  observer.handleEvent({
    type: "DOMModalDialogClosed",
    target: mockModalDialog,
  });
});

add_task(async function test_dialogClosed() {
  let observer = new modal.DialogObserver(() => mockCurBrowser);

  const dialogClosed = observer.dialogClosed();

  observer.handleEvent({
    type: "DOMModalDialogClosed",
    target: mockModalDialog,
  });

  await dialogClosed;
});