summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_widget_recreate_events.js
blob: 3eca9231a89a7baf0bc92c72e545e68747a67643 (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
"use strict";

const widgetData = {
  id: "test-widget",
  type: "view",
  viewId: "PanelUI-testbutton",
  label: "test widget label",
  onViewShowing() {},
  onViewHiding() {},
};

async function simulateWidgetOpen() {
  let testWidgetButton = document.getElementById("test-widget");
  let testWidgetShowing = BrowserTestUtils.waitForEvent(
    document,
    "popupshowing",
    true
  );
  testWidgetButton.click();
  await testWidgetShowing;
}

async function simulateWidgetClose() {
  let panel = document.getElementById("customizationui-widget-panel");
  let panelHidden = BrowserTestUtils.waitForEvent(panel, "popuphidden");

  panel.hidePopup();
  await panelHidden;
}

function createPanelView() {
  let panelView = document.createXULElement("panelview");
  panelView.id = "PanelUI-testbutton";
  let vbox = document.createXULElement("vbox");
  panelView.appendChild(vbox);
  return panelView;
}

/**
 * Check that panel view/hide events are added back,
 * if widget is destroyed and created again in one session.
 */
add_task(async function () {
  let viewCache = document.getElementById("appMenu-viewCache");
  let panelView = createPanelView();
  viewCache.appendChild(panelView);

  CustomizableUI.createWidget(widgetData);
  CustomizableUI.addWidgetToArea("test-widget", "nav-bar");

  // Simulate clicking and wait for the open
  // so we ensure the lazy event creation is done.
  await simulateWidgetOpen();

  let listeners = Services.els.getListenerInfoFor(panelView);
  ok(
    listeners.some(info => info.type == "ViewShowing"),
    "ViewShowing event added"
  );
  ok(
    listeners.some(info => info.type == "ViewHiding"),
    "ViewHiding event added"
  );

  await simulateWidgetClose();
  CustomizableUI.destroyWidget("test-widget");

  listeners = Services.els.getListenerInfoFor(panelView);
  // Ensure the events got removed after destorying the widget.
  ok(
    !listeners.some(info => info.type == "ViewShowing"),
    "ViewShowing event removed"
  );
  ok(
    !listeners.some(info => info.type == "ViewHiding"),
    "ViewHiding event removed"
  );

  CustomizableUI.createWidget(widgetData);
  // Simulate clicking and wait for the open
  // so we ensure the lazy event creation is done.
  // We need to do this again because we destroyed the widget.
  await simulateWidgetOpen();

  listeners = Services.els.getListenerInfoFor(panelView);
  ok(
    listeners.some(info => info.type == "ViewShowing"),
    "ViewShowing event added again"
  );
  ok(
    listeners.some(info => info.type == "ViewHiding"),
    "ViewHiding event added again"
  );

  await simulateWidgetClose();
  CustomizableUI.destroyWidget("test-widget");
  panelView.remove();
  CustomizableUI.reset();
});