BookmarksRobot.kt 12.9 KB
Newer Older
sv-ohorvath's avatar
sv-ohorvath committed
1
2
3
4
5
6
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

@file:Suppress("TooManyFunctions")

7
8
package org.mozilla.fenix.ui.robots

sv-ohorvath's avatar
sv-ohorvath committed
9
import android.net.Uri
10
import androidx.test.espresso.Espresso.onView
11
12
import androidx.test.espresso.action.ViewActions.clearText
import androidx.test.espresso.action.ViewActions.longClick
13
import androidx.test.espresso.action.ViewActions.replaceText
sv-ohorvath's avatar
sv-ohorvath committed
14
import androidx.test.espresso.action.ViewActions.typeText
15
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
sv-ohorvath's avatar
sv-ohorvath committed
16
import androidx.test.espresso.assertion.ViewAssertions.matches
Mihai Eduard Badea's avatar
Mihai Eduard Badea committed
17
import androidx.test.espresso.matcher.RootMatchers
sv-ohorvath's avatar
sv-ohorvath committed
18
import androidx.test.espresso.matcher.ViewMatchers
19
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
cesar's avatar
cesar committed
20
21
22
23
24
25
import androidx.test.espresso.matcher.ViewMatchers.withChild
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withParent
import androidx.test.espresso.matcher.ViewMatchers.withText
26
27
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
cesar's avatar
cesar committed
28
import androidx.test.platform.app.InstrumentationRegistry
29
import androidx.test.uiautomator.By.res
cesar's avatar
cesar committed
30
import androidx.test.uiautomator.UiDevice
31
import androidx.test.uiautomator.UiSelector
32
import org.hamcrest.Matchers.allOf
sv-ohorvath's avatar
sv-ohorvath committed
33
import org.hamcrest.Matchers.containsString
cesar's avatar
cesar committed
34
import org.junit.Assert.assertEquals
35
import org.mozilla.fenix.R
36
import org.mozilla.fenix.helpers.TestAssetHelper
37
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
38
import org.mozilla.fenix.helpers.click
39
import org.mozilla.fenix.helpers.ext.waitNotNull
40

sv-ohorvath's avatar
sv-ohorvath committed
41
42
43
/**
 * Implementation of Robot Pattern for the bookmarks menu.
 */
44
45
class BookmarksRobot {

46
47
48
49
50
51
52
    fun verifyBookmarksMenuView() {
        mDevice.findObject(
            UiSelector().text("Bookmarks")
        ).waitForExists(waitingTime)

        assertBookmarksView()
    }
53

sv-ohorvath's avatar
sv-ohorvath committed
54
55
    fun verifyEmptyBookmarksList() = assertEmptyBookmarksList()

56
    fun verifyBookmarkFavicon(forUrl: Uri) = assertBookmarkFavicon(forUrl)
sv-ohorvath's avatar
sv-ohorvath committed
57

58
    fun verifyBookmarkedURL(url: String) = assertBookmarkURL(url)
sv-ohorvath's avatar
sv-ohorvath committed
59

60
    fun verifyFolderTitle(title: String) {
61
        mDevice.findObject(UiSelector().text(title)).waitForExists(waitingTime)
62
63
        assertFolderTitle(title)
    }
sv-ohorvath's avatar
sv-ohorvath committed
64

65
    fun verifyBookmarkTitle(title: String) {
66
        mDevice.findObject(UiSelector().text(title)).waitForExists(waitingTime)
67
68
        assertBookmarkTitle(title)
    }
sv-ohorvath's avatar
sv-ohorvath committed
69

70
71
72
73
74
75
76
77
78
79
80
81
82
    fun verifyDeleteSnackBarText() = assertSnackBarText("Deleted")

    fun verifyUndoDeleteSnackBarButton() = assertUndoDeleteSnackBarButton()

    fun verifySnackBarHidden() {
        mDevice.waitNotNull(
            Until.gone(By.text("UNDO")),
            TestAssetHelper.waitingTime
        )
        onView(withId(R.id.snackbar_layout)).check(doesNotExist())
    }

    fun verifyCopySnackBarText() = assertSnackBarText("URL copied")
sv-ohorvath's avatar
sv-ohorvath committed
83
84
85

    fun verifyEditBookmarksView() = assertEditBookmarksView()

cesar's avatar
cesar committed
86
    fun verifyBookmarkNameEditBox() = assertBookmarkNameEditBox()
sv-ohorvath's avatar
sv-ohorvath committed
87
88
89
90
91

