michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.tests.components; michael@0: michael@0: import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertEquals; michael@0: import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.tests.UITestContext; michael@0: import org.mozilla.gecko.tests.helpers.DeviceHelper; michael@0: import org.mozilla.gecko.tests.helpers.WaitHelper; michael@0: michael@0: import android.support.v4.view.ViewPager; michael@0: import android.view.View; michael@0: import android.widget.TextView; michael@0: michael@0: import com.jayway.android.robotium.solo.Condition; michael@0: import com.jayway.android.robotium.solo.Solo; michael@0: michael@0: /** michael@0: * A class representing any interactions that take place on the Awesomescreen. michael@0: */ michael@0: public class AboutHomeComponent extends BaseComponent { michael@0: private static final String LOGTAG = AboutHomeComponent.class.getSimpleName(); michael@0: michael@0: // The different types of panels that can be present on about:home michael@0: public enum PanelType { michael@0: HISTORY, michael@0: TOP_SITES, michael@0: BOOKMARKS, michael@0: READING_LIST michael@0: } michael@0: michael@0: // TODO: Having a specific ordering of panels is prone to fail and thus temporary. michael@0: // Hopefully the work in bug 940565 will alleviate the need for these enums. michael@0: // Explicit ordering of HomePager panels on a phone. michael@0: private enum PhonePanel { michael@0: HISTORY, michael@0: TOP_SITES, michael@0: BOOKMARKS, michael@0: READING_LIST michael@0: } michael@0: michael@0: // Explicit ordering of HomePager panels on a tablet. michael@0: private enum TabletPanel { michael@0: TOP_SITES, michael@0: BOOKMARKS, michael@0: READING_LIST, michael@0: HISTORY michael@0: } michael@0: michael@0: // The percentage of the panel to swipe between 0 and 1. This value was set through michael@0: // testing: 0.55f was tested on try and fails on armv6 devices. michael@0: private static final float SWIPE_PERCENTAGE = 0.70f; michael@0: michael@0: public AboutHomeComponent(final UITestContext testContext) { michael@0: super(testContext); michael@0: } michael@0: michael@0: private View getHomePagerContainer() { michael@0: return mSolo.getView(R.id.home_pager_container); michael@0: } michael@0: michael@0: private ViewPager getHomePagerView() { michael@0: return (ViewPager) mSolo.getView(R.id.home_pager); michael@0: } michael@0: michael@0: private View getHomeBannerView() { michael@0: return mSolo.getView(R.id.home_banner); michael@0: } michael@0: michael@0: public AboutHomeComponent assertCurrentPanel(final PanelType expectedPanel) { michael@0: assertVisible(); michael@0: michael@0: final int expectedPanelIndex = getPanelIndexForDevice(expectedPanel.ordinal()); michael@0: fAssertEquals("The current HomePager panel is " + expectedPanel, michael@0: expectedPanelIndex, getHomePagerView().getCurrentItem()); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent assertNotVisible() { michael@0: fAssertTrue("The HomePager is not visible", michael@0: getHomePagerContainer().getVisibility() != View.VISIBLE || michael@0: getHomePagerView().getVisibility() != View.VISIBLE); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent assertVisible() { michael@0: fAssertTrue("The HomePager is visible", michael@0: getHomePagerContainer().getVisibility() == View.VISIBLE && michael@0: getHomePagerView().getVisibility() == View.VISIBLE); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent assertBannerNotVisible() { michael@0: View banner = getHomeBannerView(); michael@0: fAssertTrue("The HomeBanner is not visible", michael@0: getHomePagerContainer().getVisibility() != View.VISIBLE || michael@0: banner.getVisibility() != View.VISIBLE || michael@0: banner.getTranslationY() == banner.getHeight()); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent assertBannerVisible() { michael@0: fAssertTrue("The HomeBanner is visible", michael@0: getHomePagerContainer().getVisibility() == View.VISIBLE && michael@0: getHomeBannerView().getVisibility() == View.VISIBLE); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent assertBannerText(String text) { michael@0: assertBannerVisible(); michael@0: michael@0: final TextView textView = (TextView) getHomeBannerView().findViewById(R.id.text); michael@0: fAssertEquals("The correct HomeBanner text is shown", michael@0: text, textView.getText().toString()); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent clickOnBanner() { michael@0: assertBannerVisible(); michael@0: michael@0: mTestContext.dumpLog(LOGTAG, "Clicking on HomeBanner."); michael@0: mSolo.clickOnView(getHomeBannerView()); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent dismissBanner() { michael@0: assertBannerVisible(); michael@0: michael@0: mTestContext.dumpLog(LOGTAG, "Clicking on HomeBanner close button."); michael@0: mSolo.clickOnView(getHomeBannerView().findViewById(R.id.close)); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent swipeToPanelOnRight() { michael@0: mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the right."); michael@0: swipeToPanel(Solo.RIGHT); michael@0: return this; michael@0: } michael@0: michael@0: public AboutHomeComponent swipeToPanelOnLeft() { michael@0: mTestContext.dumpLog(LOGTAG, "Swiping to the panel on the left."); michael@0: swipeToPanel(Solo.LEFT); michael@0: return this; michael@0: } michael@0: michael@0: private void swipeToPanel(final int panelDirection) { michael@0: fAssertTrue("Swiping in a valid direction", michael@0: panelDirection == Solo.LEFT || panelDirection == Solo.RIGHT); michael@0: assertVisible(); michael@0: michael@0: final int panelIndex = getHomePagerView().getCurrentItem(); michael@0: michael@0: mSolo.scrollViewToSide(getHomePagerView(), panelDirection, SWIPE_PERCENTAGE); michael@0: michael@0: // The panel on the left is a lower index and vice versa. michael@0: final int unboundedPanelIndex = panelIndex + (panelDirection == Solo.LEFT ? -1 : 1); michael@0: final int panelCount = DeviceHelper.isTablet() ? michael@0: TabletPanel.values().length : PhonePanel.values().length; michael@0: final int maxPanelIndex = panelCount - 1; michael@0: final int expectedPanelIndex = Math.min(Math.max(0, unboundedPanelIndex), maxPanelIndex); michael@0: michael@0: waitForPanelIndex(expectedPanelIndex); michael@0: } michael@0: michael@0: private void waitForPanelIndex(final int expectedIndex) { michael@0: final String panelName; michael@0: if (DeviceHelper.isTablet()) { michael@0: panelName = TabletPanel.values()[expectedIndex].name(); michael@0: } else { michael@0: panelName = PhonePanel.values()[expectedIndex].name(); michael@0: } michael@0: michael@0: WaitHelper.waitFor("HomePager " + panelName + " panel", new Condition() { michael@0: @Override michael@0: public boolean isSatisfied() { michael@0: return (getHomePagerView().getCurrentItem() == expectedIndex); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Gets the panel index in the device specific Panel enum for the given index in the michael@0: * PanelType enum. michael@0: */ michael@0: private int getPanelIndexForDevice(final int panelIndex) { michael@0: final String panelName = PanelType.values()[panelIndex].name(); michael@0: final Class devicePanelEnum = michael@0: DeviceHelper.isTablet() ? TabletPanel.class : PhonePanel.class; michael@0: return Enum.valueOf(devicePanelEnum, panelName).ordinal(); michael@0: } michael@0: }