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