Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | package org.mozilla.gecko.tests; |
michael@0 | 2 | |
michael@0 | 3 | import org.mozilla.gecko.Actions; |
michael@0 | 4 | import org.mozilla.gecko.PaintedSurface; |
michael@0 | 5 | |
michael@0 | 6 | abstract class PixelTest extends BaseTest { |
michael@0 | 7 | private static final long PAINT_CLEAR_DELAY = 10000; // milliseconds |
michael@0 | 8 | |
michael@0 | 9 | protected final PaintedSurface loadAndGetPainted(String url) { |
michael@0 | 10 | Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint(); |
michael@0 | 11 | inputAndLoadUrl(url); |
michael@0 | 12 | verifyHomePagerHidden(); |
michael@0 | 13 | paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY); |
michael@0 | 14 | paintExpecter.unregisterListener(); |
michael@0 | 15 | PaintedSurface p = mDriver.getPaintedSurface(); |
michael@0 | 16 | if (p == null) { |
michael@0 | 17 | mAsserter.ok(p != null, "checking that painted surface loaded", |
michael@0 | 18 | "painted surface loaded"); |
michael@0 | 19 | } |
michael@0 | 20 | return p; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | protected final void loadAndPaint(String url) { |
michael@0 | 24 | PaintedSurface painted = loadAndGetPainted(url); |
michael@0 | 25 | painted.close(); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | protected final PaintedSurface reloadAndGetPainted() { |
michael@0 | 29 | Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint(); |
michael@0 | 30 | |
michael@0 | 31 | mActions.sendSpecialKey(Actions.SpecialKey.MENU); |
michael@0 | 32 | waitForText("Reload"); |
michael@0 | 33 | mSolo.clickOnText("Reload"); |
michael@0 | 34 | |
michael@0 | 35 | paintExpecter.blockUntilClear(PAINT_CLEAR_DELAY); |
michael@0 | 36 | paintExpecter.unregisterListener(); |
michael@0 | 37 | PaintedSurface p = mDriver.getPaintedSurface(); |
michael@0 | 38 | if (p == null) { |
michael@0 | 39 | mAsserter.ok(p != null, "checking that painted surface loaded", |
michael@0 | 40 | "painted surface loaded"); |
michael@0 | 41 | } |
michael@0 | 42 | return p; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | protected final void reloadAndPaint() { |
michael@0 | 46 | PaintedSurface painted = reloadAndGetPainted(); |
michael@0 | 47 | painted.close(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | public void addTab(String url, String title, boolean isPrivate) { |
michael@0 | 51 | Actions.EventExpecter tabEventExpecter = mActions.expectGeckoEvent("Tab:Added"); |
michael@0 | 52 | Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded"); |
michael@0 | 53 | if (isPrivate) { |
michael@0 | 54 | selectMenuItem(StringHelper.NEW_PRIVATE_TAB_LABEL); |
michael@0 | 55 | } else { |
michael@0 | 56 | selectMenuItem(StringHelper.NEW_TAB_LABEL); |
michael@0 | 57 | } |
michael@0 | 58 | tabEventExpecter.blockForEvent(); |
michael@0 | 59 | contentEventExpecter.blockForEvent(); |
michael@0 | 60 | waitForText(StringHelper.TITLE_PLACE_HOLDER); |
michael@0 | 61 | loadAndPaint(url); |
michael@0 | 62 | tabEventExpecter.unregisterListener(); |
michael@0 | 63 | contentEventExpecter.unregisterListener(); |
michael@0 | 64 | mAsserter.ok(waitForText(title), "Checking that the page has loaded", "The page has loaded"); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | protected final PaintedSurface waitForPaint(Actions.RepeatedEventExpecter expecter) { |
michael@0 | 68 | expecter.blockUntilClear(PAINT_CLEAR_DELAY); |
michael@0 | 69 | PaintedSurface p = mDriver.getPaintedSurface(); |
michael@0 | 70 | if (p == null) { |
michael@0 | 71 | mAsserter.ok(p != null, "checking that painted surface loaded", |
michael@0 | 72 | "painted surface loaded"); |
michael@0 | 73 | } |
michael@0 | 74 | return p; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | protected final PaintedSurface waitWithNoPaint(Actions.RepeatedEventExpecter expecter) { |
michael@0 | 78 | try { |
michael@0 | 79 | Thread.sleep(PAINT_CLEAR_DELAY); |
michael@0 | 80 | } catch (InterruptedException ie) { |
michael@0 | 81 | ie.printStackTrace(); |
michael@0 | 82 | } |
michael@0 | 83 | mAsserter.is(expecter.eventReceived(), false, "Checking gecko didn't draw unnecessarily"); |
michael@0 | 84 | PaintedSurface p = mDriver.getPaintedSurface(); |
michael@0 | 85 | if (p == null) { |
michael@0 | 86 | mAsserter.ok(p != null, "checking that painted surface loaded", |
michael@0 | 87 | "painted surface loaded"); |
michael@0 | 88 | } |
michael@0 | 89 | return p; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | // this matches the algorithm in robocop_boxes.html |
michael@0 | 93 | protected final int[] getBoxColorAt(int x, int y) { |
michael@0 | 94 | int r = ((int)Math.floor(x / 3) % 256); |
michael@0 | 95 | r = r & 0xF8; |
michael@0 | 96 | int g = (x + y) % 256; |
michael@0 | 97 | g = g & 0xFC; |
michael@0 | 98 | int b = ((int)Math.floor(y / 3) % 256); |
michael@0 | 99 | b = b & 0xF8; |
michael@0 | 100 | return new int[] { r, g, b }; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | /** |
michael@0 | 104 | * Checks the top-left corner of the visible area of the page is at (x,y) of robocop_boxes.html. |
michael@0 | 105 | */ |
michael@0 | 106 | protected final void checkScrollWithBoxes(PaintedSurface painted, int x, int y) { |
michael@0 | 107 | int[] color = getBoxColorAt(x, y); |
michael@0 | 108 | mAsserter.ispixel(painted.getPixelAt(0, 0), color[0], color[1], color[2], "Pixel at 0, 0"); |
michael@0 | 109 | color = getBoxColorAt(x + 100, y); |
michael@0 | 110 | mAsserter.ispixel(painted.getPixelAt(100, 0), color[0], color[1], color[2], "Pixel at 100, 0"); |
michael@0 | 111 | color = getBoxColorAt(x, y + 100); |
michael@0 | 112 | mAsserter.ispixel(painted.getPixelAt(0, 100), color[0], color[1], color[2], "Pixel at 0, 100"); |
michael@0 | 113 | color = getBoxColorAt(x + 100, y + 100); |
michael@0 | 114 | mAsserter.ispixel(painted.getPixelAt(100, 100), color[0], color[1], color[2], "Pixel at 100, 100"); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * Loads the robocop_boxes.html file and verifies that we are positioned at (0,0) on it. |
michael@0 | 119 | * @param url URL of the robocop_boxes.html file. |
michael@0 | 120 | * @return The painted surface after rendering the file. |
michael@0 | 121 | */ |
michael@0 | 122 | protected final void loadAndVerifyBoxes(String url) { |
michael@0 | 123 | PaintedSurface painted = loadAndGetPainted(url); |
michael@0 | 124 | try { |
michael@0 | 125 | checkScrollWithBoxes(painted, 0, 0); |
michael@0 | 126 | } finally { |
michael@0 | 127 | painted.close(); |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | } |