michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import org.mozilla.gecko.Actions; michael@0: import org.mozilla.gecko.PaintedSurface; michael@0: michael@0: abstract class PixelTest extends BaseTest { michael@0: private static final long PAINT_CLEAR_DELAY = 10000; // milliseconds michael@0: michael@0: protected final PaintedSurface loadAndGetPainted(String url) { michael@0: Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint(); michael@0: inputAndLoadUrl(url); michael@0: verifyHomePagerHidden(); michael@0: paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY); michael@0: paintExpecter.unregisterListener(); michael@0: PaintedSurface p = mDriver.getPaintedSurface(); michael@0: if (p == null) { michael@0: mAsserter.ok(p != null, "checking that painted surface loaded", michael@0: "painted surface loaded"); michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: protected final void loadAndPaint(String url) { michael@0: PaintedSurface painted = loadAndGetPainted(url); michael@0: painted.close(); michael@0: } michael@0: michael@0: protected final PaintedSurface reloadAndGetPainted() { michael@0: Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint(); michael@0: michael@0: mActions.sendSpecialKey(Actions.SpecialKey.MENU); michael@0: waitForText("Reload"); michael@0: mSolo.clickOnText("Reload"); michael@0: michael@0: paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY); michael@0: paintExpecter.unregisterListener(); michael@0: PaintedSurface p = mDriver.getPaintedSurface(); michael@0: if (p == null) { michael@0: mAsserter.ok(p != null, "checking that painted surface loaded", michael@0: "painted surface loaded"); michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: protected final void reloadAndPaint() { michael@0: PaintedSurface painted = reloadAndGetPainted(); michael@0: painted.close(); michael@0: } michael@0: michael@0: public void addTab(String url, String title, boolean isPrivate) { michael@0: Actions.EventExpecter tabEventExpecter = mActions.expectGeckoEvent("Tab:Added"); michael@0: Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded"); michael@0: if (isPrivate) { michael@0: selectMenuItem(StringHelper.NEW_PRIVATE_TAB_LABEL); michael@0: } else { michael@0: selectMenuItem(StringHelper.NEW_TAB_LABEL); michael@0: } michael@0: tabEventExpecter.blockForEvent(); michael@0: contentEventExpecter.blockForEvent(); michael@0: waitForText(StringHelper.TITLE_PLACE_HOLDER); michael@0: loadAndPaint(url); michael@0: tabEventExpecter.unregisterListener(); michael@0: contentEventExpecter.unregisterListener(); michael@0: mAsserter.ok(waitForText(title), "Checking that the page has loaded", "The page has loaded"); michael@0: } michael@0: michael@0: protected final PaintedSurface waitForPaint(Actions.RepeatedEventExpecter expecter) { michael@0: expecter.blockUntilClear(PAINT_CLEAR_DELAY); michael@0: PaintedSurface p = mDriver.getPaintedSurface(); michael@0: if (p == null) { michael@0: mAsserter.ok(p != null, "checking that painted surface loaded", michael@0: "painted surface loaded"); michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: protected final PaintedSurface waitWithNoPaint(Actions.RepeatedEventExpecter expecter) { michael@0: try { michael@0: Thread.sleep(PAINT_CLEAR_DELAY); michael@0: } catch (InterruptedException ie) { michael@0: ie.printStackTrace(); michael@0: } michael@0: mAsserter.is(expecter.eventReceived(), false, "Checking gecko didn't draw unnecessarily"); michael@0: PaintedSurface p = mDriver.getPaintedSurface(); michael@0: if (p == null) { michael@0: mAsserter.ok(p != null, "checking that painted surface loaded", michael@0: "painted surface loaded"); michael@0: } michael@0: return p; michael@0: } michael@0: michael@0: // this matches the algorithm in robocop_boxes.html michael@0: protected final int[] getBoxColorAt(int x, int y) { michael@0: int r = ((int)Math.floor(x / 3) % 256); michael@0: r = r & 0xF8; michael@0: int g = (x + y) % 256; michael@0: g = g & 0xFC; michael@0: int b = ((int)Math.floor(y / 3) % 256); michael@0: b = b & 0xF8; michael@0: return new int[] { r, g, b }; michael@0: } michael@0: michael@0: /** michael@0: * Checks the top-left corner of the visible area of the page is at (x,y) of robocop_boxes.html. michael@0: */ michael@0: protected final void checkScrollWithBoxes(PaintedSurface painted, int x, int y) { michael@0: int[] color = getBoxColorAt(x, y); michael@0: mAsserter.ispixel(painted.getPixelAt(0, 0), color[0], color[1], color[2], "Pixel at 0, 0"); michael@0: color = getBoxColorAt(x + 100, y); michael@0: mAsserter.ispixel(painted.getPixelAt(100, 0), color[0], color[1], color[2], "Pixel at 100, 0"); michael@0: color = getBoxColorAt(x, y + 100); michael@0: mAsserter.ispixel(painted.getPixelAt(0, 100), color[0], color[1], color[2], "Pixel at 0, 100"); michael@0: color = getBoxColorAt(x + 100, y + 100); michael@0: mAsserter.ispixel(painted.getPixelAt(100, 100), color[0], color[1], color[2], "Pixel at 100, 100"); michael@0: } michael@0: michael@0: /** michael@0: * Loads the robocop_boxes.html file and verifies that we are positioned at (0,0) on it. michael@0: * @param url URL of the robocop_boxes.html file. michael@0: * @return The painted surface after rendering the file. michael@0: */ michael@0: protected final void loadAndVerifyBoxes(String url) { michael@0: PaintedSurface painted = loadAndGetPainted(url); michael@0: try { michael@0: checkScrollWithBoxes(painted, 0, 0); michael@0: } finally { michael@0: painted.close(); michael@0: } michael@0: } michael@0: }