summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/private-browsing-supported/test-panel.js
blob: 1ba3e955447eebbbc0f9a5d5b4f5b2878df76eae (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
/* 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 { open, focus, close } = require('sdk/window/helpers');
const { isPrivate } = require('sdk/private-browsing');
const { defer } = require('sdk/core/promise');
const { browserWindows: windows } = require('sdk/windows');
const { getInnerId, getMostRecentBrowserWindow } = require('sdk/window/utils');
const { getActiveView } = require('sdk/view/core');

const BROWSER = 'chrome://browser/content/browser.xul';

exports.testRequirePanel = function(assert) {
  require('sdk/panel');
  assert.ok('the panel module should not throw an error');
};

exports.testShowPanelInPrivateWindow = function(assert, done) {
  let panel = require('sdk/panel').Panel({
    contentURL: "data:text/html;charset=utf-8,I'm a leaf on the wind"
  });

  assert.ok(windows.length > 0, 'there is at least one open window');
  for (let window of windows) {
    assert.equal(isPrivate(window), false, 'open window is private');
  }

  let panelView = getActiveView(panel);
  let expectedWindowId = getInnerId(panelView.backgroundFrame.contentWindow);

  function checkPanelFrame() {
    let iframe = panelView.firstChild;

    assert.equal(panelView.viewFrame, iframe, 'panel has the correct viewFrame value');

    let windowId = getInnerId(iframe.contentWindow);

    assert.equal(windowId, expectedWindowId, 'panel has the correct window visible');

    assert.equal(iframe.contentDocument.body.textContent,
                 "I'm a leaf on the wind",
                 'the panel has the expected content');
  }

  function testPanel(window) {
    let { promise, resolve } = defer();

    assert.ok(!panel.isShowing, 'the panel is not showing [1]');

    panel.once('show', function() {
      assert.ok(panel.isShowing, 'the panel is showing');

      checkPanelFrame();

      panel.once('hide', function() {
        assert.ok(!panel.isShowing, 'the panel is not showing [2]');

        resolve(window);
      });

      panel.hide();
    });

    panel.show();

    return promise;
  };

  let initialWindow = getMostRecentBrowserWindow();

  testPanel(initialWindow).
    then(makeEmptyPrivateBrowserWindow).
    then(focus).
    then(function(window) {
      assert.equal(isPrivate(window), true, 'opened window is private');
      assert.pass('private window was focused');
      return window;
    }).
    then(testPanel).
    then(close).
    then(() => focus(initialWindow)).
    then(testPanel).
    then(done).
    then(null, assert.fail);
};


function makeEmptyPrivateBrowserWindow(options) {
  options = options || {};
  return open(BROWSER, {
    features: {
      chrome: true,
      toolbar: true,
      private: true
    }
  });
}