///* 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/. */
//
//package org.mozilla.fenix.ui
//
//import androidx.test.espresso.NoMatchingViewException
//import androidx.test.platform.app.InstrumentationRegistry
//import androidx.test.uiautomator.By
//import androidx.test.uiautomator.UiDevice
//import androidx.test.uiautomator.Until
//import okhttp3.mockwebserver.MockWebServer
//import org.junit.After
//import org.junit.Before
//import org.junit.Ignore
//import org.junit.Rule
//import org.junit.Test
//import org.mozilla.fenix.helpers.AndroidAssetDispatcher
//import org.mozilla.fenix.helpers.HomeActivityTestRule
//import org.mozilla.fenix.helpers.TestAssetHelper
//import org.mozilla.fenix.ui.robots.homeScreen
//import org.mozilla.fenix.ui.robots.navigationToolbar
//
///**
// *  Tests for verifying basic functionality of tab collection
// *
// */
//
//class CollectionTest {
//    /* ktlint-disable no-blank-line-before-rbrace */ // This imposes unreadable grouping.
//
//    private val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
//    private lateinit var mockWebServer: MockWebServer
//    private val firstCollectionName = "testcollection_1"
//    private val secondCollectionName = "testcollection_2"
//
//    @get:Rule
//    val activityTestRule = HomeActivityTestRule()
//
//    @Before
//    fun setUp() {
//        mockWebServer = MockWebServer().apply {
//            setDispatcher(AndroidAssetDispatcher())
//            start()
//        }
//    }
//
//    @After
//    fun tearDown() {
//        mockWebServer.shutdown()
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    // open a webpage, and add currently opened tab to existing collection
//    fun addTabToExistingCollectionTest() {
//        val firstWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
//        val secondWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 2)
//
//        createCollection(firstCollectionName)
//
//        homeScreen {
////            verifyExistingTabList()
//            closeTab()
//        }.openNavigationToolbar {
//        }.enterURLAndEnterToBrowser(secondWebPage.url) {
//            verifyPageContent(secondWebPage.content)
//        }.openThreeDotMenu {
//            clickBrowserViewSaveCollectionButton()
//        }.selectExistingCollection(firstCollectionName) {
//            verifySnackBarText("Tab saved!")
//        }.openHomeScreen {
////            verifyExistingTabList()
//            expandCollection(firstCollectionName)
//            verifyItemInCollectionExists(firstWebPage.title)
//            verifyItemInCollectionExists(secondWebPage.title)
//        }
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    fun collectionMenuAddTabButtonTest() {
//        val secondWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 2)
//
//        createCollection(firstCollectionName)
//
//        homeScreen {
//            closeTab()
//        }.openNavigationToolbar {
//        }.enterURLAndEnterToBrowser(secondWebPage.url) {
//        }.openHomeScreen {
//            expandCollection(firstCollectionName)
//            clickCollectionThreeDotButton()
//            selectAddTabToCollection()
//            verifyTabsSelectedCounterText(1)
//            saveTabsSelectedForCollection()
//            verifySnackBarText("Tab saved!")
//            verifyItemInCollectionExists(secondWebPage.title)
//        }
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    fun renameCollectionTest() {
//        createCollection(firstCollectionName)
//
//        homeScreen {
//            // On homeview, tap the 3-dot button to expand, select rename, rename collection
//            expandCollection(firstCollectionName)
//            clickCollectionThreeDotButton()
//            selectRenameCollection()
//            typeCollectionName("renamed_collection")
//            verifyCollectionIsDisplayed("renamed_collection")
//        }
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    fun createCollectionFromTabTest() {
//        val firstWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
//
//        createCollection(firstCollectionName)
//        homeScreen {
//        }.openTabDrawer {
//            verifyExistingOpenTabs(firstWebPage.title)
//        }.openHomeScreen {
//            try {
//                verifyCollectionIsDisplayed(firstCollectionName)
//            } catch (e: NoMatchingViewException) {
//                scrollToElementByText(firstCollectionName)
//            }
//        }
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    fun removeTabFromCollectionTest() {
//        val webPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
//
//        createCollection(firstCollectionName)
//        homeScreen {
//            closeTab()
//            expandCollection(firstCollectionName)
//            removeTabFromCollection(webPage.title)
//            verifyItemInCollectionExists(webPage.title, false)
//        }
//
//        createCollection(firstCollectionName)
//        homeScreen {
//            closeTab()
//            expandCollection(firstCollectionName)
//            swipeCollectionItemLeft(webPage.title)
//            verifyItemInCollectionExists(webPage.title, false)
//        }
//
//        createCollection(firstCollectionName)
//        homeScreen {
//            closeTab()
//            expandCollection(firstCollectionName)
//            swipeCollectionItemRight(webPage.title)
//            verifyItemInCollectionExists(webPage.title, false)
//        }
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    fun selectTabOnLongTapTest() {
//        val firstWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
//        val secondWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 2)
//
//        navigationToolbar {
//        }.enterURLAndEnterToBrowser(firstWebPage.url) {
//        }.openHomeScreen {
//        }.openNavigationToolbar {
//        }.enterURLAndEnterToBrowser(secondWebPage.url) {
//        }.openHomeScreen {
//            longTapSelectTab(firstWebPage.title)
//            verifySelectTabsView()
//            verifyTabsSelectedCounterText(1)
//            selectTabForCollection(secondWebPage.title)
//            verifyTabsSelectedCounterText(2)
//            saveTabsSelectedForCollection()
//            typeCollectionName(firstCollectionName)
//            verifySnackBarText("Tabs saved!")
////            closeTabViaXButton(firstWebPage.title)
////            closeTabViaXButton(secondWebPage.title)
//            expandCollection(firstCollectionName)
//            verifyItemInCollectionExists(firstWebPage.title)
//            verifyItemInCollectionExists(secondWebPage.title)
//        }
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    fun tabsOverflowMenuSaveCollectionTest() {
//        val firstWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
//        val secondWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 2)
//
//        navigationToolbar {
//        }.enterURLAndEnterToBrowser(firstWebPage.url) {
//        }.openHomeScreen {
//        }.openNavigationToolbar {
//        }.enterURLAndEnterToBrowser(secondWebPage.url) {
//        }.openHomeScreen {
//        }.openTabsListThreeDotMenu {
//            verifySaveCollection()
//        }.clickOpenTabsMenuSaveCollection {
//            verifySelectTabsView()
//            verifyTabsSelectedCounterText(0)
//            selectAllTabsForCollection()
//            verifyTabsSelectedCounterText(2)
//            saveTabsSelectedForCollection()
//            typeCollectionName(firstCollectionName)
////            closeTabViaXButton(firstWebPage.title)
////            closeTabViaXButton(secondWebPage.title)
//            expandCollection(firstCollectionName)
//            verifyItemInCollectionExists(firstWebPage.title)
//            verifyItemInCollectionExists(secondWebPage.title)
//        }
//    }
//
//    @Ignore("Intermittent failures, see: https://github.com/mozilla-mobile/fenix/issues/10587")
//    @Test
//    fun navigateBackInCollectionFlowTest() {
//        val secondWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 2)
//
//        createCollection(firstCollectionName)
//        navigationToolbar {
//        }.enterURLAndEnterToBrowser(secondWebPage.url) {
//        }.openHomeScreen {
//            longTapSelectTab(secondWebPage.title)
//            verifySelectTabsView()
//            saveTabsSelectedForCollection()
//            verifySelectCollectionView()
//            clickAddNewCollection()
//            verifyNameCollectionView()
//            goBackCollectionFlow()
//            verifySelectCollectionView()
//            goBackCollectionFlow()
//            verifySelectTabsView()
//            goBackCollectionFlow()
//            verifyHomeComponent()
//        }
//    }
//
//    private fun createCollection(collectionName: String, firstCollection: Boolean = true) {
//        val firstWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
//
//        navigationToolbar {
//        }.enterURLAndEnterToBrowser(firstWebPage.url) {
//            verifyPageContent(firstWebPage.content)
//        }.openThreeDotMenu {
//            clickBrowserViewSaveCollectionButton()
//            if (!firstCollection)
//                clickAddNewCollection()
//
//        }.typeCollectionName(collectionName) {
//            verifySnackBarText("Tab saved!")
//        }.openHomeScreen {
//            mDevice.wait(
//                Until.findObject(By.text(collectionName)),
//                TestAssetHelper.waitingTime
//            )
//        }
//    }
//}
