From 914ce118f82dc0a11a6a1b6290b8509f11268f46 Mon Sep 17 00:00:00 2001 From: Viktar Donich Date: Tue, 31 May 2016 16:06:32 -0700 Subject: [PATCH] Make gr-selection-action-box_test cross-platform Change-Id: Ie60fb4d7cffaca76ac40eebbd35b0196cd63e8e9 --- .../gr-selection-action-box.js | 17 +++++++++++------ .../gr-selection-action-box_test.html | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box.js b/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box.js index d464947efa..0a85096f88 100644 --- a/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box.js +++ b/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box.js @@ -42,6 +42,16 @@ ], placeAbove: function(el) { + var rect = this._getTargetBoundingRect(el); + var boxRect = this.getBoundingClientRect(); + var parentRect = this.parentElement.getBoundingClientRect(); + this.style.top = + rect.top - parentRect.top - boxRect.height - 4 + 'px'; + this.style.left = + rect.left - parentRect.left + (rect.width - boxRect.width)/2 + 'px'; + }, + + _getTargetBoundingRect: function(el) { var rect; if (!(el instanceof Element)) { var range = document.createRange(); @@ -51,12 +61,7 @@ } else { rect = el.getBoundingClientRect(); } - var boxRect = this.getBoundingClientRect(); - var parentRect = this.parentElement.getBoundingClientRect(); - this.style.top = - rect.top - parentRect.top - boxRect.height - 4 + 'px'; - this.style.left = - rect.left - parentRect.left + (rect.width - boxRect.width)/2 + 'px'; + return rect; }, _handleKey: function(e) { diff --git a/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box_test.html b/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box_test.html index 3cb3a1708e..65c396f57d 100644 --- a/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box_test.html +++ b/polygerrit-ui/app/elements/diff/gr-selection-action-box/gr-selection-action-box_test.html @@ -84,7 +84,7 @@ limitations under the License. target = container.querySelector('.target'); sinon.stub(container, 'getBoundingClientRect').returns( {top:1, bottom: 2, left: 3, right: 4, width: 50, height: 6}); - sinon.stub(target, 'getBoundingClientRect').returns( + sinon.stub(element, '_getTargetBoundingRect').returns( {top:42, bottom: 20, left: 30, right: 40, width: 100, height: 60}); sinon.stub(element, 'getBoundingClientRect').returns( {width: 10, height: 10}); @@ -93,7 +93,7 @@ limitations under the License. teardown(function() { element.getBoundingClientRect.restore(); container.getBoundingClientRect.restore(); - target.getBoundingClientRect.restore(); + element._getTargetBoundingRect.restore(); }); test('placeAbove for Element argument', function() { @@ -104,7 +104,17 @@ limitations under the License. test('placeAbove for Text Node argument', function() { element.placeAbove(target.firstChild); - assert.equal(element.style.top, '-7px'); + assert.equal(element.style.top, '27px'); + assert.equal(element.style.left, '72px'); + }); + + test('uses document.createRange', function() { + sinon.spy(document, 'createRange'); + element._getTargetBoundingRect.restore(); + sinon.spy(element, '_getTargetBoundingRect'); + element.placeAbove(target.firstChild); + assert.isTrue(document.createRange.called); + document.createRange.restore(); }); }); });