1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/components/AboutHomeComponent.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,195 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.tests.components; 1.9 + 1.10 +import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertEquals; 1.11 +import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue; 1.12 + 1.13 +import org.mozilla.gecko.R; 1.14 +import org.mozilla.gecko.tests.UITestContext; 1.15 +import org.mozilla.gecko.tests.helpers.DeviceHelper; 1.16 +import org.mozilla.gecko.tests.helpers.WaitHelper; 1.17 + 1.18 +import android.support.v4.view.ViewPager; 1.19 +import android.view.View; 1.20 +import android.widget.TextView; 1.21 + 1.22 +import com.jayway.android.robotium.solo.Condition; 1.23 +import com.jayway.android.robotium.solo.Solo; 1.24 + 1.25 +/** 1.26 + * A class representing any interactions that take place on the Awesomescreen. 1.27 + */ 1.28 +public class AboutHomeComponent extends BaseComponent { 1.29 + private static final String LOGTAG = AboutHomeComponent.class.getSimpleName(); 1.30 + 1.31 + // The different types of panels that can be present on about:home 1.32 + public enum PanelType { 1.33 + HISTORY, 1.34 + TOP_SITES, 1.35 + BOOKMARKS, 1.36 + READING_LIST 1.37 + } 1.38 + 1.39 + // TODO: Having a specific ordering of panels is prone to fail and thus temporary. 1.40 + // Hopefully the work in bug 940565 will alleviate the need for these enums. 1.41 + // Explicit ordering of HomePager panels on a phone. 1.42 + private enum PhonePanel { 1.43 + HISTORY, 1.44 + TOP_SITES, 1.45 + BOOKMARKS, 1.46 + READING_LIST 1.47 + } 1.48 + 1.49 + // Explicit ordering of HomePager panels on a tablet. 1.50 + private enum TabletPanel { 1.51 + TOP_SITES, 1.52 + BOOKMARKS, 1.53 + READING_LIST, 1.54 + HISTORY 1.55 + } 1.56 + 1.57 + // The percentage of the panel to swipe between 0 and 1. This value was set through 1.58 + // testing: 0.55f was tested on try and fails on armv6 devices. 1.59 + private static final float SWIPE_PERCENTAGE = 0.70f; 1.60 + 1.61 + public AboutHomeComponent(final UITestContext testContext) { 1.62 + super(testContext); 1.63 + } 1.64 + 1.65 + private View getHomePagerContainer() { 1.66 + return mSolo.getView(R.id.home_pager_container); 1.67 + } 1.68 + 1.69 + private ViewPager getHomePagerView() { 1.70 + return (ViewPager) mSolo.getView(R.id.home_pager); 1.71 + } 1.72 + 1.73 + private View getHomeBannerView() { 1.74 + return mSolo.getView(R.id.home_banner); 1.75 + } 1.76 + 1.77 + public AboutHomeComponent assertCurrentPanel(final PanelType expectedPanel) { 1.78 + assertVisible(); 1.79 + 1.80 + final int expectedPanelIndex = getPanelIndexForDevice(expectedPanel.ordinal()); 1.81 + fAssertEquals("The current HomePager panel is " + expectedPanel, 1.82 + expectedPanelIndex, getHomePagerView().getCurrentItem()); 1.83 + return this; 1.84 + } 1.85 + 1.86 + public AboutHomeComponent assertNotVisible() { 1.87 + fAssertTrue("The HomePager is not visible", 1.88 + getHomePagerContainer().getVisibility() != View.VISIBLE || 1.89 + getHomePagerView().getVisibility() != View.VISIBLE); 1.90 + return this; 1.91 + } 1.92 + 1.93 + public AboutHomeComponent assertVisible() { 1.94 + fAssertTrue("The HomePager is visible", 1.95 + getHomePagerContainer().getVisibility() == View.VISIBLE && 1.96 + getHomePagerView().getVisibility() == View.VISIBLE); 1.97 + return this; 1.98 + } 1.99 + 1.100 + public AboutHomeComponent assertBannerNotVisible() { 1.101 + View banner = getHomeBannerView(); 1.102 + fAssertTrue("The HomeBanner is not visible", 1.103 + getHomePagerContainer().getVisibility() != View.VISIBLE || 1.104 + banner.getVisibility() != View.VISIBLE || 1.105 + banner.getTranslationY() == banner.getHeight()); 1.106 + return this; 1.107 + } 1.108 + 1.109 + public AboutHomeComponent assertBannerVisible() { 1.110 + fAssertTrue("The HomeBanner is visible", 1.111 + getHomePagerContainer().getVisibility() == View.VISIBLE && 1.112 + getHomeBannerView().getVisibility() == View.VISIBLE); 1.113 + return this; 1.114 + } 1.115 + 1.116 + public AboutHomeComponent assertBannerText(String text) { 1.117 + assertBannerVisible(); 1.118 + 1.119 + final TextView textView = (TextView) getHomeBannerView().findViewById(R.id.text); 1.120 + fAssertEquals("The correct HomeBanner text is shown", 1.121 + text, textView.getText().toString()); 1.122 + return this; 1.123 + } 1.124 + 1.125 + public AboutHomeComponent clickOnBanner() { 1.126 + assertBannerVisible(); 1.127 + 1.128 + mTestContext.dumpLog(LOGTAG, "Clicking on HomeBanner."); 1.129 + mSolo.clickOnView(getHomeBannerView()); 1.130 + return this; 1.131 + } 1.132 + 1.133 + public AboutHomeComponent dismissBanner() { 1.134 + assertBannerVisible(); 1.135 + 1.136 + mTestContext.dumpLog(LOGTAG, "Clicking on HomeBanner close button."); 1.137 + mSolo.clickOnView(getHomeBannerView().findViewById(R.id.close)); 1.138 + return this; 1.139 + } 1.140 + 1.141 + public AboutHomeComponent swipeToPanelOnRight() { 1.142 + mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the right."); 1.143 + swipeToPanel(Solo.RIGHT); 1.144 + return this; 1.145 + } 1.146 + 1.147 + public AboutHomeComponent swipeToPanelOnLeft() { 1.148 + mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the left."); 1.149 + swipeToPanel(Solo.LEFT); 1.150 + return this; 1.151 + } 1.152 + 1.153 + private void swipeToPanel(final int panelDirection) { 1.154 + fAssertTrue("Swiping in a valid direction", 1.155 + panelDirection == Solo.LEFT || panelDirection == Solo.RIGHT); 1.156 + assertVisible(); 1.157 + 1.158 + final int panelIndex = getHomePagerView().getCurrentItem(); 1.159 + 1.160 + mSolo.scrollViewToSide(getHomePagerView(), panelDirection, SWIPE_PERCENTAGE); 1.161 + 1.162 + // The panel on the left is a lower index and vice versa. 1.163 + final int unboundedPanelIndex = panelIndex + (panelDirection == Solo.LEFT ? -1 : 1); 1.164 + final int panelCount = DeviceHelper.isTablet() ? 1.165 + TabletPanel.values().length : PhonePanel.values().length; 1.166 + final int maxPanelIndex = panelCount - 1; 1.167 + final int expectedPanelIndex = Math.min(Math.max(0, unboundedPanelIndex), maxPanelIndex); 1.168 + 1.169 + waitForPanelIndex(expectedPanelIndex); 1.170 + } 1.171 + 1.172 + private void waitForPanelIndex(final int expectedIndex) { 1.173 + final String panelName; 1.174 + if (DeviceHelper.isTablet()) { 1.175 + panelName = TabletPanel.values()[expectedIndex].name(); 1.176 + } else { 1.177 + panelName = PhonePanel.values()[expectedIndex].name(); 1.178 + } 1.179 + 1.180 + WaitHelper.waitFor("HomePager " + panelName + " panel", new Condition() { 1.181 + @Override 1.182 + public boolean isSatisfied() { 1.183 + return (getHomePagerView().getCurrentItem() == expectedIndex); 1.184 + } 1.185 + }); 1.186 + } 1.187 + 1.188 + /** 1.189 + * Gets the panel index in the device specific Panel enum for the given index in the 1.190 + * PanelType enum. 1.191 + */ 1.192 + private int getPanelIndexForDevice(final int panelIndex) { 1.193 + final String panelName = PanelType.values()[panelIndex].name(); 1.194 + final Class devicePanelEnum = 1.195 + DeviceHelper.isTablet() ? TabletPanel.class : PhonePanel.class; 1.196 + return Enum.valueOf(devicePanelEnum, panelName).ordinal(); 1.197 + } 1.198 +}