How to use waitForEvent method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

filereader_test.js

Source: filereader_test.js Github

copy
1// Copyright 2011 The Closure Library Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS-IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15goog.provide('goog.testing.fs.FileReaderTest');
16goog.setTestOnly('goog.testing.fs.FileReaderTest');
17
18goog.require('goog.Timer');
19goog.require('goog.async.Deferred');
20goog.require('goog.events');
21goog.require('goog.fs.Error');
22goog.require('goog.fs.FileReader');
23goog.require('goog.fs.FileSaver');
24goog.require('goog.testing.AsyncTestCase');
25goog.require('goog.testing.fs.FileReader');
26goog.require('goog.testing.fs.FileSystem');
27goog.require('goog.testing.jsunit');
28
29var asyncTestCase = goog.testing.AsyncTestCase.createAndInstall();
30var file, deferredReader;
31var hasArrayBuffer = goog.isDef(goog.global.ArrayBuffer);
32
33function setUp() {
34  var fs = new goog.testing.fs.FileSystem();
35  var fileEntry = fs.getRoot().createDirectorySync('foo').createFileSync('bar');
36  file = fileEntry.fileSync();
37  file.setDataInternal('test content');
38
39  deferredReader = new goog.async.Deferred();
40  goog.Timer.callOnce(
41      goog.bind(deferredReader.callback, deferredReader,
42          new goog.testing.fs.FileReader()));
43}
44
45function testRead() {
46  deferredReader.
47      addCallback(goog.partial(checkReadyState,
48                               goog.fs.FileReader.ReadyState.INIT)).
49      addCallback(goog.partial(checkResult, undefined)).
50      addCallback(goog.partial(readAsText)).
51      addCallback(goog.partial(checkReadyState,
52                               goog.fs.FileReader.ReadyState.LOADING)).
53      addCallback(goog.partial(checkResult, undefined)).
54      addCallback(goog.partial(waitForEvent,
55                               goog.fs.FileReader.EventType.LOAD_START)).
56      addCallback(goog.partial(checkResult, undefined)).
57      addCallback(goog.partial(waitForEvent,
58                               goog.fs.FileReader.EventType.LOAD)).
59      addCallback(goog.partial(checkResult, undefined)).
60      addCallback(goog.partial(waitForEvent,
61                               goog.fs.FileReader.EventType.LOAD_END)).
62      addCallback(goog.partial(checkResult, file.toString())).
63      addCallback(goog.partial(checkReadyState,
64                               goog.fs.FileReader.ReadyState.DONE)).
65      addBoth(continueTesting);
66  waitForAsync('testRead');
67}
68
69function testReadAsArrayBuffer() {
70  if (!hasArrayBuffer) {
71    // Skip if array buffer is not supported
72    return;
73  }
74  deferredReader.
75      addCallback(goog.partial(checkReadyState,
76                               goog.fs.FileReader.ReadyState.INIT)).
77      addCallback(goog.partial(checkResult, undefined)).
78      addCallback(goog.partial(readAsArrayBuffer)).
79      addCallback(goog.partial(checkReadyState,
80                               goog.fs.FileReader.ReadyState.LOADING)).
81      addCallback(goog.partial(checkResult, undefined)).
82      addCallback(goog.partial(waitForEvent,
83                               goog.fs.FileReader.EventType.LOAD_START)).
84      addCallback(goog.partial(checkResult, undefined)).
85      addCallback(goog.partial(waitForEvent,
86                               goog.fs.FileReader.EventType.LOAD)).
87      addCallback(goog.partial(checkResult, undefined)).
88      addCallback(goog.partial(waitForEvent,
89                               goog.fs.FileReader.EventType.LOAD_END)).
90      addCallback(goog.partial(checkResult, file.toArrayBuffer())).
91      addCallback(goog.partial(checkReadyState,
92                               goog.fs.FileReader.ReadyState.DONE)).
93      addBoth(continueTesting);
94  waitForAsync('testReadAsArrayBuffer');
95}
96
97function testReadAsDataUrl() {
98  deferredReader.
99      addCallback(goog.partial(checkReadyState,
100                               goog.fs.FileReader.ReadyState.INIT)).
101      addCallback(goog.partial(checkResult, undefined)).
102      addCallback(goog.partial(readAsDataUrl)).
103      addCallback(goog.partial(checkReadyState,
104                               goog.fs.FileReader.ReadyState.LOADING)).
105      addCallback(goog.partial(checkResult, undefined)).
106      addCallback(goog.partial(waitForEvent,
107                               goog.fs.FileReader.EventType.LOAD_START)).
108      addCallback(goog.partial(checkResult, undefined)).
109      addCallback(goog.partial(waitForEvent,
110                               goog.fs.FileReader.EventType.LOAD)).
111      addCallback(goog.partial(checkResult, undefined)).
112      addCallback(goog.partial(waitForEvent,
113                               goog.fs.FileReader.EventType.LOAD_END)).
114      addCallback(goog.partial(checkResult, file.toDataUrl())).
115      addCallback(goog.partial(checkReadyState,
116                               goog.fs.FileReader.ReadyState.DONE)).
117      addBoth(continueTesting);
118  waitForAsync('testReadAsDataUrl');
119}
120
121function testAbort() {
122  deferredReader.
123      addCallback(goog.partial(readAsText)).
124      addCallback(function(reader) { reader.abort(); }).
125      addCallback(goog.partial(checkReadyState,
126                               goog.fs.FileReader.ReadyState.LOADING)).
127      addCallback(goog.partial(waitForError, goog.fs.Error.ErrorCode.ABORT)).
128      addCallback(goog.partial(checkReadyState,
129                               goog.fs.FileReader.ReadyState.LOADING)).
130      addCallback(goog.partial(waitForEvent,
131                               goog.fs.FileReader.EventType.ABORT)).
132      addCallback(goog.partial(checkReadyState,
133                               goog.fs.FileReader.ReadyState.LOADING)).
134      addCallback(goog.partial(waitForEvent,
135                               goog.fs.FileReader.EventType.LOAD_END)).
136      addCallback(goog.partial(checkReadyState,
137                               goog.fs.FileReader.ReadyState.DONE)).
138      addCallback(goog.partial(checkResult, undefined)).
139      addBoth(continueTesting);
140  waitForAsync('testAbort');
141}
142
143function testAbortBeforeRead() {
144  deferredReader.
145      addCallback(function(reader) { reader.abort(); }).
146      addErrback(function(err) {
147        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
148        return true;
149      }).
150      addCallback(function(calledErrback) {
151        assertTrue(calledErrback);
152      }).
153      addBoth(continueTesting);
154  waitForAsync('testAbortBeforeRead');
155}
156
157function testReadDuringRead() {
158  deferredReader.
159      addCallback(goog.partial(readAsText)).
160      addCallback(goog.partial(readAsText)).
161      addErrback(function(err) {
162        assertEquals(goog.fs.Error.ErrorCode.INVALID_STATE, err.code);
163        return true;
164      }).
165      addCallback(assertTrue).
166      addBoth(continueTesting);
167  waitForAsync('testReadDuringRead');
168}
169
170function continueTesting(result) {
171  asyncTestCase.continueTesting();
172  if (result instanceof Error) {
173    throw result;
174  }
175}
176
177function waitForAsync(msg) {
178  asyncTestCase.waitForAsync(msg);
179}
180
181function waitForEvent(type, target) {
182  var d = new goog.async.Deferred();
183  goog.events.listenOnce(target, type, goog.bind(d.callback, d, target));
184  return d;
185}
186
187function waitForError(type, target) {
188  var d = new goog.async.Deferred();
189  goog.events.listenOnce(
190      target, goog.fs.FileReader.EventType.ERROR, function(e) {
191        assertEquals(type, target.getError().code);
192        d.callback(target);
193      });
194  return d;
195}
196
197function readAsText(reader) {
198  reader.readAsText(file);
199}
200
201function readAsArrayBuffer(reader) {
202  reader.readAsArrayBuffer(file);
203}
204
205function readAsDataUrl(reader) {
206  reader.readAsDataUrl(file);
207}
208
209function readAndWait(reader) {
210  readAsText(reader);
211  return waitForEvent(goog.fs.FileSaver.EventType.LOAD_END, reader);
212}
213
214function checkResult(expectedResult, reader) {
215  checkEquals(expectedResult, reader.getResult());
216}
217
218function checkEquals(a, b) {
219  if (hasArrayBuffer &&
220      a instanceof ArrayBuffer && b instanceof ArrayBuffer) {
221    assertEquals(a.byteLength, b.byteLength);
222    var viewA = new Uint8Array(a);
223    var viewB = new Uint8Array(b);
224    for (var i = 0; i < a.byteLength; i++) {
225      assertEquals(viewA[i], viewB[i]);
226    }
227  } else {
228    assertEquals(a, b);
229  }
230}
231
232function checkReadyState(expectedState, reader) {
233  assertEquals(expectedState, reader.getReadyState());
234}
235
Full Screen

browser_context_menu_tests.js

Source: browser_context_menu_tests.js Github

copy
1// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
2/* vim: set ts=2 et sw=2 tw=80: */
3/* Any copyright is dedicated to the Public Domain.
4   http://creativecommons.org/publicdomain/zero/1.0/ */
5
6"use strict";
7
8function debugClipFlavors(aClip)
9{
10  let xfer = Cc["@mozilla.org/widget/transferable;1"].
11             createInstance(Ci.nsITransferable);
12  xfer.init(null);
13  aClip.getData(xfer, Ci.nsIClipboard.kGlobalClipboard);
14  let array = xfer.flavorsTransferableCanExport();
15  let count = array.Count();
16  info("flavors:" + count);
17  for (let idx = 0; idx < count; idx++) {
18    let string = array.GetElementAt(idx).QueryInterface(Ci.nsISupportsString);
19    info("[" + idx + "] " + string);
20  }
21}
22
23function checkContextMenuPositionRange(aElement, aMinLeft, aMaxLeft, aMinTop, aMaxTop) {
24  ok(aElement.left > aMinLeft && aElement.left < aMaxLeft,
25    "Left position is " + aElement.left + ", expected between " + aMinLeft + " and " + aMaxLeft);
26
27  ok(aElement.top > aMinTop && aElement.top < aMaxTop,
28    "Top position is " + aElement.top + ", expected between " + aMinTop + " and " + aMaxTop);
29}
30
31gTests.push({
32  desc: "text context menu",
33  run: function test() {
34    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
35
36    info(chromeRoot + "browser_context_menu_tests_02.html");
37    yield addTab(chromeRoot + "browser_context_menu_tests_02.html");
38
39    purgeEventQueue();
40    emptyClipboard();
41
42    let win = Browser.selectedTab.browser.contentWindow;
43
44    yield hideContextUI();
45
46    ////////////////////////////////////////////////////////////
47    // Context menu in content on selected text
48
49    // select some text
50    let span = win.document.getElementById("text1");
51    win.getSelection().selectAllChildren(span);
52
53    yield waitForMs(0);
54
55    // invoke selection context menu
56    let promise = waitForEvent(document, "popupshown");
57    sendContextMenuClickToElement(win, span, 85, 10);
58    yield promise;
59
60    // should be visible
61    ok(ContextMenuUI._menuPopup._visible, "is visible");
62
63    // selected text context:
64    checkContextUIMenuItemVisibility(["context-copy",
65                                      "context-search"]);
66
67    let menuItem = document.getElementById("context-copy");
68    promise = waitForEvent(document, "popuphidden");
69    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
70
71    yield promise;
72
73    // The wait is needed to give time to populate the clipboard.
74    let string = "";
75    yield waitForCondition(function () {
76      string = SpecialPowers.getClipboardData("text/unicode");
77      return string === span.textContent;
78    });
79    ok(string === span.textContent, "copied selected text from span");
80
81    win.getSelection().removeAllRanges();
82
83    ////////////////////////////////////////////////////////////
84    // Context menu in content on selected text that includes a link
85
86    // invoke selection with link context menu
87    let link = win.document.getElementById("text2-link");
88    win.getSelection().selectAllChildren(link);
89    promise = waitForEvent(document, "popupshown");
90    sendContextMenuClickToElement(win, link, 40, 10);
91    yield promise;
92
93    // should be visible
94    ok(ContextMenuUI._menuPopup._visible, "is visible");
95
96    // selected text context:
97    checkContextUIMenuItemVisibility(["context-copy",
98                                      "context-search",
99                                      "context-open-in-new-tab",
100                                      "context-copy-link"]);
101
102    promise = waitForEvent(document, "popuphidden");
103    ContextMenuUI.hide();
104    yield promise;
105    win.getSelection().removeAllRanges();
106
107    ////////////////////////////////////////////////////////////
108    // Context menu in content on a link
109
110    link = win.document.getElementById("text2-link");
111    promise = waitForEvent(document, "popupshown");
112    sendContextMenuClickToElement(win, link, 40, 10);
113    yield promise;
114
115    // should be visible
116    ok(ContextMenuUI._menuPopup._visible, "is visible");
117
118    // selected text context:
119    checkContextUIMenuItemVisibility(["context-open-in-new-tab",
120                                      "context-copy-link",
121                                      "context-bookmark-link"]);
122
123    promise = waitForEvent(document, "popuphidden");
124    ContextMenuUI.hide();
125    yield promise;
126
127    ////////////////////////////////////////////////////////////
128    // context in input with no selection, no data on clipboard
129
130    emptyClipboard();
131
132    let input = win.document.getElementById("text3-input");
133    promise = waitForEvent(document, "popupshown");
134    sendContextMenuClickToElement(win, input, 20, 10);
135    yield promise;
136
137    // should be visible
138    ok(ContextMenuUI._menuPopup._visible, "is visible");
139
140    checkContextUIMenuItemVisibility(["context-select",
141                                      "context-select-all"]);
142
143    // copy menu item should not exist when no text is selected
144    let menuItem = document.getElementById("context-copy");
145    ok(menuItem && menuItem.hidden, "menu item is not visible");
146
147    promise = waitForEvent(document, "popuphidden");
148    ContextMenuUI.hide();
149    yield promise;
150
151    ////////////////////////////////////////////////////////////
152    // context in input with selection copied to clipboard
153
154    let input = win.document.getElementById("text3-input");
155    input.value = "hello, I'm sorry but I must be going.";
156    input.setSelectionRange(0, 5);
157    promise = waitForEvent(document, "popupshown");
158    sendContextMenuClickToElement(win, input, 20, 10);
159    yield promise;
160
161    // should be visible
162    ok(ContextMenuUI._menuPopup._visible, "is visible");
163
164    checkContextUIMenuItemVisibility(["context-cut",
165                                      "context-copy"]);
166
167    let menuItem = document.getElementById("context-copy");
168    let popupPromise = waitForEvent(document, "popuphidden");
169    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
170
171    yield popupPromise;
172
173    // The wait is needed to give time to populate the clipboard.
174    let string = "";
175    yield waitForCondition(function () {
176      string = SpecialPowers.getClipboardData("text/unicode");
177      return string === "hello";
178    });
179
180    ok(string === "hello", "copied selected text");
181
182    emptyClipboard();
183
184    ////////////////////////////////////////////////////////////
185    // context in input with text selection, no data on clipboard
186
187    input = win.document.getElementById("text3-input");
188    input.select();
189    promise = waitForEvent(document, "popupshown");
190    sendContextMenuClickToElement(win, input, 20, 10);
191    yield promise;
192
193    // should be visible
194    ok(ContextMenuUI._menuPopup._visible, "is visible");
195
196    // selected text context:
197    checkContextUIMenuItemVisibility(["context-cut",
198                                      "context-copy"]);
199
200    promise = waitForEvent(document, "popuphidden");
201    ContextMenuUI.hide();
202    yield promise;
203
204    ////////////////////////////////////////////////////////////
205    // context in input with no selection, data on clipboard
206
207    SpecialPowers.clipboardCopyString("foo");
208    input = win.document.getElementById("text3-input");
209    input.select();
210    promise = waitForEvent(document, "popupshown");
211    sendContextMenuClickToElement(win, input, 20, 10);
212    yield promise;
213
214    // should be visible
215    ok(ContextMenuUI._menuPopup._visible, "is visible");
216
217    // selected text context:
218    checkContextUIMenuItemVisibility(["context-cut",
219                                      "context-copy",
220                                      "context-paste"]);
221
222    promise = waitForEvent(document, "popuphidden");
223    ContextMenuUI.hide();
224    yield promise;
225
226    ////////////////////////////////////////////////////////////
227    // context in input with selection cut to clipboard
228
229    emptyClipboard();
230
231    let input = win.document.getElementById("text3-input");
232    input.value = "hello, I'm sorry but I must be going.";
233    input.setSelectionRange(0, 5);
234    promise = waitForEvent(document, "popupshown");
235    sendContextMenuClickToElement(win, input, 20, 10);
236    yield promise;
237
238    // should be visible
239    ok(ContextMenuUI._menuPopup._visible, "is visible");
240
241    checkContextUIMenuItemVisibility(["context-cut",
242                                      "context-copy"]);
243
244    let menuItem = document.getElementById("context-cut");
245    let popupPromise = waitForEvent(document, "popuphidden");
246    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
247
248    yield popupPromise;
249
250    // The wait is needed to give time to populate the clipboard.
251    let string = "";
252    yield waitForCondition(function () {
253      string = SpecialPowers.getClipboardData("text/unicode");
254      return string === "hello";
255    });
256
257    let inputValue = input.value;
258    ok(string === "hello", "cut selected text in clipboard");
259    ok(inputValue === ", I'm sorry but I must be going.", "cut selected text from input value");
260
261    emptyClipboard();
262
263    ////////////////////////////////////////////////////////////
264    // context in empty input, data on clipboard (paste operation)
265
266    SpecialPowers.clipboardCopyString("foo");
267    input = win.document.getElementById("text3-input");
268    input.value = "";
269
270    promise = waitForEvent(document, "popupshown");
271    sendContextMenuClickToElement(win, input, 20, 10);
272    yield promise;
273
274    // should be visible
275    ok(ContextMenuUI._menuPopup._visible, "is visible");
276
277    // selected text context:
278    checkContextUIMenuItemVisibility(["context-paste"]);
279
280    promise = waitForEvent(document, "popuphidden");
281    ContextMenuUI.hide();
282    yield promise;
283
284    ////////////////////////////////////////////////////////////
285    // context in empty input, no data on clipboard (??)
286
287    emptyClipboard();
288    ContextUI.dismiss();
289
290    input = win.document.getElementById("text3-input");
291    input.value = "";
292
293    promise = waitForEvent(Elements.tray, "transitionend");
294    sendContextMenuClickToElement(win, input, 20, 10);
295    yield promise;
296
297    // should *not* be visible
298    ok(!ContextMenuUI._menuPopup._visible, "is visible");
299
300    // the test above will invoke the app bar
301    yield hideContextUI();
302
303    Browser.closeTab(Browser.selectedTab, { forceClose: true });
304    purgeEventQueue();
305  }
306});
307
308gTests.push({
309  desc: "checks for context menu positioning when browser shifts",
310  run: function test() {
311    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
312
313    info(chromeRoot + "browser_context_menu_tests_02.html");
314    yield addTab(chromeRoot + "browser_context_menu_tests_02.html");
315
316    purgeEventQueue();
317    emptyClipboard();
318
319    let browserwin = Browser.selectedTab.browser.contentWindow;
320
321    yield hideContextUI();
322
323    ////////////////////////////////////////////////////////////
324    // test for proper context menu positioning when the browser
325    // is offset by a notification box.
326
327    yield showNotification();
328
329    // select some text
330    let span = browserwin.document.getElementById("text4");
331    browserwin.getSelection().selectAllChildren(span);
332
333    // invoke selection context menu
334    let promise = waitForEvent(document, "popupshown");
335    sendContextMenuClick(225, 310);
336    yield promise;
337
338    // should be visible and at a specific position
339    ok(ContextMenuUI._menuPopup._visible, "is visible");
340
341    let notificationBox = Browser.getNotificationBox();
342    let notification = notificationBox.getNotificationWithValue("popup-blocked");
343    let notificationHeight = notification.boxObject.height;
344
345    checkContextMenuPositionRange(ContextMenuUI._panel, 65, 80, notificationHeight +  155, notificationHeight + 180);
346
347    promise = waitForEvent(document, "popuphidden");
348    ContextMenuUI.hide();
349    yield promise;
350
351    Browser.closeTab(Browser.selectedTab, { forceClose: true });
352  }
353});
354
355var observeLogger = {
356  observe: function (aSubject, aTopic, aData) {
357    info("observeLogger: " + aTopic);
358  },
359  QueryInterface: function (aIID) {
360    if (!aIID.equals(Ci.nsIObserver) &&
361        !aIID.equals(Ci.nsISupportsWeakReference) &&
362        !aIID.equals(Ci.nsISupports)) {
363      throw Components.results.NS_ERROR_NO_INTERFACE;
364    }
365    return this;
366  },
367  init: function init() {
368    Services.obs.addObserver(observeLogger, "dl-start", true);
369    Services.obs.addObserver(observeLogger, "dl-done", true);
370    Services.obs.addObserver(observeLogger, "dl-failed", true);
371    Services.obs.addObserver(observeLogger, "dl-scanning", true);
372    Services.obs.addObserver(observeLogger, "dl-blocked", true);
373    Services.obs.addObserver(observeLogger, "dl-dirty", true);
374    Services.obs.addObserver(observeLogger, "dl-cancel", true);
375  },
376  shutdown: function shutdown() {
377    Services.obs.removeObserver(observeLogger, "dl-start");
378    Services.obs.removeObserver(observeLogger, "dl-done");
379    Services.obs.removeObserver(observeLogger, "dl-failed");
380    Services.obs.removeObserver(observeLogger, "dl-scanning");
381    Services.obs.removeObserver(observeLogger, "dl-blocked");
382    Services.obs.removeObserver(observeLogger, "dl-dirty");
383    Services.obs.removeObserver(observeLogger, "dl-cancel");
384  }
385}
386
387// Image context menu tests
388gTests.push({
389  desc: "image context menu",
390  setUp: function() {
391    observeLogger.init();
392  },
393  tearDown: function() {
394    observeLogger.shutdown();
395  },
396  run: function test() {
397    info(chromeRoot + "browser_context_menu_tests_01.html");
398    yield addTab(chromeRoot + "browser_context_menu_tests_01.html");
399
400    let win = Browser.selectedTab.browser.contentWindow;
401
402    purgeEventQueue();
403
404    yield hideContextUI();
405
406    // If we don't do this, sometimes the first sendContextMenuClickToWindow
407    // will trigger the app bar.
408    yield waitForImageLoad(win, "image01");
409
410    ////////////////////////////////////////////////////////////
411    // Context menu options
412
413    // image01 - 1x1x100x100
414    let promise = waitForEvent(document, "popupshown");
415    sendContextMenuClickToWindow(win, 10, 10);
416    yield promise;
417
418    purgeEventQueue();
419
420    ok(ContextMenuUI._menuPopup._visible, "is visible");
421
422    checkContextUIMenuItemVisibility(["context-save-image-lib",
423                                      "context-copy-image",
424                                      "context-copy-image-loc",
425                                      "context-open-image-tab"]);
426
427    ////////////////////////////////////////////////////////////
428    // Save to image library
429
430    let dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
431                           .getService(Components.interfaces.nsIProperties);
432    let saveLocationPath = dirSvc.get("Pict", Components.interfaces.nsIFile);
433    saveLocationPath.append("image01.png");
434
435    registerCleanupFunction(function () {
436      saveLocationPath.remove(false);
437    });
438
439    if (saveLocationPath.exists()) {
440      info("had to remove old image!");
441      saveLocationPath.remove(false);
442    }
443
444    let menuItem = document.getElementById("context-save-image-lib");
445    ok(menuItem, "menu item exists");
446    ok(!menuItem.hidden, "menu item visible");
447
448    // dl-start, dl-failed, dl-scanning, dl-blocked, dl-dirty, dl-cancel
449    let downloadPromise = waitForObserver("dl-done", 10000);
450
451    let popupPromise = waitForEvent(document, "popuphidden");
452    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
453    yield popupPromise;
454    yield downloadPromise;
455
456    purgeEventQueue();
457
458    ok(saveLocationPath.exists(), "image saved");
459
460    ////////////////////////////////////////////////////////////
461    // Copy image
462
463    let promise = waitForEvent(document, "popupshown");
464    sendContextMenuClickToWindow(win, 20, 20);
465    yield promise;
466    ok(ContextMenuUI._menuPopup._visible, "is visible");
467
468    menuItem = document.getElementById("context-copy-image");
469    ok(menuItem, "menu item exists");
470    ok(!menuItem.hidden, "menu item visible");
471    popupPromise = waitForEvent(document, "popuphidden");
472    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
473    yield popupPromise;
474
475    purgeEventQueue();
476
477    let clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
478    let flavors = ["image/png"];
479    ok(clip.hasDataMatchingFlavors(flavors, flavors.length, Ci.nsIClipboard.kGlobalClipboard), "clip has my png flavor");
480
481    ////////////////////////////////////////////////////////////
482    // Copy image location
483
484    promise = waitForEvent(document, "popupshown");
485    sendContextMenuClickToWindow(win, 30, 30);
486    yield promise;
487    ok(ContextMenuUI._menuPopup._visible, "is visible");
488
489    menuItem = document.getElementById("context-copy-image-loc");
490    ok(menuItem, "menu item exists");
491    ok(!menuItem.hidden, "menu item visible");
492    popupPromise = waitForEvent(document, "popuphidden");
493    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
494    yield popupPromise;
495
496    purgeEventQueue();
497
498    let clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
499    let flavors = ["text/unicode"];
500    ok(clip.hasDataMatchingFlavors(flavors, flavors.length, Ci.nsIClipboard.kGlobalClipboard), "clip has my text flavor");
501
502    let xfer = Cc["@mozilla.org/widget/transferable;1"].
503               createInstance(Ci.nsITransferable);
504    xfer.init(null);
505    xfer.addDataFlavor("text/unicode");
506    clip.getData(xfer, Ci.nsIClipboard.kGlobalClipboard);
507    let str = new Object();
508    let strLength = new Object();
509    xfer.getTransferData("text/unicode", str, strLength);
510    str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
511
512    ok(str == chromeRoot + "res/image01.png", "url copied");
513
514    ////////////////////////////////////////////////////////////
515    // Open image in new tab
516
517    promise = waitForEvent(document, "popupshown");
518    sendContextMenuClickToWindow(win, 40, 40);
519    yield promise;
520    ok(ContextMenuUI._menuPopup._visible, "is visible");
521
522    menuItem = document.getElementById("context-open-image-tab");
523    ok(menuItem, "menu item exists");
524    ok(!menuItem.hidden, "menu item visible");
525    let tabPromise = waitForEvent(document, "TabOpen");
526    popupPromise = waitForEvent(document, "popuphidden");
527    EventUtils.synthesizeMouse(menuItem, 10, 10, {}, win);
528    yield popupPromise;
529    let event = yield tabPromise;
530
531    purgeEventQueue();
532
533    let imagetab = Browser.getTabFromChrome(event.originalTarget);
534    ok(imagetab != null, "tab created");
535
536    Browser.closeTab(imagetab, { forceClose: true });
537  }
538});
539
540gTests.push({
541  desc: "tests for subframe positioning",
542  run: function test() {
543    info(chromeRoot + "browser_context_menu_tests_03.html");
544    yield addTab(chromeRoot + "browser_context_menu_tests_03.html");
545
546    let win = Browser.selectedTab.browser.contentWindow;
547
548    // Sometimes the context ui is visible, sometimes it isn't.
549    try {
550      yield waitForCondition(function () {
551        return ContextUI.isVisible;
552      }, 500, 50);
553    } catch (ex) {}
554
555    ContextUI.dismiss();
556
557    let frame1 = win.document.getElementById("frame1");
558    let link1 = frame1.contentDocument.getElementById("link1");
559
560    let promise = waitForEvent(document, "popupshown");
561    sendContextMenuClickToElement(frame1.contentDocument.defaultView, link1, 85, 10);
562    yield promise;
563
564    // should be visible
565    ok(ContextMenuUI._menuPopup._visible, "is visible");
566
567    checkContextMenuPositionRange(ContextMenuUI._panel, 265, 280, 175, 190);
568
569    promise = waitForEvent(document, "popuphidden");
570    ContextMenuUI.hide();
571    yield promise;
572
573    frame1.contentDocument.defaultView.scrollBy(0, 200);
574
575    promise = waitForEvent(document, "popupshown");
576    sendContextMenuClickToElement(frame1.contentDocument.defaultView, link1, 85, 10);
577    yield promise;
578
579    // should be visible
580    ok(ContextMenuUI._menuPopup._visible, "is visible");
581
582    checkContextMenuPositionRange(ContextMenuUI._panel, 265, 280, 95, 110);
583
584    promise = waitForEvent(document, "popuphidden");
585    ContextMenuUI.hide();
586    yield promise;
587
588    let rlink1 = win.document.getElementById("rlink1");
589
590    promise = waitForEvent(document, "popupshown");
591    sendContextMenuClickToElement(win, rlink1, 40, 10);
592    yield promise;
593
594    // should be visible
595    ok(ContextMenuUI._menuPopup._visible, "is visible");
596
597    checkContextMenuPositionRange(ContextMenuUI._panel, 295, 310, 540, 555);
598
599    promise = waitForEvent(document, "popuphidden");
600    ContextMenuUI.hide();
601    yield promise;
602
603    win.scrollBy(0, 200);
604
605    promise = waitForEvent(document, "popupshown");
606    sendContextMenuClickToElement(win, rlink1, 40, 10);
607    yield promise;
608
609    // should be visible
610    ok(ContextMenuUI._menuPopup._visible, "is visible");
611
612    checkContextMenuPositionRange(ContextMenuUI._panel, 295, 310, 340, 355);
613
614    promise = waitForEvent(document, "popuphidden");
615    ContextMenuUI.hide();
616    yield promise;
617
618    let link2 = frame1.contentDocument.getElementById("link2");
619
620    promise = waitForEvent(document, "popupshown");
621    sendContextMenuClickToElement(frame1.contentDocument.defaultView, link2, 85, 10);
622    yield promise;
623
624    // should be visible
625    ok(ContextMenuUI._menuPopup._visible, "is visible");
626
627    checkContextMenuPositionRange(ContextMenuUI._panel, 265, 280, 110, 125);
628
629    promise = waitForEvent(document, "popuphidden");
630    ContextMenuUI.hide();
631    yield promise;
632  }
633});
634
635function test() {
636  runTests();
637}
638
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)