    fun verifyBookmarkURLEditBox() = assertBookmarkURLEditBox()

    fun verifyParentFolderSelector() = assertBookmarkFolderSelector()

cesar's avatar
cesar committed
92
93
94
95
    fun verifyKeyboardHidden() = assertKeyboardVisibility(isExpectedToBeVisible = false)

    fun verifyKeyboardVisible() = assertKeyboardVisibility(isExpectedToBeVisible = true)

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    fun verifyShareOverlay() = assertShareOverlay()

    fun verifyShareBookmarkFavicon() = assertShareBookmarkFavicon()

    fun verifyShareBookmarkTitle() = assertShareBookmarkTitle()

    fun verifyShareBookmarkUrl() = assertShareBookmarkUrl()

    fun verifySelectDefaultFolderSnackBarText() = assertSnackBarText("Can’t edit default folders")

    fun verifyCurrentFolderTitle(title: String) {
        onView(
            allOf(
                withText(title),
                withParent(withId(R.id.navigationToolbar))
            )
        )
            .check(matches(isDisplayed()))
    }

116
117
    fun verifySignInToSyncButton() =
        signInToSyncButton().check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
118

Mihai Eduard Badea's avatar
Mihai Eduard Badea committed
119
120
    fun verifyDeleteFolderConfirmationMessage() = assertDeleteFolderConfirmationMessage()

cesar's avatar
cesar committed
121
122
123
124
125
126
    fun createFolder(name: String) {
        clickAddFolderButton()
        addNewFolderName(name)
        saveNewFolder()
    }

sv-ohorvath's avatar
sv-ohorvath committed
127
    fun clickAddFolderButton() {
128
        mDevice.waitNotNull(
129
            Until.findObject(By.desc("Add folder")),
130
131
            TestAssetHelper.waitingTime
        )
sv-ohorvath's avatar
sv-ohorvath committed
132
133
134
        addFolderButton().click()
    }

135
136
    fun clickdeleteBookmarkButton() = deleteBookmarkButton().click()

sv-ohorvath's avatar
sv-ohorvath committed
137
    fun addNewFolderName(name: String) {
138
139
        addFolderTitleField()
            .click()
140
            .perform(replaceText(name))
sv-ohorvath's avatar
sv-ohorvath committed
141
142
143
144
145
146
147
148
149
150
    }

    fun saveNewFolder() {
        saveFolderButton().click()
    }

    fun navigateUp() {
        goBackButton().click()
    }

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
    fun clickUndoDeleteButton() {
        snackBarUndoButton().click()
    }

    fun changeBookmarkTitle(newTitle: String) {
        bookmarkNameEditBox()
            .perform(clearText())
            .perform(typeText(newTitle))
    }

    fun changeBookmarkUrl(newUrl: String) {
        bookmarkURLEditBox()
            .perform(clearText())
            .perform(typeText(newUrl))
    }

    fun saveEditBookmark() {
        saveBookmarkButton().click()
169
        mDevice.findObject(UiSelector().resourceId("R.id.bookmark_list")).waitForExists(waitingTime)
170
171
172
173
174
175
176
177
    }

    fun clickParentFolderSelector() = bookmarkFolderSelector().click()

    fun selectFolder(title: String) = onView(withText(title)).click()

    fun longTapDesktopFolder(title: String) = onView(withText(title)).perform(longClick())

Mihai Eduard Badea's avatar
Mihai Eduard Badea committed
178
179
180
181
182
183
184
    fun confirmFolderDeletion() {
        onView(withText(R.string.delete_browsing_data_prompt_allow))
            .inRoot(RootMatchers.isDialog())
            .check(matches(isDisplayed()))
            .click()
    }

185
    class Transition {
186
187
        fun closeMenu(interact: HomeScreenRobot.() -> Unit): Transition {
            closeButton().click()
188

189
            HomeScreenRobot().interact()
190
            return Transition()
191
        }
sv-ohorvath's avatar
sv-ohorvath committed
192

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
        fun goBackToBrowser(interact: BrowserRobot.() -> Unit): BrowserRobot.Transition {
            closeButton().click()

            BrowserRobot().interact()
            return BrowserRobot.Transition()
        }

