1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5'use strict';
6
7(function() {
8  function SwipeGestureOptions(opt_options) {
9    if (opt_options) {
10      this.element_ = opt_options.element;
11      this.left_start_percentage_ = opt_options.left_start_percentage;
12      this.top_start_percentage_ = opt_options.top_start_percentage;
13      this.direction_ = opt_options.direction;
14      this.distance_ = opt_options.distance;
15    } else {
16      this.element_ = document.body;
17      this.left_start_percentage_ = 0.5;
18      this.top_start_percentage_ = 0.5;
19      this.direction_ = 'left';
20      this.distance_ = 0;
21    }
22  }
23
24  function supportedByBrowser() {
25    return !!(window.chrome &&
26              chrome.gpuBenchmarking &&
27              chrome.gpuBenchmarking.swipe);
28  }
29
30  // This class swipes a page for a specified distance.
31  function SwipeAction(opt_callback) {
32    var self = this;
33
34    this.beginMeasuringHook = function() {}
35    this.endMeasuringHook = function() {}
36
37    this.callback_ = opt_callback;
38  }
39
40  SwipeAction.prototype.start = function(opt_options) {
41    this.options_ = new SwipeGestureOptions(opt_options);
42    // Assign this.element_ here instead of constructor, because the constructor
43    // ensures this method will be called after the document is loaded.
44    this.element_ = this.options_.element_;
45    requestAnimationFrame(this.startGesture_.bind(this));
46  };
47
48  SwipeAction.prototype.startGesture_ = function() {
49    this.beginMeasuringHook();
50
51    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
52    var start_left =
53        rect.left + rect.width * this.options_.left_start_percentage_;
54    var start_top =
55        rect.top + rect.height * this.options_.top_start_percentage_;
56    chrome.gpuBenchmarking.swipe(this.options_.direction_,
57                                 this.options_.distance_,
58                                 this.onGestureComplete_.bind(this),
59                                 start_left, start_top);
60  };
61
62  SwipeAction.prototype.onGestureComplete_ = function() {
63    this.endMeasuringHook();
64
65    // We're done.
66    if (this.callback_)
67      this.callback_();
68  };
69
70  window.__SwipeAction = SwipeAction;
71  window.__SwipeAction_SupportedByBrowser = supportedByBrowser;
72})();
73