Commit 15416213 authored by Anastasia Helfinstein's avatar Anastasia Helfinstein Committed by Commit Bot

Add rect helper function to check closeness in a11y extensions

AX-Relnotes: n/a.
Bug: None.
Change-Id: I5bb22367303e0a84ce68ad6b4d083ec311d2b36c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2391596
Commit-Queue: Anastasia Helfinstein <anastasi@google.com>
Reviewed-by: default avatarAbigail Klein <abigailbklein@google.com>
Cr-Commit-Position: refs/heads/master@{#808593}
parent 6c16fe9c
...@@ -78,6 +78,28 @@ const RectUtil = { ...@@ -78,6 +78,28 @@ const RectUtil = {
return {x, y}; return {x, y};
}, },
/**
* Checks if the two specified rectangles are within at most a specified
* distance of each other both horizontally and vertically.
* @param {ScreenRect} rect1
* @param {ScreenRect} rect2
* @param {number} tolerance
* @return {boolean}
*/
close: (rect1, rect2, tolerance) => {
const maxLeft = rect1.left > rect2.left ? rect1.left : rect2.left;
const minRight = RectUtil.right(rect1) < RectUtil.right(rect2) ?
RectUtil.right(rect1) :
RectUtil.right(rect2);
const maxTop = rect1.top > rect2.top ? rect1.top : rect2.top;
const minBottom = RectUtil.bottom(rect1) < RectUtil.bottom(rect2) ?
RectUtil.bottom(rect1) :
RectUtil.bottom(rect2);
// Negative values indicate the rectangles overlap in that dimension.
return maxLeft - minRight <= tolerance && maxTop - minBottom <= tolerance;
},
/** /**
* @param {ScreenRect} outer * @param {ScreenRect} outer
* @param {ScreenRect} inner * @param {ScreenRect} inner
......
...@@ -71,6 +71,63 @@ TEST_F('RectUtilUnitTest', 'Adjacent', function() { ...@@ -71,6 +71,63 @@ TEST_F('RectUtilUnitTest', 'Adjacent', function() {
} }
}); });
TEST_F('RectUtilUnitTest', 'Close', function() {
const centerRect = {left: 10, top: 10, width: 10, height: 10};
assertTrue(RectUtil.close(centerRect, centerRect, 0));
// Adjacent to the left of centerRect.
const leftRect = {left: 5, top: 10, width: 5, height: 5};
assertTrue(RectUtil.close(centerRect, leftRect, 5));
assertTrue(RectUtil.close(centerRect, leftRect, 0));
assertTrue(RectUtil.close(leftRect, centerRect, 0));
// 2dp away to the right of centerRect.
const rightRect = {left: 22, top: 17, width: 5, height: 5};
assertTrue(RectUtil.close(centerRect, rightRect, 5));
assertTrue(RectUtil.close(rightRect, centerRect, 5));
assertFalse(RectUtil.close(centerRect, rightRect, 0));
assertFalse(RectUtil.close(rightRect, centerRect, 0));
// 5dp above centerRect.
const topRect = {left: 12, top: 0, width: 5, height: 5};
assertTrue(RectUtil.close(centerRect, topRect, 5));
assertTrue(RectUtil.close(topRect, centerRect, 5));
assertFalse(RectUtil.close(centerRect, topRect, 4));
assertFalse(RectUtil.close(topRect, centerRect, 4));
// Adjacent below centerRect.
const bottomRect = {left: 15, top: 20, width: 5, height: 5};
assertTrue(RectUtil.close(centerRect, bottomRect, 0));
assertTrue(RectUtil.close(bottomRect, centerRect, 0));
// Touching at the top left corner of centerRect.
const topLeftRect = {left: 5, top: 5, width: 5, height: 5};
assertTrue(RectUtil.close(centerRect, topLeftRect, 0));
assertTrue(RectUtil.close(topLeftRect, centerRect, 0));
// 1dp off in each dimension from the bottom right corner of centerRect.
const bottomRightRect = {left: 21, top: 21, width: 5, height: 5};
assertTrue(RectUtil.close(centerRect, bottomRightRect, 1));
assertTrue(RectUtil.close(bottomRightRect, centerRect, 1));
assertFalse(RectUtil.close(centerRect, bottomRightRect, 0));
assertFalse(RectUtil.close(bottomRightRect, centerRect, 0));
// Overlapping horizontally, but far vertically.
const verticallyFarRect = {left: 15, top: 100, width: 5, height: 5};
assertFalse(RectUtil.close(centerRect, verticallyFarRect, 5));
assertFalse(RectUtil.close(verticallyFarRect, centerRect, 5));
// Overlapping vertically, but far horizontally.
const horizontallyFarRect = {left: 100, top: 15, width: 5, height: 5};
assertFalse(RectUtil.close(centerRect, horizontallyFarRect, 5));
assertFalse(RectUtil.close(horizontallyFarRect, centerRect, 5));
// Far away in both dimensions from centerRect.
const farRect = {left: 100, top: 100, width: 1, height: 1};
assertFalse(RectUtil.close(centerRect, farRect, 20));
assertFalse(RectUtil.close(farRect, centerRect, 20));
});
TEST_F('RectUtilUnitTest', 'Equals', function() { TEST_F('RectUtilUnitTest', 'Equals', function() {
const rect1 = {left: 0, top: 0, width: 10, height: 10}; const rect1 = {left: 0, top: 0, width: 10, height: 10};
const rect2 = {left: 0, top: 0, width: 10, height: 10}; const rect2 = {left: 0, top: 0, width: 10, height: 10};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment