forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#12061 - Added tests for AddressPicker
- Loading branch information
Alexandru2909
committed
May 11, 2022
1 parent
0127822
commit fe7ffe0
Showing
4 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...re/prompts/src/test/java/mozilla/components/feature/prompts/address/AddressAdapterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
package mozilla.components.feature.prompts.address | ||
|
||
import android.view.LayoutInflater | ||
import android.widget.TextView | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.concept.storage.Address | ||
import mozilla.components.feature.prompts.R | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AddressAdapterTest { | ||
|
||
private val address = Address( | ||
guid = "1", | ||
givenName = "Location", | ||
additionalName = "Location", | ||
familyName = "Location", | ||
organization = "Mozilla", | ||
streetAddress = "1230 Main st", | ||
addressLevel3 = "Location3", | ||
addressLevel2 = "Location2", | ||
addressLevel1 = "Location1", | ||
postalCode = "90237", | ||
country = "USA", | ||
tel = "00", | ||
email = "email" | ||
) | ||
|
||
@Test | ||
fun testAddressDiffCallback() { | ||
val address2 = address.copy() | ||
|
||
assertTrue( | ||
AddressDiffCallback.areItemsTheSame(address, address2) | ||
) | ||
assertTrue( | ||
AddressDiffCallback.areContentsTheSame(address, address2) | ||
) | ||
|
||
val address3 = address.copy(guid = "2") | ||
|
||
assertFalse( | ||
AddressDiffCallback.areItemsTheSame(address, address3) | ||
) | ||
assertFalse( | ||
AddressDiffCallback.areItemsTheSame(address, address3) | ||
) | ||
} | ||
|
||
@Test | ||
fun `WHEN an address is bound to the adapter THEN set the address display name`() { | ||
val view = | ||
LayoutInflater.from(testContext).inflate(R.layout.mozac_feature_prompts_address_list_item, null) | ||
val addressName: TextView = view.findViewById(R.id.address_name) | ||
|
||
AddressViewHolder(view) {}.bind(address) | ||
|
||
assertEquals(address.displayFormat(), addressName.text) | ||
} | ||
|
||
@Test | ||
fun `WHEN an address item is clicked THEN call the onAddressSelected callback`() { | ||
var addressSelected = false | ||
val view = | ||
LayoutInflater.from(testContext).inflate(R.layout.mozac_feature_prompts_address_list_item, null) | ||
val onAddressSelect: (Address) -> Unit = { addressSelected = true } | ||
|
||
AddressViewHolder(view, onAddressSelect).bind(address) | ||
view.performClick() | ||
|
||
assertTrue(addressSelected) | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...ure/prompts/src/test/java/mozilla/components/feature/prompts/address/AddressPickerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* 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 mozilla.components.feature.prompts.address | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.browser.state.state.BrowserState | ||
import mozilla.components.browser.state.state.ContentState | ||
import mozilla.components.browser.state.state.CustomTabSessionState | ||
import mozilla.components.browser.state.state.TabSessionState | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import mozilla.components.concept.engine.prompt.PromptRequest | ||
import mozilla.components.concept.storage.Address | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.whenever | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.verify | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AddressPickerTest { | ||
|
||
private lateinit var store: BrowserStore | ||
private lateinit var state: BrowserState | ||
private lateinit var addressPicker: AddressPicker | ||
private lateinit var addressSelectBar: AddressSelectBar | ||
|
||
private val address = Address( | ||
guid = "1", | ||
givenName = "Location", | ||
additionalName = "Location", | ||
familyName = "Location", | ||
organization = "Mozilla", | ||
streetAddress = "1230 Main st", | ||
addressLevel3 = "Location3", | ||
addressLevel2 = "Location2", | ||
addressLevel1 = "Location1", | ||
postalCode = "90237", | ||
country = "USA", | ||
tel = "00", | ||
email = "email" | ||
) | ||
var onDismissCalled = false | ||
var confirmedAddress: Address? = null | ||
private val promptRequest = PromptRequest.SelectAddress( | ||
addresses = listOf(address), | ||
onDismiss = { onDismissCalled = true }, | ||
onConfirm = { confirmedAddress = it } | ||
) | ||
|
||
var selectAddressCalled = false | ||
private val selectAddressCallback: () -> Unit = { selectAddressCalled = true } | ||
|
||
@Before | ||
fun setup() { | ||
store = mock() | ||
state = mock() | ||
addressSelectBar = mock() | ||
addressPicker = AddressPicker( | ||
store = store, | ||
addressSelectBar = addressSelectBar, | ||
selectAddressCallback = selectAddressCallback, | ||
) | ||
|
||
whenever(store.state).thenReturn(state) | ||
} | ||
|
||
@Test | ||
fun `WHEN onOptionSelect is called with an address THEN selectAddressCallback is invoked and prompt is hidden`() { | ||
assertNull(addressPicker.selectedAddress) | ||
val content: ContentState = mock() | ||
whenever(content.promptRequests).thenReturn(listOf(promptRequest)) | ||
val selectedTab = TabSessionState("browser-tab", content, mock(), mock()) | ||
whenever(state.selectedTabId).thenReturn(selectedTab.id) | ||
whenever(state.tabs).thenReturn(listOf(selectedTab)) | ||
|
||
addressPicker.onOptionSelect(address) | ||
|
||
verify(addressSelectBar).hidePrompt() | ||
assertTrue(selectAddressCalled) | ||
assertEquals(address, addressPicker.selectedAddress) | ||
} | ||
|
||
@Test | ||
fun `GIVEN a prompt request WHEN handleSelectAddressRequest is called THEN the prompt is shown with the provided addresses`() { | ||
addressPicker.handleSelectAddressRequest(promptRequest) | ||
|
||
verify(addressSelectBar).showPrompt(promptRequest.addresses) | ||
} | ||
|
||
@Test | ||
fun `GIVEN a custom tab and a prompt request WHEN handleSelectAddressRequest is called THEN the prompt is shown with the provided addresses`() { | ||
val customTabContent: ContentState = mock() | ||
val customTab = CustomTabSessionState("custom-tab", customTabContent, mock(), mock()) | ||
whenever(customTabContent.promptRequests).thenReturn(listOf(promptRequest)) | ||
whenever(state.customTabs).thenReturn(listOf(customTab)) | ||
|
||
addressPicker.handleSelectAddressRequest(promptRequest) | ||
|
||
verify(addressSelectBar).showPrompt(promptRequest.addresses) | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
.../prompts/src/test/java/mozilla/components/feature/prompts/address/AddressSelectBarTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* 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 mozilla.components.feature.prompts.address | ||
|
||
import android.widget.LinearLayout | ||
import androidx.appcompat.widget.AppCompatTextView | ||
import androidx.core.view.isVisible | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.concept.storage.Address | ||
import mozilla.components.feature.prompts.R | ||
import mozilla.components.feature.prompts.concept.SelectablePromptView | ||
import mozilla.components.support.test.ext.appCompatContext | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.verify | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AddressSelectBarTest { | ||
|
||
private lateinit var addressSelectBar: AddressSelectBar | ||
|
||
private val address = Address( | ||
guid = "1", | ||
givenName = "Location", | ||
additionalName = "Location", | ||
familyName = "Location", | ||
organization = "Mozilla", | ||
streetAddress = "1230 Main st", | ||
addressLevel3 = "Location3", | ||
addressLevel2 = "Location2", | ||
addressLevel1 = "Location1", | ||
postalCode = "90237", | ||
country = "USA", | ||
tel = "00", | ||
email = "email" | ||
) | ||
|
||
@Before | ||
fun setup() { | ||
addressSelectBar = AddressSelectBar(appCompatContext) | ||
} | ||
|
||
@Test | ||
fun `WHEN showPrompt is called THEN the select bar is shown`() { | ||
val addresses = listOf(address) | ||
|
||
addressSelectBar.showPrompt(addresses) | ||
|
||
assertTrue(addressSelectBar.isVisible) | ||
} | ||
|
||
@Test | ||
fun `WHEN hidePrompt is called THEN the select bar is hidden`() { | ||
assertTrue(addressSelectBar.isVisible) | ||
|
||
addressSelectBar.hidePrompt() | ||
|
||
assertFalse(addressSelectBar.isVisible) | ||
} | ||
|
||
@Test | ||
fun `WHEN the selectBar header is clicked two times THEN the list of addresses is shown, then hidden`() { | ||
addressSelectBar.showPrompt(listOf(address)) | ||
addressSelectBar.findViewById<AppCompatTextView>(R.id.select_address_header).performClick() | ||
|
||
assertTrue(addressSelectBar.findViewById<RecyclerView>(R.id.address_list).isVisible) | ||
|
||
addressSelectBar.findViewById<AppCompatTextView>(R.id.select_address_header).performClick() | ||
|
||
assertFalse(addressSelectBar.findViewById<RecyclerView>(R.id.address_list).isVisible) | ||
} | ||
|
||
@Test | ||
fun `GIVEN a listener WHEN an address is clicked THEN onOptionSelected is called`() { | ||
val listener: SelectablePromptView.Listener<Address> = mock() | ||
|
||
assertNull(addressSelectBar.listener) | ||
|
||
addressSelectBar.listener = listener | ||
|
||
addressSelectBar.showPrompt(listOf(address)) | ||
val adapter = addressSelectBar.findViewById<RecyclerView>(R.id.address_list).adapter as AddressAdapter | ||
val holder = adapter.onCreateViewHolder(LinearLayout(testContext), 0) | ||
adapter.bindViewHolder(holder, 0) | ||
|
||
holder.itemView.performClick() | ||
|
||
verify(listener).onOptionSelect(address) | ||
} | ||
} |