summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_sources-sorting.js
blob: 2a600893b0479db4d3a0cf42af5b0ee64124ff11 (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests that urls are correctly sorted when added to the sources widget.
 */

const TAB_URL = EXAMPLE_URL + "doc_recursion-stack.html";

var gTab, gPanel, gDebugger;
var gSources, gUtils;

function test() {
  let options = {
    source: TAB_URL,
    line: 1
  };
  initDebugger(TAB_URL, options).then(([aTab,, aPanel]) => {
    gTab = aTab;
    gPanel = aPanel;
    gDebugger = gPanel.panelWin;
    gSources = gDebugger.DebuggerView.Sources;
    gUtils = gDebugger.SourceUtils;

    addSourceAndCheckOrder(1);
    addSourceAndCheckOrder(2);
    addSourceAndCheckOrder(3);
    closeDebuggerAndFinish(gPanel);
  });
}

function addSourceAndCheckOrder(aMethod) {
  gSources.empty();
  gSources.suppressSelectionEvents = true;

  let urls = [
    { href: "ici://some.address.com/random/", leaf: "subrandom/" },
    { href: "ni://another.address.org/random/subrandom/", leaf: "page.html" },
    { href: "san://interesting.address.gro/random/", leaf: "script.js" },
    { href: "si://interesting.address.moc/random/", leaf: "script.js" },
    { href: "si://interesting.address.moc/random/", leaf: "x/script.js" },
    { href: "si://interesting.address.moc/random/", leaf: "x/y/script.js?a=1" },
    { href: "si://interesting.address.moc/random/x/", leaf: "y/script.js?a=1&b=2" },
    { href: "si://interesting.address.moc/random/x/y/", leaf: "script.js?a=1&b=2&c=3" }
  ];

  urls.sort(function (a, b) {
    return Math.random() - 0.5;
  });

  let id = 0;

  switch (aMethod) {
    case 1:
      for (let { href, leaf } of urls) {
        let url = href + leaf;
        let actor = "actor" + id++;
        let label = gUtils.getSourceLabel(url);
        let dummy = document.createElement("label");
        gSources.push([dummy, actor], {
          staged: true,
          attachment: {
            label: label
          }
        });
      }
      gSources.commit({ sorted: true });
      break;

    case 2:
      for (let { href, leaf } of urls) {
        let url = href + leaf;
        let actor = "actor" + id++;
        let label = gUtils.getSourceLabel(url);
        let dummy = document.createElement("label");
        gSources.push([dummy, actor], {
          staged: false,
          attachment: {
            label: label
          }
        });
      }
      break;

    case 3:
      let i = 0;
      for (; i < urls.length / 2; i++) {
        let { href, leaf } = urls[i];
        let url = href + leaf;
        let actor = "actor" + id++;
        let label = gUtils.getSourceLabel(url);
        let dummy = document.createElement("label");
        gSources.push([dummy, actor], {
          staged: true,
          attachment: {
            label: label
          }
        });
      }
      gSources.commit({ sorted: true });

      for (; i < urls.length; i++) {
        let { href, leaf } = urls[i];
        let url = href + leaf;
        let actor = "actor" + id++;
        let label = gUtils.getSourceLabel(url);
        let dummy = document.createElement("label");
        gSources.push([dummy, actor], {
          staged: false,
          attachment: {
            label: label
          }
        });
      }
      break;
  }

  checkSourcesOrder(aMethod);
}

function checkSourcesOrder(aMethod) {
  let attachments = gSources.attachments;

  for (let i = 0; i < attachments.length - 1; i++) {
    let first = attachments[i].label;
    let second = attachments[i + 1].label;
    ok(first < second,
      "Using method " + aMethod + ", " +
      "the sources weren't in the correct order: " + first + " vs. " + second);
  }
}

registerCleanupFunction(function () {
  gTab = null;
  gPanel = null;
  gDebugger = null;
  gSources = null;
  gUtils = null;
});