Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
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.fAssertFalse; |
michael@0 | 9 | import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertNotNull; |
michael@0 | 10 | import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue; |
michael@0 | 11 | |
michael@0 | 12 | import org.mozilla.gecko.R; |
michael@0 | 13 | import org.mozilla.gecko.tests.UITestContext; |
michael@0 | 14 | import org.mozilla.gecko.tests.helpers.DeviceHelper; |
michael@0 | 15 | import org.mozilla.gecko.tests.helpers.WaitHelper; |
michael@0 | 16 | |
michael@0 | 17 | import android.view.View; |
michael@0 | 18 | import android.widget.EditText; |
michael@0 | 19 | import android.widget.ImageButton; |
michael@0 | 20 | import android.widget.TextView; |
michael@0 | 21 | |
michael@0 | 22 | import com.jayway.android.robotium.solo.Condition; |
michael@0 | 23 | import com.jayway.android.robotium.solo.Solo; |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * A class representing any interactions that take place on the Toolbar. |
michael@0 | 27 | */ |
michael@0 | 28 | public class ToolbarComponent extends BaseComponent { |
michael@0 | 29 | public ToolbarComponent(final UITestContext testContext) { |
michael@0 | 30 | super(testContext); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | public ToolbarComponent assertIsEditing() { |
michael@0 | 34 | fAssertTrue("The toolbar is in the editing state", isEditing()); |
michael@0 | 35 | return this; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | public ToolbarComponent assertIsNotEditing() { |
michael@0 | 39 | fAssertFalse("The toolbar is not in the editing state", isEditing()); |
michael@0 | 40 | return this; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | public ToolbarComponent assertTitle(final String expected) { |
michael@0 | 44 | fAssertEquals("The Toolbar title is " + expected, expected, getTitle()); |
michael@0 | 45 | return this; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | public ToolbarComponent assertUrl(final String expected) { |
michael@0 | 49 | assertIsEditing(); |
michael@0 | 50 | fAssertEquals("The Toolbar url is " + expected, expected, getUrlEditText().getText()); |
michael@0 | 51 | return this; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * Returns the root View for the browser toolbar. |
michael@0 | 56 | */ |
michael@0 | 57 | private View getToolbarView() { |
michael@0 | 58 | return mSolo.getView(R.id.browser_toolbar); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | private EditText getUrlEditText() { |
michael@0 | 62 | return (EditText) getToolbarView().findViewById(R.id.url_edit_text); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | private View getUrlDisplayLayout() { |
michael@0 | 66 | return getToolbarView().findViewById(R.id.display_layout); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | private TextView getUrlTitleText() { |
michael@0 | 70 | return (TextView) getToolbarView().findViewById(R.id.url_bar_title); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | private ImageButton getBackButton() { |
michael@0 | 74 | DeviceHelper.assertIsTablet(); |
michael@0 | 75 | return (ImageButton) getToolbarView().findViewById(R.id.back); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | private ImageButton getForwardButton() { |
michael@0 | 79 | DeviceHelper.assertIsTablet(); |
michael@0 | 80 | return (ImageButton) getToolbarView().findViewById(R.id.forward); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Returns the View for the edit cancel button in the browser toolbar. |
michael@0 | 85 | */ |
michael@0 | 86 | private ImageButton getEditCancelButton() { |
michael@0 | 87 | return (ImageButton) getToolbarView().findViewById(R.id.edit_cancel); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | private CharSequence getTitle() { |
michael@0 | 91 | return getTitleHelper(true); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /** |
michael@0 | 95 | * Returns the title of the page. Note that this makes no assertions to Toolbar state and |
michael@0 | 96 | * may return a value that may never be visible to the user. Callers likely want to use |
michael@0 | 97 | * {@link assertTitle} instead. |
michael@0 | 98 | */ |
michael@0 | 99 | public CharSequence getPotentiallyInconsistentTitle() { |
michael@0 | 100 | return getTitleHelper(false); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | private CharSequence getTitleHelper(final boolean shouldAssertNotEditing) { |
michael@0 | 104 | if (shouldAssertNotEditing) { |
michael@0 | 105 | assertIsNotEditing(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | return getUrlTitleText().getText(); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | private boolean isEditing() { |
michael@0 | 112 | return getUrlDisplayLayout().getVisibility() != View.VISIBLE && |
michael@0 | 113 | getUrlEditText().getVisibility() == View.VISIBLE; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | public ToolbarComponent enterEditingMode() { |
michael@0 | 117 | assertIsNotEditing(); |
michael@0 | 118 | |
michael@0 | 119 | mSolo.clickOnView(getUrlTitleText(), true); |
michael@0 | 120 | |
michael@0 | 121 | waitForEditing(); |
michael@0 | 122 | WaitHelper.waitFor("UrlEditText to be input method target", new Condition() { |
michael@0 | 123 | @Override |
michael@0 | 124 | public boolean isSatisfied() { |
michael@0 | 125 | return getUrlEditText().isInputMethodTarget(); |
michael@0 | 126 | } |
michael@0 | 127 | }); |
michael@0 | 128 | |
michael@0 | 129 | return this; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | public ToolbarComponent commitEditingMode() { |
michael@0 | 133 | assertIsEditing(); |
michael@0 | 134 | |
michael@0 | 135 | WaitHelper.waitForPageLoad(new Runnable() { |
michael@0 | 136 | @Override |
michael@0 | 137 | public void run() { |
michael@0 | 138 | mSolo.sendKey(Solo.ENTER); |
michael@0 | 139 | } |
michael@0 | 140 | }); |
michael@0 | 141 | waitForNotEditing(); |
michael@0 | 142 | |
michael@0 | 143 | return this; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | public ToolbarComponent dismissEditingMode() { |
michael@0 | 147 | assertIsEditing(); |
michael@0 | 148 | |
michael@0 | 149 | // Cancel Button not implemeneted in tablet. |
michael@0 | 150 | if (DeviceHelper.isTablet()) { |
michael@0 | 151 | if (getUrlEditText().isInputMethodTarget()) { |
michael@0 | 152 | // Drop the soft keyboard. |
michael@0 | 153 | // TODO: Solo.hideSoftKeyboard() does not clear focus, causing unexpected |
michael@0 | 154 | // behavior, but we may want to use it over goBack(). |
michael@0 | 155 | mSolo.goBack(); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | mSolo.goBack(); |
michael@0 | 159 | } else { |
michael@0 | 160 | mSolo.clickOnView(getEditCancelButton()); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | waitForNotEditing(); |
michael@0 | 164 | |
michael@0 | 165 | return this; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | public ToolbarComponent enterUrl(final String url) { |
michael@0 | 169 | fAssertNotNull("url is not null", url); |
michael@0 | 170 | |
michael@0 | 171 | assertIsEditing(); |
michael@0 | 172 | |
michael@0 | 173 | final EditText urlEditText = getUrlEditText(); |
michael@0 | 174 | fAssertTrue("The UrlEditText is the input method target", |
michael@0 | 175 | urlEditText.isInputMethodTarget()); |
michael@0 | 176 | |
michael@0 | 177 | mSolo.clearEditText(urlEditText); |
michael@0 | 178 | mSolo.enterText(urlEditText, url); |
michael@0 | 179 | |
michael@0 | 180 | return this; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | public ToolbarComponent pressBackButton() { |
michael@0 | 184 | final ImageButton backButton = getBackButton(); |
michael@0 | 185 | return pressButton(backButton, "back"); |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | public ToolbarComponent pressForwardButton() { |
michael@0 | 189 | final ImageButton forwardButton = getForwardButton(); |
michael@0 | 190 | return pressButton(forwardButton, "forward"); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | private ToolbarComponent pressButton(final View view, final String buttonName) { |
michael@0 | 194 | fAssertNotNull("The " + buttonName + " button View is not null", view); |
michael@0 | 195 | fAssertTrue("The " + buttonName + " button is enabled", view.isEnabled()); |
michael@0 | 196 | fAssertEquals("The " + buttonName + " button is visible", |
michael@0 | 197 | View.VISIBLE, view.getVisibility()); |
michael@0 | 198 | assertIsNotEditing(); |
michael@0 | 199 | |
michael@0 | 200 | WaitHelper.waitForPageLoad(new Runnable() { |
michael@0 | 201 | @Override |
michael@0 | 202 | public void run() { |
michael@0 | 203 | mSolo.clickOnView(view); |
michael@0 | 204 | } |
michael@0 | 205 | }); |
michael@0 | 206 | |
michael@0 | 207 | return this; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | private void waitForEditing() { |
michael@0 | 211 | WaitHelper.waitFor("Toolbar to enter editing mode", new Condition() { |
michael@0 | 212 | @Override |
michael@0 | 213 | public boolean isSatisfied() { |
michael@0 | 214 | return isEditing(); |
michael@0 | 215 | } |
michael@0 | 216 | }); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | private void waitForNotEditing() { |
michael@0 | 220 | WaitHelper.waitFor("Toolbar to exit editing mode", new Condition() { |
michael@0 | 221 | @Override |
michael@0 | 222 | public boolean isSatisfied() { |
michael@0 | 223 | return !isEditing(); |
michael@0 | 224 | } |
michael@0 | 225 | }); |
michael@0 | 226 | } |
michael@0 | 227 | } |