Unit testing for SkTileGrid
Review URL: https://codereview.appspot.com/6827053

git-svn-id: http://skia.googlecode.com/svn/trunk@6331 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/tests.gyp b/gyp/tests.gyp
index 66888dd..18dc558 100644
--- a/gyp/tests.gyp
+++ b/gyp/tests.gyp
@@ -91,6 +91,7 @@
         '../tests/Test.cpp',
         '../tests/Test.h',
         '../tests/TestSize.cpp',
+        '../tests/TileGridTest.cpp',
         '../tests/TLSTest.cpp',
         '../tests/ToUnicode.cpp',
         '../tests/UnicodeTest.cpp',
diff --git a/src/core/SkTileGrid.cpp b/src/core/SkTileGrid.cpp
index 720d4fa..f038716 100644
--- a/src/core/SkTileGrid.cpp
+++ b/src/core/SkTileGrid.cpp
@@ -16,6 +16,7 @@
     fYTileCount = yTileCount;
     fTileCount = fXTileCount * fYTileCount;
     fInsertionCount = 0;
+    fGridBounds = SkIRect::MakeXYWH(0, 0, fTileWidth * fXTileCount, fTileHeight * fYTileCount);
 
     fTileData = SkNEW_ARRAY(SkTDArray<void *>, fTileCount);
 }
@@ -33,6 +34,10 @@
     SkIRect dilatedBounds = bounds;
     dilatedBounds.outset(1,1); // Consideration for filtering and AA
 
+    if (!SkIRect::Intersects(dilatedBounds, fGridBounds)) {
+        return;
+    }
+
     int minTileX = SkMax32(SkMin32(dilatedBounds.left() / fTileWidth, fXTileCount - 1), 0);
     int maxTileX = SkMax32(SkMin32(dilatedBounds.right() / fTileWidth, fXTileCount - 1), 0);
     int minTileY = SkMax32(SkMin32(dilatedBounds.top() / fTileHeight, fYTileCount -1), 0);
diff --git a/src/core/SkTileGrid.h b/src/core/SkTileGrid.h
index 529cb1f..53136bc 100644
--- a/src/core/SkTileGrid.h
+++ b/src/core/SkTileGrid.h
@@ -56,7 +56,9 @@
     int fTileWidth, fTileHeight, fXTileCount, fYTileCount, fTileCount;
     SkTDArray<void *> *fTileData;
     int fInsertionCount;
+    SkIRect fGridBounds;
 
+    friend class TileGridTest;
     typedef SkBBoxHierarchy INHERITED;
 };
 
diff --git a/tests/TileGridTest.cpp b/tests/TileGridTest.cpp
new file mode 100644
index 0000000..e0c855c
--- /dev/null
+++ b/tests/TileGridTest.cpp
@@ -0,0 +1,60 @@
+
+/*
+ * Copyright 2012 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Test.h"
+#include "SkTileGrid.h"
+
+enum Tile {
+    kTopLeft_Tile = 0x1,
+    kTopRight_Tile = 0x2,
+    kBottomLeft_Tile = 0x4,
+    kBottomRight_Tile = 0x8,
+
+    kAll_Tile = kTopLeft_Tile | kTopRight_Tile | kBottomLeft_Tile | kBottomRight_Tile,
+};
+
+class TileGridTest {
+public:
+    static void verifyTileHits(skiatest::Reporter* reporter, SkIRect rect, uint32_t tileMask) {
+        SkTileGrid grid(10, 10, 2, 2);
+        grid.insert(NULL, rect, false);
+        REPORTER_ASSERT(reporter, grid.tile(0,0).count() ==
+            ((tileMask & kTopLeft_Tile)? 1 : 0));
+        REPORTER_ASSERT(reporter, grid.tile(1,0).count() ==
+            ((tileMask & kTopRight_Tile)? 1 : 0));
+        REPORTER_ASSERT(reporter, grid.tile(0,1).count() ==
+            ((tileMask & kBottomLeft_Tile)? 1 : 0));
+        REPORTER_ASSERT(reporter, grid.tile(1,1).count() ==
+            ((tileMask & kBottomRight_Tile)? 1 : 0));
+    }
+
+    static void Test(skiatest::Reporter* reporter) {
+        // Out of bounds
+        verifyTileHits(reporter, SkIRect::MakeXYWH(30, 0, 1, 1),  0);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(0, 30, 1, 1),  0);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(-10, 0, 1, 1),  0);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(0, -10, 1, 1),  0);
+
+        // Dilation for AA consideration
+        verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 8, 8),  kTopLeft_Tile);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(0, 0, 9, 9),  kAll_Tile);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(10, 10, 1, 1),  kAll_Tile);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(11, 11, 1, 1),  kBottomRight_Tile);
+
+        // BBoxes that overlap tiles
+        verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 1),  kTopLeft_Tile | kTopRight_Tile);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 1, 10),  kTopLeft_Tile |
+                       kBottomLeft_Tile);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(5, 5, 10, 10),  kAll_Tile);
+        verifyTileHits(reporter, SkIRect::MakeXYWH(-10, -10, 40, 40),  kAll_Tile);
+    }
+};
+
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("TileGrid", TileGridTestClass, TileGridTest::Test)