summaryrefslogtreecommitdiffstats
path: root/dom/base/test/unit/test_cancelPrefetch.js
blob: 6663d60fbbb0a9aed168c381ccbd075704a28897 (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
var prefetch = Cc["@mozilla.org/prefetch-service;1"].getService(
  Ci.nsIPrefetchService
);

var ReferrerInfo = Components.Constructor(
  "@mozilla.org/referrer-info;1",
  "nsIReferrerInfo",
  "init"
);

var ios = Services.io;
var prefs = Services.prefs;

var parser = new DOMParser();

var doc;

var docbody =
  '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>' +
  '<link id="node1"/><link id="node2"/>' +
  "</body></html>";

var node1;
var node2;

function run_test() {
  prefs.setBoolPref("network.prefetch-next", true);

  doc = parser.parseFromString(docbody, "text/html");

  node1 = doc.getElementById("node1");
  node2 = doc.getElementById("node2");

  run_next_test();
}

add_test(function test_cancel1() {
  var uri = ios.newURI("http://localhost/1");
  var referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri);

  prefetch.prefetchURI(uri, referrerInfo, node1, true);

  Assert.ok(prefetch.hasMoreElements(), "There is a request in the queue");

  // Trying to prefetch again the same uri with the same node will fail.
  var didFail = 0;

  try {
    prefetch.prefetchURI(uri, referrerInfo, node1, true);
  } catch (e) {
    didFail = 1;
  }

  Assert.ok(
    didFail == 1,
    "Prefetching the same request with the same node fails."
  );

  Assert.ok(prefetch.hasMoreElements(), "There is still request in the queue");

  prefetch.cancelPrefetchPreloadURI(uri, node1);

  Assert.ok(!prefetch.hasMoreElements(), "There is no request in the queue");
  run_next_test();
});

add_test(function test_cancel2() {
  // Prefetch a uri with 2 different nodes. There should be 2 request
  // in the queue and canceling one will not cancel the other.

  var uri = ios.newURI("http://localhost/1");
  var referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri);

  prefetch.prefetchURI(uri, referrerInfo, node1, true);
  prefetch.prefetchURI(uri, referrerInfo, node2, true);

  Assert.ok(prefetch.hasMoreElements(), "There are requests in the queue");

  prefetch.cancelPrefetchPreloadURI(uri, node1);

  Assert.ok(
    prefetch.hasMoreElements(),
    "There is still one more request in the queue"
  );

  prefetch.cancelPrefetchPreloadURI(uri, node2);

  Assert.ok(!prefetch.hasMoreElements(), "There is no request in the queue");
  run_next_test();
});

add_test(function test_cancel3() {
  // Request a prefetch of a uri. Trying to cancel a prefetch for the same uri
  // with a different node will fail.
  var uri = ios.newURI("http://localhost/1");
  var referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri);

  prefetch.prefetchURI(uri, referrerInfo, node1, true);

  Assert.ok(prefetch.hasMoreElements(), "There is a request in the queue");

  var didFail = 0;

  try {
    prefetch.cancelPrefetchPreloadURI(uri, node2, true);
  } catch (e) {
    didFail = 1;
  }
  Assert.ok(didFail == 1, "Canceling the request failed");

  Assert.ok(
    prefetch.hasMoreElements(),
    "There is still a request in the queue"
  );

  prefetch.cancelPrefetchPreloadURI(uri, node1);
  Assert.ok(!prefetch.hasMoreElements(), "There is no request in the queue");
  run_next_test();
});

add_test(function test_cancel4() {
  // Request a prefetch of a uri. Trying to cancel a prefetch for a different uri
  // with the same node will fail.
  var uri1 = ios.newURI("http://localhost/1");
  var uri2 = ios.newURI("http://localhost/2");
  var referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri1);

  prefetch.prefetchURI(uri1, referrerInfo, node1, true);

  Assert.ok(prefetch.hasMoreElements(), "There is a request in the queue");

  var didFail = 0;

  try {
    prefetch.cancelPrefetchPreloadURI(uri2, node1);
  } catch (e) {
    didFail = 1;
  }
  Assert.ok(didFail == 1, "Canceling the request failed");

  Assert.ok(
    prefetch.hasMoreElements(),
    "There is still a request in the queue"
  );

  prefetch.cancelPrefetchPreloadURI(uri1, node1);
  Assert.ok(!prefetch.hasMoreElements(), "There is no request in the queue");
  run_next_test();
});