summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_open.js
blob: ec55f0101e4afec4937f30f4cf79ec49d1801e36 (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
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// only finish() when correct number of tests are done
const expected = 4;
var count = 0;
var lastUniqueName = null;

function done()
{
  if (++count == expected) {
    finish();
  }
}

function test()
{
  waitForExplicitFinish();
  testOpen();
  testOpenWithState();
  testOpenInvalidState();
  testOpenTestFile();
}

function testUniqueName(name)
{
  ok(name, "Scratchpad has a uniqueName");

  if (lastUniqueName === null) {
    lastUniqueName = name;
    return;
  }

  ok(name !== lastUniqueName,
      "Unique name for this instance differs from the last one.");
}

function testOpen()
{
  openScratchpad(function (win) {
    is(win.Scratchpad.filename, undefined, "Default filename is undefined");
    isnot(win.Scratchpad.getText(), null, "Default text should not be null");
    is(win.Scratchpad.executionContext, win.SCRATCHPAD_CONTEXT_CONTENT,
      "Default execution context is content");
    testUniqueName(win.Scratchpad.uniqueName);

    win.close();
    done();
  }, {noFocus: true});
}

function testOpenWithState()
{
  let state = {
    filename: "testfile",
    executionContext: 2,
    text: "test text"
  };

  openScratchpad(function (win) {
    is(win.Scratchpad.filename, state.filename, "Filename loaded from state");
    is(win.Scratchpad.executionContext, state.executionContext, "Execution context loaded from state");
    is(win.Scratchpad.getText(), state.text, "Content loaded from state");
    testUniqueName(win.Scratchpad.uniqueName);

    win.close();
    done();
  }, {state: state, noFocus: true});
}

function testOpenInvalidState()
{
  let win = openScratchpad(null, {state: 7});
  ok(!win, "no scratchpad opened if state is not an object");
  done();
}

function testOpenTestFile()
{
  let win = openScratchpad(function (win) {
    ok(win, "scratchpad opened for file open");
    try {
      win.Scratchpad.importFromFile(
        "http://example.com/browser/devtools/client/scratchpad/test/NS_ERROR_ILLEGAL_INPUT.txt",
        "silent",
        function (aStatus, content) {
          let nb = win.document.querySelector("#scratchpad-notificationbox");
          is(nb.querySelectorAll("notification").length, 1, "There is just one notification");
          let cn = nb.currentNotification;
          is(cn.priority, nb.PRIORITY_WARNING_HIGH, "notification priority is correct");
          is(cn.value, "file-import-convert-failed", "notification value is corrent");
          is(cn.type, "warning", "notification type is correct");
          done();
        });
      ok(true, "importFromFile does not cause exception");
    } catch (exception) {
      ok(false, "importFromFile causes exception " + DevToolsUtils.safeErrorString(exception));
    }
  }, {noFocus: true});
}