mobile/android/base/tests/components/AboutHomeComponent.java

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:74a302873c46
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.gecko.tests.components;
6
7 import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertEquals;
8 import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue;
9
10 import org.mozilla.gecko.R;
11 import org.mozilla.gecko.tests.UITestContext;
12 import org.mozilla.gecko.tests.helpers.DeviceHelper;
13 import org.mozilla.gecko.tests.helpers.WaitHelper;
14
15 import android.support.v4.view.ViewPager;
16 import android.view.View;
17 import android.widget.TextView;
18
19 import com.jayway.android.robotium.solo.Condition;
20 import com.jayway.android.robotium.solo.Solo;
21
22 /**
23 * A class representing any interactions that take place on the Awesomescreen.
24 */
25 public class AboutHomeComponent extends BaseComponent {
26 private static final String LOGTAG = AboutHomeComponent.class.getSimpleName();
27
28 // The different types of panels that can be present on about:home
29 public enum PanelType {
30 HISTORY,
31 TOP_SITES,
32 BOOKMARKS,
33 READING_LIST
34 }
35
36 // TODO: Having a specific ordering of panels is prone to fail and thus temporary.
37 // Hopefully the work in bug 940565 will alleviate the need for these enums.
38 // Explicit ordering of HomePager panels on a phone.
39 private enum PhonePanel {
40 HISTORY,
41 TOP_SITES,
42 BOOKMARKS,
43 READING_LIST
44 }
45
46 // Explicit ordering of HomePager panels on a tablet.
47 private enum TabletPanel {
48 TOP_SITES,
49 BOOKMARKS,
50 READING_LIST,
51 HISTORY
52 }
53
54 // The percentage of the panel to swipe between 0 and 1. This value was set through
55 // testing: 0.55f was tested on try and fails on armv6 devices.
56 private static final float SWIPE_PERCENTAGE = 0.70f;
57
58 public AboutHomeComponent(final UITestContext testContext) {
59 super(testContext);
60 }
61
62 private View getHomePagerContainer() {
63 return mSolo.getView(R.id.home_pager_container);
64 }
65
66 private ViewPager getHomePagerView() {
67 return (ViewPager) mSolo.getView(R.id.home_pager);
68 }
69
70 private View getHomeBannerView() {
71 return mSolo.getView(R.id.home_banner);
72 }
73
74 public AboutHomeComponent assertCurrentPanel(final PanelType expectedPanel) {
75 assertVisible();
76
77 final int expectedPanelIndex = getPanelIndexForDevice(expectedPanel.ordinal());
78 fAssertEquals("The current HomePager panel is " + expectedPanel,
79 expectedPanelIndex, getHomePagerView().getCurrentItem());
80 return this;
81 }
82
83 public AboutHomeComponent assertNotVisible() {
84 fAssertTrue("The HomePager is not visible",
85 getHomePagerContainer().getVisibility() != View.VISIBLE ||
86 getHomePagerView().getVisibility() != View.VISIBLE);
87 return this;
88 }
89
90 public AboutHomeComponent assertVisible() {
91 fAssertTrue("The HomePager is visible",
92 getHomePagerContainer().getVisibility() == View.VISIBLE &&
93 getHomePagerView().getVisibility() == View.VISIBLE);
94 return this;
95 }
96
97 public AboutHomeComponent assertBannerNotVisible() {
98 View banner = getHomeBannerView();
99 fAssertTrue("The HomeBanner is not visible",
100 getHomePagerContainer().getVisibility() != View.VISIBLE ||
101 banner.getVisibility() != View.VISIBLE ||
102 banner.getTranslationY() == banner.getHeight());
103 return this;
104 }
105
106 public AboutHomeComponent assertBannerVisible() {
107 fAssertTrue("The HomeBanner is visible",
108 getHomePagerContainer().getVisibility() == View.VISIBLE &&
109 getHomeBannerView().getVisibility() == View.VISIBLE);
110 return this;
111 }
112
113 public AboutHomeComponent assertBannerText(String text) {
114 assertBannerVisible();
115
116 final TextView textView = (TextView) getHomeBannerView().findViewById(R.id.text);
117 fAssertEquals("The correct HomeBanner text is shown",
118 text, textView.getText().toString());
119 return this;
120 }
121
122 public AboutHomeComponent clickOnBanner() {
123 assertBannerVisible();
124
125 mTestContext.dumpLog(LOGTAG, "Clicking on HomeBanner.");
126 mSolo.clickOnView(getHomeBannerView());
127 return this;
128 }
129
130 public AboutHomeComponent dismissBanner() {
131 assertBannerVisible();
132
133 mTestContext.dumpLog(LOGTAG, "Clicking on HomeBanner close button.");
134 mSolo.clickOnView(getHomeBannerView().findViewById(R.id.close));
135 return this;
136 }
137
138 public AboutHomeComponent swipeToPanelOnRight() {
139 mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the right.");
140 swipeToPanel(Solo.RIGHT);
141 return this;
142 }
143
144 public AboutHomeComponent swipeToPanelOnLeft() {
145 mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the left.");
146 swipeToPanel(Solo.LEFT);
147 return this;
148 }
149
150 private void swipeToPanel(final int panelDirection) {
151 fAssertTrue("Swiping in a valid direction",
152 panelDirection == Solo.LEFT || panelDirection == Solo.RIGHT);
153 assertVisible();
154
155 final int panelIndex = getHomePagerView().getCurrentItem();
156
157 mSolo.scrollViewToSide(getHomePagerView(), panelDirection, SWIPE_PERCENTAGE);
158
159 // The panel on the left is a lower index and vice versa.
160 final int unboundedPanelIndex = panelIndex + (panelDirection == Solo.LEFT ? -1 : 1);
161 final int panelCount = DeviceHelper.isTablet() ?
162 TabletPanel.values().length : PhonePanel.values().length;
163 final int maxPanelIndex = panelCount - 1;
164 final int expectedPanelIndex = Math.min(Math.max(0, unboundedPanelIndex), maxPanelIndex);
165
166 waitForPanelIndex(expectedPanelIndex);
167 }
168
169 private void waitForPanelIndex(final int expectedIndex) {
170 final String panelName;
171 if (DeviceHelper.isTablet()) {
172 panelName = TabletPanel.values()[expectedIndex].name();
173 } else {
174 panelName = PhonePanel.values()[expectedIndex].name();
175 }
176
177 WaitHelper.waitFor("HomePager " + panelName + " panel", new Condition() {
178 @Override
179 public boolean isSatisfied() {
180 return (getHomePagerView().getCurrentItem() == expectedIndex);
181 }
182 });
183 }
184
185 /**
186 * Gets the panel index in the device specific Panel enum for the given index in the
187 * PanelType enum.
188 */
189 private int getPanelIndexForDevice(final int panelIndex) {
190 final String panelName = PanelType.values()[panelIndex].name();
191 final Class devicePanelEnum =
192 DeviceHelper.isTablet() ? TabletPanel.class : PhonePanel.class;
193 return Enum.valueOf(devicePanelEnum, panelName).ordinal();
194 }
195 }

mercurial