diff --git a/WordPress/src/androidTest/java/org/wordpress/android/e2e/BlockEditorTests.java b/WordPress/src/androidTest/java/org/wordpress/android/e2e/BlockEditorTests.java index dd6e299d4636..6157274feaa6 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/e2e/BlockEditorTests.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/e2e/BlockEditorTests.java @@ -1,72 +1,74 @@ package org.wordpress.android.e2e; -// -//import android.Manifest.permission; -// -//import androidx.test.rule.ActivityTestRule; -//import androidx.test.rule.GrantPermissionRule; -// -//import org.junit.Before; -//import org.junit.Rule; -//import org.junit.Test; -//import org.wordpress.android.e2e.components.MasterbarComponent; -//import org.wordpress.android.e2e.pages.BlockEditorPage; -//import org.wordpress.android.e2e.pages.EditorPage; -//import org.wordpress.android.e2e.pages.MySitesPage; -//import org.wordpress.android.e2e.pages.PostPreviewPage; -//import org.wordpress.android.e2e.pages.SiteSettingsPage; -//import org.wordpress.android.support.BaseTest; -//import org.wordpress.android.ui.WPLaunchActivity; -// -//import static androidx.test.espresso.Espresso.pressBack; -//import static org.wordpress.android.support.WPSupportUtils.sleep; -// -//public class BlockEditorTests extends BaseTest { -// @Rule -// public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(WPLaunchActivity.class); -// -// @Rule -// public GrantPermissionRule mRuntimeImageAccessRule = GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE); -// -// @Before -// public void setUp() { -// logoutIfNecessary(); -// wpLogin(); -// } -// -// @Test -// public void testSwitchToClassicAndPreview() { -// String title = "Hello Espresso!"; -// -// MasterbarComponent mb = new MasterbarComponent().goToMySitesTab(); -// sleep(); -// -// MySitesPage mySitesPage = new MySitesPage(); -// mySitesPage.gotoSiteSettings(); -// -// // Set to Gutenberg. Apparently the site is defaulting to Aztec still. -// new SiteSettingsPage().toggleGutenbergSetting(); -// -// // exit the Settings page -// pressBack(); -// -// mb.clickBlogPosts(); -// -// new MySitesPage() -// .startNewPost(); -// -// BlockEditorPage blockEditorPage = new BlockEditorPage(); -// blockEditorPage.waitForTitleDisplayed(); -// -// blockEditorPage.enterTitle(title); -// -// blockEditorPage.switchToClassic(); -// -// EditorPage editorPage = new EditorPage(); -// editorPage.hasTitle(title); -// -// editorPage.previewPost(); -// sleep(); -// -// new PostPreviewPage(); -// } -//} + +import android.Manifest.permission; + +import androidx.test.rule.ActivityTestRule; +import androidx.test.rule.GrantPermissionRule; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.wordpress.android.e2e.components.MasterbarComponent; +import org.wordpress.android.e2e.pages.BlockEditorPage; +import org.wordpress.android.e2e.pages.EditorPage; +import org.wordpress.android.e2e.pages.MySitesPage; +import org.wordpress.android.e2e.pages.PostPreviewPage; +import org.wordpress.android.e2e.pages.SiteSettingsPage; +import org.wordpress.android.support.BaseTest; +import org.wordpress.android.ui.WPLaunchActivity; + +import static androidx.test.espresso.Espresso.pressBack; +import static org.wordpress.android.support.WPSupportUtils.sleep; + +public class BlockEditorTests extends BaseTest { + @Rule + public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(WPLaunchActivity.class); + + @Rule + public GrantPermissionRule mRuntimeImageAccessRule = GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE); + + @Before + public void setUp() { + logoutIfNecessary(); + wpLogin(); + } + + @Ignore("until startup times are improved or idling resources are made more reliable") + @Test + public void testSwitchToClassicAndPreview() { + String title = "Hello Espresso!"; + + MasterbarComponent mb = new MasterbarComponent().goToMySitesTab(); + sleep(); + + MySitesPage mySitesPage = new MySitesPage(); + mySitesPage.gotoSiteSettings(); + + // Set to Gutenberg. Apparently the site is defaulting to Aztec still. + new SiteSettingsPage().setEditorToGutenberg(); + + // exit the Settings page + pressBack(); + + mb.clickBlogPosts(); + + new MySitesPage() + .startNewPost(); + + BlockEditorPage blockEditorPage = new BlockEditorPage(); + blockEditorPage.waitForTitleDisplayed(); + + blockEditorPage.enterTitle(title); + + blockEditorPage.switchToClassic(); + + EditorPage editorPage = new EditorPage(); + editorPage.hasTitle(title); + + editorPage.previewPost(); + sleep(); + + new PostPreviewPage(); + } +} diff --git a/WordPress/src/androidTest/java/org/wordpress/android/e2e/EditorTests.java b/WordPress/src/androidTest/java/org/wordpress/android/e2e/EditorTests.java index 56b298ef2dc8..cf76c61a3ac6 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/e2e/EditorTests.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/e2e/EditorTests.java @@ -1,107 +1,111 @@ package org.wordpress.android.e2e; -// -//import android.Manifest.permission; -// -//import androidx.test.rule.ActivityTestRule; -//import androidx.test.rule.GrantPermissionRule; -// -//import org.junit.Before; -//import org.junit.Rule; -//import org.junit.Test; -//import org.wordpress.android.R; -//import org.wordpress.android.e2e.components.MasterbarComponent; -//import org.wordpress.android.e2e.pages.EditorPage; -//import org.wordpress.android.e2e.pages.MySitesPage; -//import org.wordpress.android.support.BaseTest; -//import org.wordpress.android.ui.WPLaunchActivity; -// -//import java.time.Instant; -// -//import static androidx.test.espresso.Espresso.onView; -//import static androidx.test.espresso.Espresso.pressBack; -//import static androidx.test.espresso.matcher.ViewMatchers.withId; -//import static androidx.test.espresso.matcher.ViewMatchers.withText; -//import static junit.framework.TestCase.assertTrue; -//import static org.wordpress.android.support.WPSupportUtils.checkViewHasText; -//import static org.wordpress.android.support.WPSupportUtils.sleep; -//import static org.wordpress.android.support.WPSupportUtils.waitForElementToNotBeDisplayed; -// -//public class EditorTests extends BaseTest { -// @Rule -// public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(WPLaunchActivity.class); -// -// @Rule -// public GrantPermissionRule mRuntimeImageAccessRule = GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE); -// -// @Before -// public void setUp() { -// logoutIfNecessary(); -// wpLogin(); -// } -// -// @Test -// public void testPublishSimplePost() { -// String title = "Hello Espresso!"; -// String content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; -// -// MasterbarComponent mb = new MasterbarComponent().goToMySitesTab(); -// sleep(); -// mb.clickBlogPosts(); -// -// new MySitesPage() -// .startNewPost(); -// -// EditorPage editorPage = new EditorPage(); -// editorPage.enterTitle(title); -// editorPage.enterContent(content); -// boolean isPublished = editorPage.publishPost(); -// assertTrue(isPublished); -// } -// -// @Test -// public void testPublishFullPost() { -// String title = "Hello Espresso!"; -// String content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " -// + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis " -// + "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."; -// String category = "Wedding"; -// long now = Instant.now().toEpochMilli(); -// String tag = "Tag " + now; -// -// MasterbarComponent mb = new MasterbarComponent().goToMySitesTab(); -// sleep(); -// mb.clickBlogPosts(); -// -// new MySitesPage() -// .startNewPost(); -// -// EditorPage editorPage = new EditorPage(); -// editorPage.enterTitle(title); -// editorPage.enterContent(content); -// editorPage.enterImage(); -// editorPage.openSettings(); -// -// editorPage.addACategory(category); -// editorPage.addATag(tag); -// editorPage.setFeaturedImage(); -// -// // ---------------------------- -// // Verify post settings data -// // ---------------------------- -// // Verify Category added -// checkViewHasText(onView(withId(R.id.post_categories)), category); -// -// // Verify tag added -// checkViewHasText(onView(withId(R.id.post_tags)), tag); -// -// // Verify the featured image added -// waitForElementToNotBeDisplayed(onView(withText(R.string.post_settings_set_featured_image))); -// -// // head back to the post -// pressBack(); -// -// // publish -// boolean isPublished = editorPage.publishPost(); -// assertTrue(isPublished); -// } -//} + +import android.Manifest.permission; + +import androidx.test.rule.ActivityTestRule; +import androidx.test.rule.GrantPermissionRule; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.wordpress.android.R; +import org.wordpress.android.e2e.components.MasterbarComponent; +import org.wordpress.android.e2e.pages.EditorPage; +import org.wordpress.android.e2e.pages.MySitesPage; +import org.wordpress.android.e2e.pages.SiteSettingsPage; +import org.wordpress.android.support.BaseTest; +import org.wordpress.android.ui.WPLaunchActivity; + +import java.time.Instant; + +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.Espresso.pressBack; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static junit.framework.TestCase.assertTrue; +import static org.wordpress.android.support.WPSupportUtils.checkViewHasText; +import static org.wordpress.android.support.WPSupportUtils.sleep; +import static org.wordpress.android.support.WPSupportUtils.waitForElementToNotBeDisplayed; + +public class EditorTests extends BaseTest { + @Rule + public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(WPLaunchActivity.class); + + @Rule + public GrantPermissionRule mRuntimeImageAccessRule = GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE); + + @Before + public void setUp() { + logoutIfNecessary(); + wpLogin(); + + MasterbarComponent mb = new MasterbarComponent().goToMySitesTab(); + sleep(); + + MySitesPage mySitesPage = new MySitesPage(); + mySitesPage.gotoSiteSettings(); + + // Set to Classic. + new SiteSettingsPage().setEditorToClassic(); + + // exit the Settings page + pressBack(); + + mb.clickBlogPosts(); + + new MySitesPage() + .startNewPost(); + } + + @Test + public void testPublishSimplePost() { + String title = "Hello Espresso!"; + String content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; + + EditorPage editorPage = new EditorPage(); + editorPage.enterTitle(title); + editorPage.enterContent(content); + boolean isPublished = editorPage.publishPost(); + assertTrue(isPublished); + } + + @Test + public void testPublishFullPost() { + String title = "Hello Espresso!"; + String content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod " + + "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud " + + "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."; + String category = "Wedding"; + long now = Instant.now().toEpochMilli(); + String tag = "Tag " + now; + + EditorPage editorPage = new EditorPage(); + editorPage.enterTitle(title); + editorPage.enterContent(content); + editorPage.enterImage(); + editorPage.openSettings(); + + editorPage.addACategory(category); + editorPage.addATag(tag); + editorPage.setFeaturedImage(); + + // ---------------------------- + // Verify post settings data + // ---------------------------- + // Verify Category added + checkViewHasText(onView(withId(R.id.post_categories)), category); + + // Verify tag added + checkViewHasText(onView(withId(R.id.post_tags)), tag); + + // Verify the featured image added + waitForElementToNotBeDisplayed(onView(withText(R.string.post_settings_set_featured_image))); + + // head back to the post + pressBack(); + + // publish + boolean isPublished = editorPage.publishPost(); + assertTrue(isPublished); + } +} diff --git a/WordPress/src/androidTest/java/org/wordpress/android/e2e/pages/SiteSettingsPage.java b/WordPress/src/androidTest/java/org/wordpress/android/e2e/pages/SiteSettingsPage.java index 05fb76c9f7e1..3bbf986c6902 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/e2e/pages/SiteSettingsPage.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/e2e/pages/SiteSettingsPage.java @@ -4,12 +4,14 @@ import org.wordpress.android.R; +import static androidx.test.espresso.Espresso.onData; import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.scrollTo; import static androidx.test.espresso.assertion.ViewAssertions.matches; +import static androidx.test.espresso.matcher.PreferenceMatchers.withTitle; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static org.wordpress.android.support.WPSupportUtils.ensureSwitchPreferenceIsChecked; public class SiteSettingsPage { private static ViewInteraction settings = onView(withText(R.string.site_settings_title_title)); @@ -18,9 +20,15 @@ public SiteSettingsPage() { settings.check(matches(isDisplayed())); } - public void toggleGutenbergSetting() { - onView(withText(R.string.site_settings_gutenberg_default_for_new_posts)) + public void setEditorToClassic() { + onData(withTitle(R.string.site_settings_gutenberg_default_for_new_posts)) .perform(scrollTo()) - .perform((click())); + .perform(ensureSwitchPreferenceIsChecked(false)); + } + + public void setEditorToGutenberg() { + onData(withTitle(R.string.site_settings_gutenberg_default_for_new_posts)) + .perform(scrollTo()) + .perform(ensureSwitchPreferenceIsChecked(true)); } } diff --git a/WordPress/src/androidTest/java/org/wordpress/android/support/WPSupportUtils.java b/WordPress/src/androidTest/java/org/wordpress/android/support/WPSupportUtils.java index cc866c4da881..8dc8e239135d 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/support/WPSupportUtils.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/support/WPSupportUtils.java @@ -6,22 +6,26 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; +import android.widget.Checkable; import androidx.recyclerview.widget.RecyclerView; import androidx.test.espresso.AmbiguousViewMatcherException; import androidx.test.espresso.Espresso; +import androidx.test.espresso.UiController; import androidx.test.espresso.ViewAction; import androidx.test.espresso.ViewInteraction; import androidx.test.espresso.action.GeneralClickAction; import androidx.test.espresso.action.GeneralLocation; import androidx.test.espresso.action.Press; import androidx.test.espresso.action.Tap; +import androidx.test.espresso.action.ViewActions; import androidx.test.espresso.matcher.ViewMatchers.Visibility; import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; import androidx.test.uiautomator.UiDevice; import androidx.test.uiautomator.UiObjectNotFoundException; import androidx.test.uiautomator.UiSelector; +import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.Matchers; @@ -55,6 +59,7 @@ import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.anyOf; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.isA; public class WPSupportUtils { @@ -134,6 +139,38 @@ public static ViewAction click(ViewAction rollbackAction) { rollbackAction); } + /** Ensures that a SwitchPreference isChecked is true or false regardless of the current value. This is useful to + * guarantee a particular resulting state where a previously toggled state may not be known. + * @param isChecked the value to set the preference to + * @return the ViewAction + */ + public static ViewAction ensureSwitchPreferenceIsChecked(final boolean isChecked) { + return new ViewAction() { + @Override public BaseMatcher getConstraints() { + return new BaseMatcher() { + @Override public boolean matches(Object item) { + return isA(Checkable.class).matches(((View) item).findViewById(android.R.id.switch_widget)); + } + + @Override public void describeTo(Description description) { + description.appendText("checkable"); + } + }; + } + + @Override public String getDescription() { + return "setChecked(" + isChecked + ")"; + } + + @Override public void perform(UiController uiController, View view) { + // perform click only if necessary + if (((Checkable) view.findViewById(android.R.id.switch_widget)).isChecked() != isChecked) { + ViewActions.click().perform(uiController, view); + } + } + }; + } + private static boolean isResourceId(String text) {