3 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 Code distributed by Google as part of the polymer project is also
8 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
12 <title>core-ajax
</title>
14 <script src=
"../../webcomponentsjs/webcomponents.js"></script>
15 <script src=
"../../web-component-tester/browser.js"></script>
19 <link rel=
"import" href=
"../core-ajax.html">
29 Test consistency of core-ajax's loading properties.
32 test('progress', function(done
) {
33 var ajax
= document
.querySelector("core-ajax");
34 var xhr
= sinon
.useFakeXMLHttpRequest();
36 "Content-Type": "text/json"
38 var body
= '{"content": "plentiful"}'
39 var requests
= this.requests
= [];
40 xhr
.onCreate = function (xhr
) {
42 // Polymer inspects the xhr object for the precense of onprogress to determine
43 // whether to attach an event listener.
44 xhr
['onprogress'] = null;
46 var progressEvent = function(lengthComputable
, loaded
, total
) {
47 var progress
= new ProgressEvent('progress', {
48 lengthComputable
: lengthComputable
,
55 // Fake a file download by sending multiple progress events.
58 ajax
.url
="http://example.org/downloadLargeFile"
64 requests
[0].dispatchEvent(progressEvent(true, 10, 100));
70 assert(ajax
.loading
=== true,
71 "Request partially complete, but loading property was false.");
72 var progress
= ajax
.progress
;
73 assert(progress
.lengthComputable
, "Progress should be computable");
74 assert(progress
.loaded
== 10, "Expected 10 bytes loaded, got " + progress
.loaded
);
75 assert(progress
.total
== 100, "Expected 100 bytes total, got " + progress
.total
);
80 requests
[0].dispatchEvent(progressEvent(true, 100, 100));
85 assert(ajax
.loading
=== true,
86 "Request partially complete, but loading property was false.");
87 var progress
= ajax
.progress
;
88 assert(progress
.lengthComputable
, "Progress should be computable");
89 assert(progress
.loaded
== 100, "Expected 10 bytes loaded, got " + progress
.loaded
);
90 assert(progress
.total
== 100, "Expected 100 bytes total, got " + progress
.total
);
94 requests
[0].respond(200, headers
, body
);
99 assert(ajax
.loading
=== false,
100 "Request complete, but loading property was true.");
101 assert(ajax
.response
.content
=== "plentiful", "response not parsed");