Bug 783758 - Add a close function on the PaintedSurface class to clean up the opened FileInputStream, and invoke it from tests. r=jmaher
authorKartikaya Gupta <kgupta@mozilla.com>
Mon, 27 Aug 2012 13:44:58 -0400
changeset 105622 16975ba38809accb39eeb50fd1f1cee06e8d50f6
parent 105621 b4c8fe10f767af3ee5be13626f7b75ff5446af2e
child 105623 72bb1083fbab13792b9434efe92a49403cb71433
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersjmaher
bugs783758
milestone17.0a1
Bug 783758 - Add a close function on the PaintedSurface class to clean up the opened FileInputStream, and invoke it from tests. r=jmaher
build/mobile/robocop/PaintedSurface.java.in
mobile/android/base/tests/PixelTest.java.in
mobile/android/base/tests/testAxisLocking.java.in
mobile/android/base/tests/testFlingCorrectness.java.in
mobile/android/base/tests/testOverscroll.java.in
mobile/android/base/tests/testPanCorrectness.java.in
mobile/android/base/tests/testPermissions.java.in
mobile/android/base/tests/test_bug720538.java.in
--- a/build/mobile/robocop/PaintedSurface.java.in
+++ b/build/mobile/robocop/PaintedSurface.java.in
@@ -7,32 +7,33 @@ package @ANDROID_PACKAGE_NAME@;
 
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
 import java.nio.MappedByteBuffer;
 import java.nio.channels.FileChannel;
 
 public class PaintedSurface {
-    private String mFileName = null;
-    private int mWidth = -1;
-    private int mHeight = -1;
-    private MappedByteBuffer mPixelBuffer = null;
+    private String mFileName;
+    private int mWidth;
+    private int mHeight;
+    private FileInputStream mPixelFile;
+    private MappedByteBuffer mPixelBuffer;
 
     public PaintedSurface(String filename, int width, int height) {
         mFileName = filename;
         mWidth = width;
         mHeight = height;
-        
+
         try {
             File f = new File(filename);
             int pixelSize = (int)f.length();
-            
-            FileInputStream pixelFile = new FileInputStream(filename);
-            mPixelBuffer = pixelFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, pixelSize);
+
+            mPixelFile = new FileInputStream(filename);
+            mPixelBuffer = mPixelFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, pixelSize);
         } catch (java.io.FileNotFoundException e) {
             FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
         } catch (java.io.IOException e) {
             FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
         }
     }
 
     public final int getPixelAt(int x, int y) {
@@ -53,10 +54,17 @@ public class PaintedSurface {
         int index = (x + ((mHeight - y - 1) * mWidth)) * 4;
         int b1 = mPixelBuffer.get(index) & 0xFF;
         int b2 = mPixelBuffer.get(index + 1) & 0xFF;
         int b3 = mPixelBuffer.get(index + 2) & 0xFF;
         int b4 = mPixelBuffer.get(index + 3) & 0xFF;
         int value = (b1 << 24) + (b2 << 16) + (b3 << 8) + (b4 << 0);
         return value;
     }
+
+    public void close() {
+        try {
+            mPixelFile.close();
+        } catch (Exception e) {
+            FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, e);
+        }
+    }
 }
-
--- a/mobile/android/base/tests/PixelTest.java.in
+++ b/mobile/android/base/tests/PixelTest.java.in
@@ -1,44 +1,54 @@
 #filter substitution
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
 
 abstract class PixelTest extends BaseTest {
     private static final long PAINT_CLEAR_DELAY = 1000; // milliseconds
 
-    protected final PaintedSurface loadAndPaint(String url) {
+    protected final PaintedSurface loadAndGetPainted(String url) {
         Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
         loadUrl(url);
         paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY);
         PaintedSurface p = mDriver.getPaintedSurface();
         if (p == null) {
             mAsserter.ok(p != null, "checking that painted surface loaded", 
                  "painted surface loaded");
         }
         return p;
     }
 
-    protected final PaintedSurface reloadAndPaint() {
+    protected final void loadAndPaint(String url) {
+        PaintedSurface painted = loadAndGetPainted(url);
+        painted.close();
+    }
+
+    protected final PaintedSurface reloadAndGetPainted() {
         Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
 
         mActions.sendSpecialKey(Actions.SpecialKey.MENU);
         mSolo.waitForText("Reload");
         mSolo.clickOnText("Reload");
 
         paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY);
         PaintedSurface p = mDriver.getPaintedSurface();
         if (p == null) {
             mAsserter.ok(p != null, "checking that painted surface loaded", 
                  "painted surface loaded");
         }
         return p;
     }
 
+    protected final void reloadAndPaint() {
+        PaintedSurface painted = reloadAndGetPainted();
+        painted.close();
+    }
+
     protected final PaintedSurface waitForPaint(Actions.RepeatedEventExpecter expecter) {
         expecter.blockUntilClear(PAINT_CLEAR_DELAY);
         PaintedSurface p = mDriver.getPaintedSurface();
         if (p == null) {
             mAsserter.ok(p != null, "checking that painted surface loaded", 
                  "painted surface loaded");
         }
         return p;
@@ -84,14 +94,17 @@ abstract class PixelTest extends BaseTes
         mAsserter.ispixel(painted.getPixelAt(100, 100), color[0], color[1], color[2], "Pixel at 100, 100");
     }
 
     /**
      * Loads the robocop_boxes.html file and verifies that we are positioned at (0,0) on it.
      * @param url URL of the robocop_boxes.html file.
      * @return The painted surface after rendering the file.
      */
-    protected final PaintedSurface loadAndVerifyBoxes(String url) {
-        PaintedSurface painted = loadAndPaint(url);
-        checkScrollWithBoxes(painted, 0, 0);
-        return painted;
+    protected final void loadAndVerifyBoxes(String url) {
+        PaintedSurface painted = loadAndGetPainted(url);
+        try {
+            checkScrollWithBoxes(painted, 0, 0);
+        } finally {
+            painted.close();
+        }
     }
 }
--- a/mobile/android/base/tests/testAxisLocking.java.in
+++ b/mobile/android/base/tests/testAxisLocking.java.in
@@ -28,22 +28,31 @@ public class testAxisLocking extends Pix
         // load page and check we're at 0,0
         loadAndVerifyBoxes(url);
 
         // drag page upwards by 100 pixels with a slight angle. verify that
         // axis locking prevents any horizontal scrolling
         Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
         meh.dragSync(20, 150, 10, 50);
         PaintedSurface painted = waitForPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 0, 100);
-        // since checkScrollWithBoxes only checks 4 points, it may not pick up a
-        // sub-100 pixel horizontal shift. so we check another point manually to make sure.
-        int[] color = getBoxColorAt(0, 100);
-        mAsserter.ispixel(painted.getPixelAt(99, 0), color[0], color[1], color[2], "Pixel at 99, 0 indicates no horizontal scroll");
+        try {
+            checkScrollWithBoxes(painted, 0, 100);
+            // since checkScrollWithBoxes only checks 4 points, it may not pick up a
+            // sub-100 pixel horizontal shift. so we check another point manually to make sure.
+            int[] color = getBoxColorAt(0, 100);
+            mAsserter.ispixel(painted.getPixelAt(99, 0), color[0], color[1], color[2], "Pixel at 99, 0 indicates no horizontal scroll");
 
-        // now drag at a 45-degree angle to ensure we break the axis lock, and
-        // verify that we have both horizontal and vertical scrolling
-        paintExpecter = mActions.expectPaint();
-        meh.dragSync(150, 150, 50, 50);
+            // now drag at a 45-degree angle to ensure we break the axis lock, and
+            // verify that we have both horizontal and vertical scrolling
+            paintExpecter = mActions.expectPaint();
+            meh.dragSync(150, 150, 50, 50);
+        } finally {
+            painted.close();
+        }
+
         painted = waitForPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 100, 200);