        fun confirmBookmarkFolderDeletionAndGoBackToBrowser(interact: BrowserRobot.() -> Unit): BrowserRobot.Transition {
            onView(withText(R.string.delete_browsing_data_prompt_allow))
                .inRoot(RootMatchers.isDialog())
                .check(matches(isDisplayed()))
                .click()

            BrowserRobot().interact()
            return BrowserRobot.Transition()
        }

210
        fun openThreeDotMenu(interact: ThreeDotMenuBookmarksRobot.() -> Unit): ThreeDotMenuBookmarksRobot.Transition {
211
            mDevice.waitNotNull(Until.findObject(res("org.mozilla.fenix.debug:id/overflow_menu")))
sv-ohorvath's avatar
sv-ohorvath committed
212
213
            threeDotMenu().click()

214
215
            ThreeDotMenuBookmarksRobot().interact()
            return ThreeDotMenuBookmarksRobot.Transition()
sv-ohorvath's avatar
sv-ohorvath committed
216
        }
cesar's avatar
cesar committed
217
218

        fun openThreeDotMenu(bookmarkTitle: String, interact: ThreeDotMenuBookmarksRobot.() -> Unit): ThreeDotMenuBookmarksRobot.Transition {
219
            mDevice.waitNotNull(Until.findObject(res("org.mozilla.fenix.debug:id/overflow_menu")))
cesar's avatar
cesar committed
220
221
222
223
224
            threeDotMenu(bookmarkTitle).click()

            ThreeDotMenuBookmarksRobot().interact()
            return ThreeDotMenuBookmarksRobot.Transition()
        }
225
226
227
228
229
230
231

        fun openThreeDotMenu(bookmarkUrl: Uri, interact: ThreeDotMenuBookmarksRobot.() -> Unit): ThreeDotMenuBookmarksRobot.Transition {
            threeDotMenu(bookmarkUrl).click()

            ThreeDotMenuBookmarksRobot().interact()
            return ThreeDotMenuBookmarksRobot.Transition()
        }
232
233
234
235
236
237
238

        fun clickSingInToSyncButton(interact: SettingsTurnOnSyncRobot.() -> Unit): SettingsTurnOnSyncRobot.Transition {
            signInToSyncButton().click()

            SettingsTurnOnSyncRobot().interact()
            return SettingsTurnOnSyncRobot.Transition()
        }
239
240
241
242
243
244
245
246
    }
}

fun bookmarksMenu(interact: BookmarksRobot.() -> Unit): BookmarksRobot.Transition {
    BookmarksRobot().interact()
    return BookmarksRobot.Transition()
}

247
248
private fun closeButton() = onView(withId(R.id.close_bookmarks))

sv-ohorvath's avatar
sv-ohorvath committed
249
250
private fun goBackButton() = onView(withContentDescription("Navigate up"))

251
252
253
254
255
256
257
258
259
private fun bookmarkFavicon(url: String) = onView(
    allOf(
        withId(R.id.favicon),
        withParent(
            withParent(
                withChild(allOf(withId(R.id.url), withText(url)))
            )
        )
    )
260
)
sv-ohorvath's avatar
sv-ohorvath committed
261

262
private fun bookmarkURL(url: String) = onView(allOf(withId(R.id.url), withText(url)))
sv-ohorvath's avatar
sv-ohorvath committed
263
264
265

private fun addFolderButton() = onView(withId(R.id.add_bookmark_folder))

266
private fun addFolderTitleField() = onView(withId(R.id.bookmarkNameEdit))
sv-ohorvath's avatar
sv-ohorvath committed
267
268
269

private fun saveFolderButton() = onView(withId(R.id.confirm_add_folder_button))

270
271
private fun deleteBookmarkButton() = onView(withId(R.id.delete_bookmark_button))

272
273
274
275
276
277
278
279
private fun threeDotMenu(bookmarkUrl: Uri) = onView(
    allOf(
        withId(R.id.overflow_menu),
        withParent(withChild(allOf(withId(R.id.url), withText(bookmarkUrl.toString()))))
    )
)

private fun threeDotMenu(bookmarkTitle: String) = onView(
cesar's avatar
cesar committed
280
281
    allOf(
        withId(R.id.overflow_menu),
282
        withParent(withChild(allOf(withId(R.id.title), withText(bookmarkTitle))))
cesar's avatar
cesar committed
283
284
285
    )
)

286
private fun threeDotMenu() = onView(withId(R.id.overflow_menu)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
sv-ohorvath's avatar
sv-ohorvath committed
287
288
289

private fun snackBarText() = onView(withId(R.id.snackbar_text))

290
291
292
293
294
295
296
297
298
299
300
301
private fun snackBarUndoButton() = onView(withId(R.id.snackbar_btn))

private fun bookmarkNameEditBox() = onView(withId(R.id.bookmarkNameEdit))

private fun bookmarkFolderSelector() = onView(withId(R.id.bookmarkParentFolderSelector))

private fun bookmarkURLEditBox() = onView(withId(R.id.bookmarkUrlEdit))

private fun saveBookmarkButton() = onView(withId(R.id.save_bookmark_button))

private fun signInToSyncButton() = onView(withId(R.id.bookmark_folders_sign_in))

302
private fun assertBookmarksView() {
sv-ohorvath's avatar
sv-ohorvath committed
303
304
    onView(
        allOf(
305
            withText("Bookmarks"),
sv-ohorvath's avatar
sv-ohorvath committed
306
307
308
309
            withParent(withId(R.id.navigationToolbar))
        )
    )
        .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
310
311
}

sv-ohorvath's avatar
sv-ohorvath committed
312
313
314
private fun assertEmptyBookmarksList() =
    onView(withId(R.id.bookmarks_empty_view)).check(matches(withText("No bookmarks here")))

315
private fun assertBookmarkFavicon(forUrl: Uri) = bookmarkFavicon(forUrl.toString()).check(
sv-ohorvath's avatar
sv-ohorvath committed
316
317
318
319
320
321
322
    matches(
        withEffectiveVisibility(
            ViewMatchers.Visibility.VISIBLE
        )
    )
)

323
324
private fun assertBookmarkURL(expectedURL: String) =
    mDevice.findObject(UiSelector().text(expectedURL))
sv-ohorvath's avatar
sv-ohorvath committed
325

326
327
328
329
330
private fun assertFolderTitle(expectedTitle: String) =
    onView(withText(expectedTitle)).check(matches(isDisplayed()))

private fun assertBookmarkTitle(expectedTitle: String) =
    onView(withText(expectedTitle)).check(matches(isDisplayed()))
sv-ohorvath's avatar
sv-ohorvath committed
331

332
333
private fun assertUndoDeleteSnackBarButton() =
    snackBarUndoButton().check(matches(withText("UNDO")))
sv-ohorvath's avatar
sv-ohorvath committed
334

335
336
private fun assertSnackBarText(text: String) =
    snackBarText().check(matches(withText(containsString(text))))
sv-ohorvath's avatar
sv-ohorvath committed
337
338
339
340
341
342
343
344
345

private fun assertEditBookmarksView() = onView(withText("Edit bookmark"))
    .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))

private fun assertBookmarkNameEditBox() =
    onView(withId(R.id.bookmarkNameEdit))
        .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))

private fun assertBookmarkFolderSelector() =
346
    onView(withId(R.id.bookmarkParentFolderSelector))
sv-ohorvath's avatar
sv-ohorvath committed
347
348
349
350
351
        .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))

private fun assertBookmarkURLEditBox() =
    onView(withId(R.id.bookmarkUrlEdit))
        .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
cesar's avatar
cesar committed
352
353

private fun assertKeyboardVisibility(isExpectedToBeVisible: Boolean) =
cesar's avatar
cesar committed
354
    assertEquals(
cesar's avatar
cesar committed
355
356
357
358
359
        isExpectedToBeVisible,
        UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
            .executeShellCommand("dumpsys input_method | grep mInputShown")
            .contains("mInputShown=true")
    )
360
361
362
363
364
365
366
367
368
369
370
371

private fun assertShareOverlay() =
    onView(withId(R.id.shareWrapper)).check(matches(ViewMatchers.isDisplayed()))

private fun assertShareBookmarkTitle() =
    onView(withId(R.id.share_tab_title)).check(matches(ViewMatchers.isDisplayed()))

private fun assertShareBookmarkFavicon() =
    onView(withId(R.id.share_tab_favicon)).check(matches(ViewMatchers.isDisplayed()))

private fun assertShareBookmarkUrl() =
    onView(withId(R.id.share_tab_url)).check(matches(isDisplayed()))
Mihai Eduard Badea's avatar
Mihai Eduard Badea committed
372
373
374
375
376

private fun assertDeleteFolderConfirmationMessage() =
    onView(withText(R.string.bookmark_delete_folder_confirmation_dialog))
        .inRoot(RootMatchers.isDialog())
        .check(matches(isDisplayed()))