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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial