15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)'use strict';
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)(function() {
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  function TapGestureOptions(opt_options) {
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (opt_options) {
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.element_ = opt_options.element;
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.left_position_percentage_ = opt_options.left_position_percentage;
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.top_position_percentage_ = opt_options.top_position_percentage;
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.duration_ms_ = opt_options.duration_ms;
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.gesture_source_type_ = opt_options.gesture_source_type;
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    } else {
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.element_ = document.body;
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.left_position_percentage_ = 0.5;
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.top_position_percentage_ = 0.5;
20effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      this.duration_ms_ = 50;
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.gesture_source_type_ = chrome.gpuBenchmarking.DEFAULT_INPUT;
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  function supportedByBrowser() {
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return !!(window.chrome &&
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              chrome.gpuBenchmarking &&
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)              chrome.gpuBenchmarking.tap);
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  function TapAction(opt_callback) {
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    var self = this;
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    this.beginMeasuringHook = function() {};
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    this.endMeasuringHook = function() {};
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    this.callback_ = opt_callback;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TapAction.prototype.start = function(opt_options) {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    this.options_ = new TapGestureOptions(opt_options);
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Assign this.element_ here instead of constructor, because the constructor
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // ensures this method will be called after the document is loaded.
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    this.element_ = this.options_.element_;
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    this.beginMeasuringHook();
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    var rect = __GestureCommon_GetBoundingVisibleRect(this.options_.element_);
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    var position_left =
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        rect.left + rect.width * this.options_.left_position_percentage_;
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    var position_top =
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        rect.top + rect.height * this.options_.top_position_percentage_;
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (position_left < 0 || position_left >= window.innerWidth ||
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        position_top < 0 || position_top >= window.innerHeight) {
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      throw new Error('Tap position is off-screen');
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    chrome.gpuBenchmarking.tap(position_left, position_top,
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               this.onGestureComplete_.bind(this),
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               this.options_.duration_ms_,
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                               this.options_.gesture_source_type_);
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TapAction.prototype.onGestureComplete_ = function() {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    this.endMeasuringHook();
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // We're done.
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (this.callback_)
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this.callback_();
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  window.__TapAction = TapAction;
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  window.__TapAction_SupportedByBrowser = supportedByBrowser;
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)})();
74