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
9  function TapGestureOptions(opt_options) {
10    if (opt_options) {
11      this.element_ = opt_options.element;
12      this.left_position_percentage_ = opt_options.left_position_percentage;
13      this.top_position_percentage_ = opt_options.top_position_percentage;
14      this.duration_ms_ = opt_options.duration_ms;
15      this.gesture_source_type_ = opt_options.gesture_source_type;
16    } else {
17      this.element_ = document.body;
18      this.left_position_percentage_ = 0.5;
19      this.top_position_percentage_ = 0.5;
20      this.duration_ms_ = 50;
21      this.gesture_source_type_ = chrome.gpuBenchmarking.DEFAULT_INPUT;
22    }
23  }
24
25  function supportedByBrowser() {
26    return !!(window.chrome &&
27              chrome.gpuBenchmarking &&
28              chrome.gpuBenchmarking.tap);
29  }
30
31  function TapAction(opt_callback) {
32    var self = this;
33
34    this.beginMeasuringHook = function() {};
35    this.endMeasuringHook = function() {};
36
37    this.callback_ = opt_callback;
38  }
39
40  TapAction.prototype.start = function(opt_options) {
41    this.options_ = new TapGestureOptions(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
46    this.beginMeasuringHook();
47
48    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
49    var position_left =
50        rect.left + rect.width * this.options_.left_position_percentage_;
51    var position_top =
52        rect.top + rect.height * this.options_.top_position_percentage_;
53    if (position_left < 0 || position_left >= window.innerWidth ||
54        position_top < 0 || position_top >= window.innerHeight) {
55      throw new Error('Tap position is off-screen');
56    }
57    chrome.gpuBenchmarking.tap(position_left, position_top,
58                               this.onGestureComplete_.bind(this),
59                               this.options_.duration_ms_,
60                               this.options_.gesture_source_type_);
61  };
62
63  TapAction.prototype.onGestureComplete_ = function() {
64    this.endMeasuringHook();
65
66    // We're done.
67    if (this.callback_)
68      this.callback_();
69  };
70
71  window.__TapAction = TapAction;
72  window.__TapAction_SupportedByBrowser = supportedByBrowser;
73})();
74