Skip to content

Commit

Permalink
Working around limitations in subpixel precision event handling.
Browse files Browse the repository at this point in the history
The bug/limitation where browser supports subpixel precision for elements but not for dispatched events was found in both modern Chrome (http://crbug.com/396380) and Firefox (?) browsers.
(IE doesn't seem to be affected: before and including IE9 - no subpixel for elements and events; from ie10 - subpixel is supported for both elements and events).

This test was lucky so far (mostly?) and didn't hit this issue, until Chrome 37 that enabled subpixel text scaling by default.

This change makes *sure* elements have subpixel coordinates (if only browser supports'em) and then it makes sure this test doesn't fail because of that (while still testing selenium atoms).

While there, I moved asserts from event handlers into the normal test flow so jsunit can properly attribute assertion failures to specific test methods.
  • Loading branch information
sevaseva committed Sep 4, 2014
1 parent 09513a8 commit 03feed0
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions javascript/selenium-atoms/test/event_firing_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,32 @@
<script type="text/javascript">
var fired;

// These should work, they just dosn't.
// These should work, they just don't.
var CLICKING_WITH_COORDINATES_BROKEN = goog.userAgent.product.SAFARI ||
goog.userAgent.product.ANDROID ||
(goog.userAgent.product.OPERA &&
bot.userAgent.isEngineVersion(12));

// Some browsers support subpixel element locations but not (yet) subpixel events:
// Chrome: http://crbug.com/396380
// Firefox: (bugreport?).
// In such browsers, when events with subpixel coordinates are dispatched,
// their coordinates lose the fractional part.
// Note, IE behavior isn't problematic as far as this test is concerned:
// in IE9 and below both elements and events are pixel-precise while IE10, IE11
// (and, possbly, later) support both subpixel elements and events.
var SUBPIXEL_PRECISION_SUPPORTED = !CLICKING_WITH_COORDINATES_BROKEN &&
!goog.userAgent.product.CHROME && !goog.userAgent.product.FIREFOX;

function expectedCoordinate(num) {
if (SUBPIXEL_PRECISION_SUPPORTED) {
return num;
} else {
// When browser doesn't support subpixel (for event coords) it truncates to integers.
return Math.floor(num);
}
}

function setUp() {
fired = false;
}
Expand Down Expand Up @@ -65,16 +85,19 @@
var offsetY = 7;

var fired = false;
var eventClientX, eventClientY;
goog.events.listenOnce(mouseTarget, goog.events.EventType.MOUSEUP, function(e) {
fired = true;
assertEquals(clickLocation.left + offsetX, e.clientX);
assertEquals(clickLocation.top + offsetY, e.clientY);
eventClientX = e.clientX;
eventClientY = e.clientY;
});

core.events.fireAt(
'id=location', 'mouseup', offsetX + ',' + offsetY);

assertTrue(fired);
assertEquals(expectedCoordinate(clickLocation.left + offsetX), eventClientX);
assertEquals(expectedCoordinate(clickLocation.top + offsetY), eventClientY);
}

function testFiringEventsAtAParticularLocationWillDefaultToTopLeft() {
Expand All @@ -85,15 +108,18 @@
var clickLocation = goog.style.getBounds(mouseTarget);

var fired = false;
var eventClientX, eventClientY;
goog.events.listenOnce(mouseTarget, goog.events.EventType.MOUSEUP, function(e) {
fired = true;
assertEquals(clickLocation.left, e.clientX);
assertEquals(clickLocation.top, e.clientY);
eventClientX = e.clientX;
eventClientY = e.clientY;
});

core.events.fireAt('id=location', goog.events.EventType.MOUSEUP);

assertTrue(fired);
assertEquals(expectedCoordinate(clickLocation.left), eventClientX);
assertEquals(expectedCoordinate(clickLocation.top), eventClientY);
}

function testCanOverrideTheValueOfAnInputElement() {
Expand Down Expand Up @@ -123,6 +149,9 @@
<p id="target">This is the target</p>

<form action="#" action="get">
<!-- Padding insures input's coordinates fractional if only browser supports subpixel. -->
<div style="height: 20.37px;">subpixel padding top</div>
<div style="display: inline-block; width: 200.97px;">subpixel padding left</div>
<label for="location">Click me:</label><input id="location" /><br/>
</form>
</body>
Expand Down

0 comments on commit 03feed0

Please sign in to comment.