+        try {
+            checkScrollWithBoxes(painted, 100, 200);
+        } finally {
+            painted.close();
+        }
     }
 }
--- a/mobile/android/base/tests/testFlingCorrectness.java.in
+++ b/mobile/android/base/tests/testFlingCorrectness.java.in
@@ -27,17 +27,25 @@ public class testFlingCorrectness extend
         loadAndVerifyBoxes(url);
 
         // drag page upwards by 200 pixels (use two drags instead of one in case
         // the screen size is small)
         Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
         meh.dragSync(10, 150, 10, 50);
         meh.dragSync(10, 150, 10, 50);
         PaintedSurface painted = waitForPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 0, 200);
+        try {
+            checkScrollWithBoxes(painted, 0, 200);
+        } finally {
+            painted.close();
+        }
 
         // now fling page downwards using a 100-pixel drag but a velocity of 15px/sec, so that
         // we scroll the full 200 pixels back to the top of the page
         meh.flingSync(10, 50, 10, 150, 15);
         painted = waitForPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 0, 0);
+        try {
+            checkScrollWithBoxes(painted, 0, 0);
+        } finally {
+            painted.close();
+        }
     }
 }
--- a/mobile/android/base/tests/testOverscroll.java.in
+++ b/mobile/android/base/tests/testOverscroll.java.in
@@ -28,17 +28,25 @@ public class testOverscroll extends Pixe
 
         // drag page downwards by 100 pixels so that it goes into overscroll.
         // wait for it to bounce back and check we're at the right spot.
         // the screen size is small). Note that since we only go into overscroll
         // and back this should NOT trigger a gecko-paint
         Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
         meh.dragSync(10, 50, 10, 150);
         PaintedSurface painted = waitWithNoPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 0, 0);
+        try {
+            checkScrollWithBoxes(painted, 0, 0);
+        } finally {
+            painted.close();
+        }
 
         // drag page rightwards to go into overscroll on the left. let it bounce and verify.
         paintExpecter = mActions.expectPaint();
         meh.dragSync(50, 10, 150, 10);
         painted = waitWithNoPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 0, 0);
+        try {
+            checkScrollWithBoxes(painted, 0, 0);
+        } finally {
+            painted.close();
+        }
     }
 }
--- a/mobile/android/base/tests/testPanCorrectness.java.in
+++ b/mobile/android/base/tests/testPanCorrectness.java.in
@@ -25,17 +25,25 @@ public class testPanCorrectness extends 
 
         // load page and check we're at 0,0
         loadAndVerifyBoxes(url);
 
         // drag page upwards by 100 pixels
         Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
         meh.dragSync(10, 150, 10, 50);
         PaintedSurface painted = waitForPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 0, 100);
+        try {
+            checkScrollWithBoxes(painted, 0, 100);
+        } finally {
+            painted.close();
+        }
 
         // drag page leftwards by 100 pixels
         paintExpecter = mActions.expectPaint();
         meh.dragSync(150, 10, 50, 10);
         painted = waitForPaint(paintExpecter);
-        checkScrollWithBoxes(painted, 100, 100);
+        try {
+            checkScrollWithBoxes(painted, 100, 100);
+        } finally {
+            painted.close();
+        }
     }
 }
