|
| 1 | +<!doctype html> |
| 2 | +<!-- |
| 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 |
| 9 | +--> |
| 10 | +<html> |
| 11 | +<head> |
| 12 | + <title>core-ajax</title> |
| 13 | + |
| 14 | + <script src="../../webcomponentsjs/webcomponents.js"></script> |
| 15 | + <script src="../../web-component-tester/browser.js"></script> |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + <link rel="import" href="../core-ajax.html"> |
| 20 | + |
| 21 | +</head> |
| 22 | +<body> |
| 23 | + |
| 24 | + <core-ajax |
| 25 | + handleAs="json" |
| 26 | + auto></core-ajax> |
| 27 | + |
| 28 | +<!-- |
| 29 | + Test consistency of core-ajax's loading properties. |
| 30 | + --> |
| 31 | + <script> |
| 32 | + test('progress', function(done) { |
| 33 | + var ajax = document.querySelector("core-ajax"); |
| 34 | + var xhr = sinon.useFakeXMLHttpRequest(); |
| 35 | + var headers = { |
| 36 | + "Content-Type": "text/json" |
| 37 | + }; |
| 38 | + var body = '{"content": "plentiful"}' |
| 39 | + var requests = this.requests = []; |
| 40 | + xhr.onCreate = function (xhr) { |
| 41 | + requests.push(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; |
| 45 | + }; |
| 46 | + var animationFrameFlush = function(callback) { |
| 47 | + requestAnimationFrame(function(){ |
| 48 | + flush(callback); |
| 49 | + }) |
| 50 | + }; |
| 51 | + var progressEvent = function(lengthComputable, loaded, total) { |
| 52 | + var progress = new ProgressEvent('progress', { |
| 53 | + lengthComputable: lengthComputable, |
| 54 | + loaded: loaded, |
| 55 | + total: total |
| 56 | + }); |
| 57 | + return progress; |
| 58 | + } |
| 59 | + |
| 60 | + // Fake a file download by sending multiple progress events. |
| 61 | + async.series([ |
| 62 | + function(cb) { |
| 63 | + ajax.url="http://example.org/downloadLargeFile" |
| 64 | + cb(); |
| 65 | + }, |
| 66 | + flush, |
| 67 | + animationFrameFlush, |
| 68 | + function(cb) { |
| 69 | + requests[0].dispatchEvent(progressEvent(true, 10, 100)); |
| 70 | + cb(); |
| 71 | + }, |
| 72 | + flush, |
| 73 | + animationFrameFlush, |
| 74 | + function(cb) { |
| 75 | + assert(ajax.loading === true, |
| 76 | + "Request partially complete, but loading property was false."); |
| 77 | + var progress = ajax.progress; |
| 78 | + assert(progress.lengthComputable, "Progress should be computable"); |
| 79 | + assert(progress.loaded == 10, "Expected 10 bytes loaded, got " + progress.loaded); |
| 80 | + assert(progress.total == 100, "Expected 100 bytes total, got " + progress.total); |
| 81 | + cb(); |
| 82 | + }, |
| 83 | + animationFrameFlush, |
| 84 | + function(cb) { |
| 85 | + requests[0].dispatchEvent(progressEvent(true, 100, 100)); |
| 86 | + cb(); |
| 87 | + }, |
| 88 | + animationFrameFlush, |
| 89 | + function(cb) { |
| 90 | + assert(ajax.loading === true, |
| 91 | + "Request partially complete, but loading property was false."); |
| 92 | + var progress = ajax.progress; |
| 93 | + assert(progress.lengthComputable, "Progress should be computable"); |
| 94 | + assert(progress.loaded == 100, "Expected 10 bytes loaded, got " + progress.loaded); |
| 95 | + assert(progress.total == 100, "Expected 100 bytes total, got " + progress.total); |
| 96 | + cb(); |
| 97 | + }, |
| 98 | + function(cb) { |
| 99 | + requests[0].respond(200, headers, body); |
| 100 | + cb(); |
| 101 | + }, |
| 102 | + animationFrameFlush, |
| 103 | + function(cb) { |
| 104 | + assert(ajax.loading === false, |
| 105 | + "Request complete, but loading property was true."); |
| 106 | + assert(ajax.response.content === "plentiful", "response not parsed"); |
| 107 | + cb(); |
| 108 | + } |
| 109 | + ], done); |
| 110 | + }); |
| 111 | +</script> |
| 112 | +</body> |
| 113 | +</html> |
0 commit comments