diff options
author | nduca@chromium.org <nduca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-23 05:49:24 +0000 |
---|---|---|
committer | nduca@chromium.org <nduca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-23 05:49:24 +0000 |
commit | 6df3267e893f7ada7c45d6dcc07fc22760dab654 (patch) | |
tree | 57901492e32f40a0f1d07508f138176daf5270a6 /chrome/test/data/scroll | |
parent | 5146e0b8a8d87c91d25c7cdbe39f51a8b6893a32 (diff) | |
download | chromium_src-6df3267e893f7ada7c45d6dcc07fc22760dab654.zip chromium_src-6df3267e893f7ada7c45d6dcc07fc22760dab654.tar.gz chromium_src-6df3267e893f7ada7c45d6dcc07fc22760dab654.tar.bz2 |
Make scroll.js aware of beginSmoothScroll
Review URL: https://chromiumcodereview.appspot.com/10829448
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152954 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/data/scroll')
-rw-r--r-- | chrome/test/data/scroll/scroll.js | 159 |
1 files changed, 112 insertions, 47 deletions
diff --git a/chrome/test/data/scroll/scroll.js b/chrome/test/data/scroll/scroll.js index 3815344..d974ca4 100644 --- a/chrome/test/data/scroll/scroll.js +++ b/chrome/test/data/scroll/scroll.js @@ -34,33 +34,92 @@ }; })().bind(window); - function GpuBenchmarkingScrollStats() { + /** + * Scrolls a given element down a certain amount to emulate user scrolling. + * Uses smooth scrolling capabilities provided by the platform, if available. + * @constructor + */ + function SmoothScrollDownGesture(opt_element, opt_isGmailTest) { + this.element_ = opt_element || document.body; + this.isGmailTest_ = opt_isGmailTest; + }; + + SmoothScrollDownGesture.prototype.start = function(callback) { + this.callback_ = callback; + if (chrome && + chrome.gpuBenchmarking && + chrome.gpuBenchmarking.beginSmoothScrollDown) { + chrome.gpuBenchmarking.beginSmoothScrollDown(true, function() { + callback(); + }); + return; + } + + if (this.isGmailTest_) { + this.element_.scrollByLines(1); + requestAnimationFrame(callback); + return; + } + + var SCROLL_DELTA = 100; + this.element_.scrollTop += SCROLL_DELTA; + requestAnimationFrame(callback); + } + + /** + * Tracks rendering performance using the gpuBenchmarking.renderingStats API. + * @constructor + */ + function GpuBenchmarkingRenderingStats() { + } + + GpuBenchmarkingRenderingStats.prototype.start = function() { this.initialStats_ = this.getRenderingStats_(); } + GpuBenchmarkingRenderingStats.prototype.stop = function() { + this.finalStats_ = this.getRenderingStats_(); + } + + GpuBenchmarkingRenderingStats.prototype.getResult = function() { + if (!this.initialStats_) + throw new Error("Start not called."); - GpuBenchmarkingScrollStats.prototype.getResult = function() { - var stats = this.getRenderingStats_(); + if (!this.finalStats_) + throw new Error("Stop was not called."); + + var stats = this.finalStats_; for (var key in stats) stats[key] -= this.initialStats_[key]; return stats; }; - GpuBenchmarkingScrollStats.prototype.getRenderingStats_ = function() { + GpuBenchmarkingRenderingStats.prototype.getRenderingStats_ = function() { var stats = chrome.gpuBenchmarking.renderingStats(); stats.totalTimeInSeconds = getTimeMs() / 1000; return stats; }; - function RafScrollStats(timestamp) { - this.frameTimes_ = [timestamp]; + /** + * Tracks rendering performance using requestAnimationFrame. + * @constructor + */ + function RafRenderingStats() { + this.recording_ = false; + this.frameTimes_ = []; + } + + RafRenderingStats.prototype.start = function() { + if (this.recording_) + throw new Error("Already started."); this.recording_ = true; - requestAnimationFrame(this.processStep_.bind(this)); + requestAnimationFrame(this.recordFrameTime_.bind(this)); } - RafScrollStats.prototype.getResult = function() { + RafRenderingStats.prototype.stop = function() { this.recording_ = false; + } - // Fill in the result object. + RafRenderingStats.prototype.getResult = function() { var result = {}; result.numAnimationFrames = this.frameTimes_.length - 1; result.droppedFrameCount = this.getDroppedFrameCount_(this.frameTimes_); @@ -69,15 +128,15 @@ return result; }; - RafScrollStats.prototype.processStep_ = function(timestamp) { + RafRenderingStats.prototype.recordFrameTime_ = function(timestamp) { if (!this.recording_) return; this.frameTimes_.push(timestamp); - requestAnimationFrame(this.processStep_.bind(this)); + requestAnimationFrame(this.recordFrameTime_.bind(this)); }; - RafScrollStats.prototype.getDroppedFrameCount_ = function(frameTimes) { + RafRenderingStats.prototype.getDroppedFrameCount_ = function(frameTimes) { var droppedFrameCount = 0; for (var i = 1; i < frameTimes.length; i++) { var frameTime = frameTimes[i] - frameTimes[i-1]; @@ -87,16 +146,26 @@ return droppedFrameCount; }; - // In this class, a "step" is when the page is being scrolled. - // i.e. startScroll -> startStep -> scroll -> processStep -> - // -> startStep -> scroll -> processStep -> endScroll - function ScrollTest(callback, opt_isGmailTest) { + // This class scrolls a page from the top to the bottom a given number of + // times. + // + // Each full transit of the page is called a "pass." + // + // The page is scrolled down by a set of scroll gestures. These gestures + // correspond to a reading gesture on that platform. + // + // i.e. for TOTAL_ITERATIONS_ = 2, we do + // start_ -> startPass_ -> ...scrolling... -> onGestureComplete_ -> + // -> startPass_ -> .. scrolling... -> onGestureComplete_ -> callback_ + // + // TODO(nduca): This test starts in its constructor. That is strange. We + // should change it to start explicitly. + function ScrollTest(opt_callback, opt_isGmailTest) { var self = this; this.TOTAL_ITERATIONS_ = 2; - this.SCROLL_DELTA_ = 100; - this.callback_ = callback; + this.callback_ = opt_callback; this.isGmailTest_ = opt_isGmailTest; this.iteration_ = 0; @@ -114,58 +183,47 @@ } } - ScrollTest.prototype.scroll_ = function(x, y) { - if (this.isGmailTest_) - this.element_.scrollByLines(1); - else - window.scrollBy(x, y); - }; - ScrollTest.prototype.start_ = function(opt_element) { // Assign this.element_ here instead of constructor, because the constructor // ensures this method will be called after the document is loaded. this.element_ = opt_element || document.body; - requestAnimationFrame(this.startScroll_.bind(this)); + requestAnimationFrame(this.startPass_.bind(this)); }; - ScrollTest.prototype.startScroll_ = function(timestamp) { + ScrollTest.prototype.startPass_ = function() { this.element_.scrollTop = 0; if (window.chrome && chrome.gpuBenchmarking) - this.scrollStats_ = new GpuBenchmarkingScrollStats(); + this.renderingStats_ = new GpuBenchmarkingRenderingStats(); else - this.scrollStats_ = new RafScrollStats(timestamp); - this.startStep_(); - }; + this.renderingStats_ = new RafRenderingStats(); + this.renderingStats_.start(); - ScrollTest.prototype.startStep_ = function() { - this.scroll_(0, this.SCROLL_DELTA_); - requestAnimationFrame(this.processStep_.bind(this)); + this.gesture_ = new SmoothScrollDownGesture(this.element_, + this.isGmailTest_); + this.gesture_.start(this.onGestureComplete_.bind(this)); }; - ScrollTest.prototype.endScroll_ = function() { - this.results_.push(this.scrollStats_.getResult()); - this.iteration_++; - }; - - ScrollTest.prototype.processStep_ = function(timestamp) { + ScrollTest.prototype.onGestureComplete_ = function(timestamp) { // clientHeight is "special" for the body element. + var clientHeight; if (this.element_ == document.body) - var clientHeight = window.innerHeight; + clientHeight = window.innerHeight; else - var clientHeight = this.element_.clientHeight; - var isScrollComplete = + clientHeight = this.element_.clientHeight; + + var isPassComplete = this.element_.scrollTop + clientHeight >= this.element_.scrollHeight; - if (!isScrollComplete) { - this.startStep_(); + if (!isPassComplete) { + this.gesture_.start(this.onGestureComplete_.bind(this)); return; } - this.endScroll_(); + this.endPass_(); var isTestComplete = this.iteration_ >= this.TOTAL_ITERATIONS_; if (!isTestComplete) { - requestAnimationFrame(this.startScroll_.bind(this)); + this.startPass_(); return; } @@ -176,5 +234,12 @@ console.log(this.results_); }; + ScrollTest.prototype.endPass_ = function() { + this.renderingStats_.stop(); + this.results_.push(this.renderingStats_.getResult()); + this.iteration_++; + }; + + window.__ScrollTest = ScrollTest; })(); |