--- a/mobile/android/base/tests/testPermissions.java.in
+++ b/mobile/android/base/tests/testPermissions.java.in
@@ -2,58 +2,69 @@
 package @ANDROID_PACKAGE_NAME@.tests;
 
 import @ANDROID_PACKAGE_NAME@.*;
 
 import android.widget.CheckBox;
 import java.util.ArrayList;
 
 public class testPermissions extends PixelTest {
-    private PaintedSurface mPaintedSurface;
     private Actions.RepeatedEventExpecter mPaintExpecter;
 
     @Override
     protected int getTestType() {
         return TEST_MOCHITEST;
     }
 
     public void testPermissions() {
         mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
 
         geolocationTest();
     }
 
     private void geolocationTest() {
         // Test geolocation notification
-        mPaintedSurface = loadAndPaint(getAbsoluteUrl("/robocop/robocop_geolocation.html"));
+        loadAndPaint(getAbsoluteUrl("/robocop/robocop_geolocation.html"));
         mSolo.waitForText("wants your location");
 
         // Uncheck the "Don't ask again for this site" checkbox
         ArrayList<CheckBox> checkBoxes = mSolo.getCurrentCheckBoxes();
         mAsserter.ok(checkBoxes.size() == 1, "checkbox count", "only one checkbox visible");
         mAsserter.ok(mSolo.isCheckBoxChecked(0), "checkbox checked", "checkbox is checked");
         mSolo.clickOnCheckBox(0);
         mAsserter.ok(!mSolo.isCheckBoxChecked(0), "checkbox not checked", "checkbox is not checked");
 
         // Test "Share" button functionality with unchecked checkbox
         mPaintExpecter = mActions.expectPaint();
         mSolo.clickOnText("Share");
-        mPaintedSurface = waitForPaint(mPaintExpecter);
-        mAsserter.ispixel(mPaintedSurface.getPixelAt(10, 10), 0, 0x80, 0, "checking page background is green");
+        PaintedSurface painted = waitForPaint(mPaintExpecter);
+        try {
+            mAsserter.ispixel(painted.getPixelAt(10, 10), 0, 0x80, 0, "checking page background is green");
+        } finally {
+            painted.close();
+        }
 
         // Re-trigger geolocation notification
         reloadAndPaint();
         mSolo.waitForText("wants your location");
 
         // Make sure the checkbox is checked this time
         mAsserter.ok(mSolo.isCheckBoxChecked(0), "checkbox checked", "checkbox is checked");
 
         // Test "Share" button functionality with checked checkbox
         mPaintExpecter = mActions.expectPaint();
         mSolo.clickOnText("Share");
-        mPaintedSurface = waitForPaint(mPaintExpecter);
-        mAsserter.ispixel(mPaintedSurface.getPixelAt(10, 10), 0, 0x80, 0, "checking page background is green");
+        painted = waitForPaint(mPaintExpecter);
+        try {
+            mAsserter.ispixel(painted.getPixelAt(10, 10), 0, 0x80, 0, "checking page background is green");
+        } finally {
+            painted.close();
+        }
 
         // When we reload the page, location should be automatically shared
-        mPaintedSurface = reloadAndPaint();
-        mAsserter.ispixel(mPaintedSurface.getPixelAt(10, 10), 0, 0x80, 0, "checking page background is green");
+        painted = reloadAndGetPainted();
+        try {
+            mAsserter.ispixel(painted.getPixelAt(10, 10), 0, 0x80, 0, "checking page background is green");
+        } finally {
+            painted.close();
+        }
     }
 }
--- a/mobile/android/base/tests/test_bug720538.java.in
+++ b/mobile/android/base/tests/test_bug720538.java.in
@@ -23,44 +23,56 @@ public class test_bug720538 extends Pixe
          * view area leaving a little bit of space on either side. We can test for this by checking
          * a few pixels to ensure that there is some background visible on either side of the iframe.
          * Finally, when we double-tap on it to zoom out again, we need to check that the bottom of
          * the view doesn't have any checkerboarding. We can verify this by getting a few pixels and
          * checking that it is the same as the expected background color of the page, as opposed to
          * the gray shades of the checkerboard.
          */
 
-        PaintedSurface painted = loadAndPaint(url);
+        PaintedSurface painted = loadAndGetPainted(url);
 
-        // first we check that the point we want to double-tap (100, 100) is blue, indicating it's inside the iframe
-        mAsserter.ispixel(painted.getPixelAt(100, 100), 0, 0, 0xFF, "Ensuring double-tap point is in the iframe");
+        try {
+            // first we check that the point we want to double-tap (100, 100) is blue, indicating it's inside the iframe
+            mAsserter.ispixel(painted.getPixelAt(100, 100), 0, 0, 0xFF, "Ensuring double-tap point is in the iframe");
+        } finally {
+            painted.close();
+        }
 
         // do the double tap and wait for the double-tap animation to finish. we assume the animation is done
         // when we find a 500ms period with no paint events that occurs after at least one paint event.
         Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint();
         MotionEventHelper meh = new MotionEventHelper(getInstrumentation(), mDriver.getGeckoLeft(), mDriver.getGeckoTop());
         meh.doubleTap(100, 100);
         painted = waitForPaint(paintExpecter);
 
-        // check a few points to ensure that we did a good zoom-to-block on the iframe. this checks that
-        // the background color is visible on the left and right edges of the viewport, but the iframe is
-        // visible in between those edges
-        mAsserter.ispixel(painted.getPixelAt(0, 100), 0, 0x80, 0, "Checking page background to the left of the iframe");
-        mAsserter.ispixel(painted.getPixelAt(50, 100), 0, 0, 0xFF, "Checking for iframe a few pixels from the left edge");
-        mAsserter.ispixel(painted.getPixelAt(mDriver.getGeckoWidth() - 51, 100), 0, 0, 0xFF, "Checking for iframe a few pixels from the right edge");
-        mAsserter.ispixel(painted.getPixelAt(mDriver.getGeckoWidth() - 1, 100), 0, 0x80, 0, "Checking page background the right of the iframe");
+        try {
+            // check a few points to ensure that we did a good zoom-to-block on the iframe. this checks that
+            // the background color is visible on the left and right edges of the viewport, but the iframe is
+            // visible in between those edges
+            mAsserter.ispixel(painted.getPixelAt(0, 100), 0, 0x80, 0, "Checking page background to the left of the iframe");
+            mAsserter.ispixel(painted.getPixelAt(50, 100), 0, 0, 0xFF, "Checking for iframe a few pixels from the left edge");
+            mAsserter.ispixel(painted.getPixelAt(mDriver.getGeckoWidth() - 51, 100), 0, 0, 0xFF, "Checking for iframe a few pixels from the right edge");
+            mAsserter.ispixel(painted.getPixelAt(mDriver.getGeckoWidth() - 1, 100), 0, 0x80, 0, "Checking page background the right of the iframe");
+        } finally {
+            painted.close();
+        }
 
         // now we do double-tap again to zoom out and wait for the animation to finish, as before
         paintExpecter = mActions.expectPaint();
         meh.doubleTap(100, mDriver.getGeckoWidth() / 2);
         painted = waitForPaint(paintExpecter);
 
-        // and now we check a pixel at the bottom of the view to ensure that we have the page
-        // background and not some checkerboarding. use the second-last row of pixels instead of
-        // the last row because the last row is subject to rounding and clipping errors
-        for (int y = 2; y < 10; y++) {
-            for (int x = 0; x < 10; x++) {
-                mAsserter.dumpLog("Pixel at " + x + ", " + (mDriver.getGeckoHeight() - y) + ": " + Integer.toHexString(painted.getPixelAt(x, mDriver.getGeckoHeight() - y)));
+        try {
+            // and now we check a pixel at the bottom of the view to ensure that we have the page
+            // background and not some checkerboarding. use the second-last row of pixels instead of
+            // the last row because the last row is subject to rounding and clipping errors
+            for (int y = 2; y < 10; y++) {
+                for (int x = 0; x < 10; x++) {
+                    mAsserter.dumpLog("Pixel at " + x + ", " + (mDriver.getGeckoHeight() - y) + ": " + Integer.toHexString(painted.getPixelAt(x, mDriver.getGeckoHeight() - y)));
+                }
             }
+            mAsserter.ispixel(painted.getPixelAt(0, mDriver.getGeckoHeight() - 2), 0, 0x80, 0, "Checking bottom-left corner of viewport");
+        } finally {
+            painted.close();
         }
-        mAsserter.ispixel(painted.getPixelAt(0, mDriver.getGeckoHeight() - 2), 0, 0x80, 0, "Checking bottom-left corner of viewport");
     }
 }