1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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 package com.android.ide.eclipse.adt.internal.editors.layout.gle2; 17 18 import org.eclipse.swt.graphics.Rectangle; 19 20 import java.util.Arrays; 21 import java.util.Collection; 22 import java.util.Collections; 23 import java.util.List; 24 import java.util.Random; 25 26 import junit.framework.TestCase; 27 28 public class IncludeOverlayTest extends TestCase { 29 testSubtractRectangles()30 public void testSubtractRectangles() throws Exception { 31 checkSubtract(new Rectangle(0, 0, 100, 80), Collections.<Rectangle> emptyList()); 32 33 checkSubtract(new Rectangle(0, 0, 100, 80), Arrays.asList(new Rectangle(50, 50, 20, 20))); 34 35 checkSubtract(new Rectangle(0, 0, 100, 80), Arrays.asList(new Rectangle(50, 50, 20, 20), 36 new Rectangle(90, 90, 10, 10))); 37 38 checkSubtract(new Rectangle(0, 0, 100, 80), Arrays.asList(new Rectangle(50, 50, 20, 20), 39 new Rectangle(90, 90, 10, 10), new Rectangle(0, 0, 10, 10))); 40 41 } 42 checkSubtract(Rectangle rectangle, List<Rectangle> holes)43 private void checkSubtract(Rectangle rectangle, List<Rectangle> holes) { 44 Collection<Rectangle> result = IncludeOverlay.subtractRectangles(rectangle, holes); 45 46 // Do some Monte Carlo testing - pick random coordinates and check that if they 47 // are within one of the holes then they are not in the result list and vice versa 48 Random random = new Random(42L); 49 for (int i = 0; i < 1000; i++) { 50 int x = random.nextInt(rectangle.width); 51 int y = random.nextInt(rectangle.height); 52 53 boolean inHole = false; 54 for (Rectangle hole : holes) { 55 if (hole.contains(x, y)) { 56 inHole = true; 57 } 58 } 59 60 boolean inResult = false; 61 for (Rectangle r : result) { 62 if (r.contains(x, y)) { 63 inResult = true; 64 break; 65 } 66 } 67 68 if (inHole == inResult) { 69 fail("Wrong result at (" + x + "," + y + ") for rectangle=" + rectangle 70 + " and holes=" + holes + " where inHole=inResult=" 71 + inResult); 72 } 73 } 74 } 75 } 76