summaryrefslogtreecommitdiffstats
path: root/dom/events/test/bug299673.js
blob: 07e33bd1371f0fe5e87d061cd8530024bdb96c7e (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var popup;

function OpenWindow() {
  log({}, ">>> OpenWindow");
  popup = window.open("", "Test");

  var output = "<html>";

  output += "<body>";
  output += "<form>";
  output +=
    "<input id='popupText1' type='text' onfocus='opener.log(event)' onblur='opener.log(event)'>";
  output += "</form>";
  output += "</body>";
  output += "</html>";

  popup.document.open();
  popup.document.write(output);
  popup.document.close();

  popup.document.onclick = function(event) {
    log(event, "popup-doc");
  };
  popup.document.onfocus = function(event) {
    log(event, "popup-doc");
  };
  popup.document.onblur = function(event) {
    log(event, "popup-doc");
  };
  popup.document.onchange = function(event) {
    log(event, "popup-doc");
  };

  var e = popup.document.getElementById("popupText1");
  popup.focus();
  e.focus();
  is(
    popup.document.activeElement,
    e,
    "input element in popup should be focused"
  );
  log({}, "<<< OpenWindow");
}

var result;

function log(event, message) {
  if (event && event.eventPhase == 3) {
    return;
  }
  e = event.currentTarget || event.target || event.srcElement;
  var id = e ? (e.id ? e.id : e.name ? e.name : e.value ? e.value : "") : "";
  if (id) {
    id = "(" + id + ")";
  }
  result +=
    (e ? (e.tagName ? e.tagName : "") : " ") +
    id +
    ": " +
    (event.type ? event.type : "") +
    " " +
    (message ? message : "") +
    "\n";
}

document.onclick = function(event) {
  log(event, "top-doc");
};
document.onfocus = function(event) {
  log(event, "top-doc");
};
document.onblur = function(event) {
  log(event, "top-doc");
};
document.onchange = function(event) {
  log(event, "top-doc");
};

function doTest1_rest2(expectedEventLog, focusAfterCloseId) {
  try {
    is(
      document.activeElement,
      document.getElementById(focusAfterCloseId),
      "wrong element is focused after popup was closed"
    );
    is(result, expectedEventLog, "unexpected events");
    SimpleTest.finish();
  } catch (e) {
    if (popup) {
      popup.close();
    }
    throw e;
  }
}
function doTest1_rest1(expectedEventLog, focusAfterCloseId) {
  try {
    synthesizeKey("V", {}, popup);
    synthesizeKey("A", {}, popup);
    synthesizeKey("L", {}, popup);
    is(
      popup.document.getElementById("popupText1").value,
      "VAL",
      "input element in popup did not accept input"
    );

    var p = popup;
    popup = null;
    p.close();

    SimpleTest.waitForFocus(function() {
      doTest1_rest2(expectedEventLog, focusAfterCloseId);
    }, window);
  } catch (e) {
    if (popup) {
      popup.close();
    }
    throw e;
  }
}

function doTest1(expectedEventLog, focusAfterCloseId) {
  try {
    var select1 = document.getElementById("Select1");
    select1.focus();
    is(document.activeElement, select1, "select element should be focused");
    synthesizeKey("KEY_ArrowDown");
    synthesizeKey("KEY_Tab");
    SimpleTest.waitForFocus(function() {
      doTest1_rest1(expectedEventLog, focusAfterCloseId);
    }, popup);
  } catch (e) {
    if (popup) {
      popup.close();
    }
    throw e;
  }
}

function setPrefAndDoTest(expectedEventLog, focusAfterCloseId, prefValue) {
  var select1 = document.getElementById("Select1");
  select1.blur();
  result = "";
  log({}, "Test with browser.link.open_newwindow = " + prefValue);
  SpecialPowers.pushPrefEnv(
    { set: [["browser.link.open_newwindow", prefValue]] },
    function() {
      doTest1(expectedEventLog, focusAfterCloseId);
    }
  );
}