This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add progress and loading attributes; includes docs, demo, and test.
- Loading branch information
Showing
4 changed files
with
217 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="../platform/platform.js" debug></script> | ||
<meta charset="utf-8"> | ||
<title>Race condition</title> | ||
</head> | ||
<body> | ||
<link rel="import" href="core-ajax.html"> | ||
<link rel="import" href="../paper-progress/paper-progress.html"> | ||
<polymer-element name="progress-test"> | ||
<template> | ||
<core-ajax | ||
id='ajax' auto | ||
url="http://httpbin.org/drip" | ||
params="{{ {numbytes: numbytes, duration:5} }}" | ||
response="{{response}}" | ||
progress="{{progress}}" | ||
loading="{{loading}}" | ||
></core-ajax> | ||
|
||
<!-- | ||
Ordinarily you'd gate on progress.lengthComputable, but we know the | ||
length in this case (and httpbin sadly doesn't return a | ||
Content-Length header for this requesthere). | ||
--> | ||
<div> | ||
<button on-click='{{restart}}'>Restart</button> | ||
<template if='{{loading}}'> | ||
Loading... | ||
</template> | ||
<template if='{{!loading}}'> | ||
Loaded! | ||
</template> | ||
</div> | ||
<template if='{{loading && progress.loaded}}'> | ||
<paper-progress | ||
value="{{progress.loaded}}" | ||
min="0" | ||
max="{{numbytes}}"> | ||
</paper-progress><br> | ||
</template> | ||
</template> | ||
<script> | ||
Polymer('progress-test', { | ||
loading: true, | ||
i: 0, | ||
numbytes: 1000, | ||
pretty: function(i) { | ||
return JSON.stringify(i, null, 2); | ||
}, | ||
restart: function() { | ||
this.$.ajax.abort(); | ||
this.$.ajax.go(); | ||
} | ||
}); | ||
</script> | ||
|
||
</polymer-element> | ||
|
||
<progress-test></progress-test> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<!doctype html> | ||
<!-- | ||
Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<html> | ||
<head> | ||
<title>core-ajax-response-and-error</title> | ||
|
||
<script src="../../../platform/platform.js"></script> | ||
<script src="../../../polymer-test-tools/chai/chai.js"></script> | ||
<script src="../../../polymer-test-tools/htmltest.js"></script> | ||
|
||
<link rel="import" href="../../core-ajax.html"> | ||
|
||
</head> | ||
<body> | ||
|
||
<polymer-element name="race-condition"> | ||
<template> | ||
<!-- TODO: change this from /headers to /drip if we can get httpbin to | ||
send a Content-Length with their /drip response. --> | ||
<core-ajax | ||
auto url="http://httpbin.org/headers" | ||
params="{{ {numbytes: numbytes, duration:1} }}" | ||
loading="{{loading}}" progress="{{progress}}" | ||
on-core-response='{{responseReceived}}'></core-ajax> | ||
</template> | ||
<script> | ||
Polymer({ | ||
numbytes: 1000, | ||
loading: null, | ||
loadingWasTrue: false, | ||
loadingFinished: false, | ||
previousLoaded: null, | ||
domReady: function() { | ||
// This will fail the test if it neither passes nor fails in time. | ||
this.finalTimeout = setTimeout(function() { | ||
chai.assert.fail('', '', 'Test timed out.'); | ||
}, 3000); | ||
}, | ||
loadingChanged: function() { | ||
this.assertConsistent(); | ||
}, | ||
progressChanged: function() { | ||
this.assertConsistent(); | ||
}, | ||
responseReceived: function() { | ||
// Shortly after the response is received we should settle into | ||
// the final consistent state for the element. | ||
setTimeout(function() { | ||
chai.assert.equal(this.loading, false); | ||
chai.assert.equal(this.loadingWasTrue, true); | ||
if (this.progress && this.progress.lengthComputable) { | ||
chai.assert.equal(this.progress.total, this.progress.loaded); | ||
} | ||
this.assertConsistent(); | ||
clearTimeout(this.finalTimeout); | ||
done(); | ||
}.bind(this), 100); | ||
}, | ||
assertConsistent: function() { | ||
debugger; | ||
if (this.progress && this.progress.lengthComputable) { | ||
chai.assert.isNumber(this.progress.total); | ||
chai.assert.isNumber(this.progress.loaded); | ||
chai.assert(this.progress.loaded >= 0, | ||
'Bytes loaded should not be negative.'); | ||
chai.assert(this.progress.loaded <= this.progress.total, | ||
'Bytes loaded should be less than or equal the total.'); | ||
if (this.previousLoaded !== null) { | ||
chai.assert(this.progress.loaded >= this.previousLoaded, | ||
'Bytes loaded should increase monotonically.'); | ||
} | ||
this.previousLoaded = this.progress.loaded; | ||
console.log(this.progress.loaded + ' of ' + this.progress.total); | ||
} | ||
if (this.loading === true) { | ||
this.loadingWasTrue = true; | ||
} | ||
if (this.loadingWasTrue && this.loading === false) { | ||
this.loadingFinished = true; | ||
} | ||
if (this.loadingFinished) { | ||
chai.assert.equal( | ||
this.loading, false, | ||
'Once loaded, the request should stay that way'); | ||
} | ||
} | ||
}); | ||
</script> | ||
</polymer-element> | ||
|
||
<race-condition></race-condition> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
htmlSuite('core-ajax', function() { | ||
htmlTest('html/core-ajax.html'); | ||
htmlTest('html/core-ajax-response-and-error.html'); | ||
htmlTest('html/core-ajax-progress-and-loading.html'); | ||
}); |