Petar Šegina | 91df3f9 | 2017-08-15 16:20:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | |
| 21 | import android.graphics.PointF; |
| 22 | import android.graphics.RectF; |
| 23 | |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
| 28 | import java.util.Arrays; |
| 29 | import java.util.List; |
| 30 | |
| 31 | @RunWith(JUnit4.class) |
| 32 | public final class SelectionActionModeHelperTest { |
| 33 | |
| 34 | /* |
| 35 | * The test rectangle set is composed of three 1x1 rectangles as illustrated below. |
| 36 | * |
| 37 | * (0, 0) ____________ (100001, 0) |
| 38 | * |█ █| |
| 39 | * |_█________| |
| 40 | * (0, 2) (100001, 2) |
| 41 | */ |
| 42 | private final List<RectF> mRectFList = Arrays.asList( |
| 43 | new RectF(0, 0, 1, 1), |
| 44 | new RectF(100000, 0, 100001, 1), |
| 45 | new RectF(1, 1, 2, 2)); |
| 46 | |
| 47 | @Test |
| 48 | public void testMovePointInsideNearestRectangle_pointIsInsideRectangle() { |
| 49 | testMovePointInsideNearestRectangle( |
| 50 | 0.1f /* pointX */, |
| 51 | 0.1f /* pointY */, |
| 52 | 0.1f /* expectedPointX */, |
| 53 | 0.5f /* expectedPointY */); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testMovePointInsideNearestRectangle_pointIsAboveRectangle() { |
| 58 | testMovePointInsideNearestRectangle( |
| 59 | 0.1f /* pointX */, |
| 60 | -1.0f /* pointY */, |
| 61 | 0.1f /* expectedPointX */, |
| 62 | 0.5f /* expectedPointY */); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testMovePointInsideNearestRectangle_pointIsLeftOfRectangle() { |
| 67 | testMovePointInsideNearestRectangle( |
| 68 | -1.0f /* pointX */, |
| 69 | 0.4f /* pointY */, |
| 70 | 0.0f /* expectedPointX */, |
| 71 | 0.5f /* expectedPointY */); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testMovePointInsideNearestRectangle_pointIsRightOfRectangle() { |
| 76 | testMovePointInsideNearestRectangle( |
| 77 | 1.1f /* pointX */, |
| 78 | 0.0f /* pointY */, |
| 79 | 1.0f /* expectedPointX */, |
| 80 | 0.5f /* expectedPointY */); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testMovePointInsideNearestRectangle_pointIsBelowRectangle() { |
| 85 | testMovePointInsideNearestRectangle( |
| 86 | 0.1f /* pointX */, |
| 87 | 1.1f /* pointY */, |
| 88 | 0.1f /* expectedPointX */, |
| 89 | 0.5f /* expectedPointY */); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testMovePointInsideNearestRectangle_pointIsToRightOfTheRightmostRectangle() { |
| 94 | testMovePointInsideNearestRectangle( |
| 95 | 200000.0f /* pointX */, |
| 96 | 0.1f /* pointY */, |
| 97 | 100001.0f /* expectedPointX */, |
| 98 | 0.5f /* expectedPointY */); |
| 99 | } |
| 100 | |
| 101 | private void testMovePointInsideNearestRectangle(final float pointX, final float pointY, |
| 102 | final float expectedPointX, |
| 103 | final float expectedPointY) { |
| 104 | final PointF point = new PointF(pointX, pointY); |
| 105 | final PointF adjustedPoint = |
| 106 | SelectionActionModeHelper.movePointInsideNearestRectangle(point, |
| 107 | mRectFList); |
| 108 | |
| 109 | assertEquals(expectedPointX, adjustedPoint.x, 0.0f); |
| 110 | assertEquals(expectedPointY, adjustedPoint.y, 0.0f); |
| 111 | } |
| 112 | |
| 113 | } |