|
| 1 | +/* |
| 2 | + * Nextcloud - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com> |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +package com.owncloud.android.utils |
| 9 | + |
| 10 | +import android.app.Activity |
| 11 | +import android.app.Instrumentation |
| 12 | +import android.content.Intent |
| 13 | +import androidx.annotation.StringRes |
| 14 | +import androidx.fragment.app.DialogFragment |
| 15 | +import androidx.fragment.app.Fragment |
| 16 | +import androidx.test.core.app.launchActivity |
| 17 | +import androidx.test.espresso.Espresso.onView |
| 18 | +import androidx.test.espresso.assertion.ViewAssertions.matches |
| 19 | +import androidx.test.espresso.intent.Intents |
| 20 | +import androidx.test.espresso.intent.Intents.intending |
| 21 | +import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent |
| 22 | +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
| 23 | +import androidx.test.espresso.matcher.ViewMatchers.withText |
| 24 | +import com.google.android.material.bottomsheet.BottomSheetDialogFragment |
| 25 | +import com.nextcloud.client.onboarding.FirstRunActivity |
| 26 | +import com.nextcloud.test.TestActivity |
| 27 | +import com.owncloud.android.R |
| 28 | +import com.owncloud.android.authentication.AuthenticatorActivity |
| 29 | +import org.hamcrest.Matchers.anyOf |
| 30 | +import org.junit.After |
| 31 | +import org.junit.Before |
| 32 | +import org.junit.Test |
| 33 | + |
| 34 | +class DisplayUtilsSnackbarTest { |
| 35 | + |
| 36 | + class NormalTestFragment : Fragment() |
| 37 | + class DialogTestFragment : DialogFragment() |
| 38 | + class BottomSheetTestFragment : BottomSheetDialogFragment() |
| 39 | + |
| 40 | + @Before |
| 41 | + fun setUp() { |
| 42 | + Intents.init() |
| 43 | + val cancelledResult = Instrumentation.ActivityResult(Activity.RESULT_CANCELED, Intent()) |
| 44 | + intending( |
| 45 | + anyOf( |
| 46 | + hasComponent(AuthenticatorActivity::class.java.name), |
| 47 | + hasComponent(FirstRunActivity::class.java.name) |
| 48 | + ) |
| 49 | + ).respondWith(cancelledResult) |
| 50 | + } |
| 51 | + |
| 52 | + @After |
| 53 | + fun tearDown() { |
| 54 | + Intents.release() |
| 55 | + } |
| 56 | + |
| 57 | + private fun assertSnackbarVisible(msg: String) { |
| 58 | + onView(withText(msg)).check(matches(isDisplayed())) |
| 59 | + } |
| 60 | + |
| 61 | + private fun assertSnackbarVisible(@StringRes msgRes: Int) { |
| 62 | + onView(withText(msgRes)).check(matches(isDisplayed())) |
| 63 | + } |
| 64 | + |
| 65 | + private fun testFragmentSnackbar(fragment: Fragment, @StringRes msgRes: Int) { |
| 66 | + launchActivity<TestActivity>().use { scenario -> |
| 67 | + scenario.onActivity { sut -> |
| 68 | + sut.addFragment(fragment) |
| 69 | + } |
| 70 | + scenario.onActivity { |
| 71 | + DisplayUtils.showSnackMessage(fragment, msgRes) |
| 72 | + } |
| 73 | + assertSnackbarVisible(msgRes) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + fun testNormalFragmentSnackbar() { |
| 79 | + testFragmentSnackbar(NormalTestFragment(), R.string.app_name) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun testDialogFragmentSnackbar() { |
| 84 | + testFragmentSnackbar(DialogTestFragment(), R.string.app_name) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun testBottomSheetFragmentSnackbar() { |
| 89 | + testFragmentSnackbar(BottomSheetTestFragment(), R.string.app_name) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun testNullFragmentSnackbar_doesNotCrash() { |
| 94 | + DisplayUtils.showSnackMessage(null as Fragment?, R.string.app_name) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun testActivityStringResSnackbar() { |
| 99 | + launchActivity<TestActivity>().use { scenario -> |
| 100 | + scenario.onActivity { sut -> |
| 101 | + DisplayUtils.showSnackMessage(sut, R.string.app_name) |
| 102 | + } |
| 103 | + assertSnackbarVisible(R.string.app_name) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun testActivityStringSnackbar() { |
| 109 | + launchActivity<TestActivity>().use { scenario -> |
| 110 | + var message = "" |
| 111 | + scenario.onActivity { sut -> |
| 112 | + message = sut.getString(R.string.app_name) |
| 113 | + DisplayUtils.showSnackMessage(sut, message) |
| 114 | + } |
| 115 | + assertSnackbarVisible(message) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun testViewStringResSnackbar() { |
| 121 | + launchActivity<TestActivity>().use { scenario -> |
| 122 | + scenario.onActivity { sut -> |
| 123 | + val contentView = sut.findViewById<android.view.View>(android.R.id.content) |
| 124 | + DisplayUtils.showSnackMessage(contentView, R.string.app_name) |
| 125 | + } |
| 126 | + assertSnackbarVisible(R.string.app_name) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + fun testViewStringSnackbar() { |
| 132 | + launchActivity<TestActivity>().use { scenario -> |
| 133 | + var message = "" |
| 134 | + scenario.onActivity { sut -> |
| 135 | + message = sut.getString(R.string.app_name) |
| 136 | + val contentView = sut.findViewById<android.view.View>(android.R.id.content) |
| 137 | + DisplayUtils.showSnackMessage(contentView, message) |
| 138 | + } |
| 139 | + assertSnackbarVisible(message) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun testActivityStringResWithFormatArgsSnackbar() { |
| 145 | + launchActivity<TestActivity>().use { scenario -> |
| 146 | + scenario.onActivity { sut -> |
| 147 | + DisplayUtils.showSnackMessage(sut, R.string.app_name) |
| 148 | + } |
| 149 | + assertSnackbarVisible(R.string.app_name) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun testContextViewStringResWithFormatArgsSnackbar() { |
| 155 | + launchActivity<TestActivity>().use { scenario -> |
| 156 | + scenario.onActivity { sut -> |
| 157 | + val contentView = sut.findViewById<android.view.View>(android.R.id.content) |
| 158 | + DisplayUtils.showSnackMessage(sut, contentView, R.string.app_name) |
| 159 | + } |
| 160 | + assertSnackbarVisible(R.string.app_name) |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments