forked from mozilla-mobile/fenix
-
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#4776: Added Unit tests for About Page.
- Loading branch information
1 parent
c9cf20d
commit 9b0a5f0
Showing
2 changed files
with
104 additions
and
1 deletion.
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
101 changes: 101 additions & 0 deletions
101
app/src/test/java/org/mozilla/fenix/settings/about/AboutPageAdapterTest.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,101 @@ | ||
/* 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.settings.about | ||
|
||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import assertk.assertThat | ||
import assertk.assertions.isEqualTo | ||
import io.mockk.Runs | ||
import io.mockk.every | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import io.mockk.verify | ||
import kotlinx.android.synthetic.main.about_list_item.view.* | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mozilla.fenix.TestApplication | ||
import org.mozilla.fenix.settings.about.viewholders.AboutItemViewHolder | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
@Config(application = TestApplication::class) | ||
class AboutPageAdapterTest { | ||
private var aboutList: List<AboutPageItem> = | ||
mutableListOf( | ||
AboutPageItem.Item( | ||
AboutItem.ExternalLink( | ||
AboutItemType.WHATS_NEW, | ||
"https://mozilla.org" | ||
), "Libraries" | ||
), | ||
AboutPageItem.Item(AboutItem.Libraries, "Libraries") | ||
) | ||
private val listener: AboutPageListener = mockk(relaxed = true) | ||
|
||
@Test | ||
fun `updateData should set the new data and call notifyDataSetChanged()`() { | ||
val adapter = spyk(AboutPageAdapter(listener), recordPrivateCalls = true) | ||
every { adapter.notifyDataSetChanged() } just Runs | ||
|
||
adapter.updateData(aboutList) | ||
|
||
// Wasn't able to test in verify block the 'adapter.aboutList' setter from the updateData method | ||
assertThat(adapter.aboutList).isEqualTo(aboutList) | ||
verify { | ||
adapter.notifyDataSetChanged() | ||
} | ||
} | ||
|
||
@Test | ||
fun `getItemCount on a default instantiated Adapter should return 0`() { | ||
val adapter = AboutPageAdapter(listener) | ||
|
||
assertThat(adapter.itemCount).isEqualTo(0) | ||
} | ||
|
||
@Test | ||
fun `getItemCount after updateData() call should return the correct list size`() { | ||
val adapter = AboutPageAdapter(listener) | ||
|
||
adapter.updateData(aboutList) | ||
|
||
assertThat(adapter.itemCount).isEqualTo(2) | ||
} | ||
|
||
@Test | ||
fun `the adapter uses AboutItemViewHolder`() { | ||
val adapter = AboutPageAdapter(listener) | ||
val parentView: ViewGroup = mockk(relaxed = true) | ||
every { parentView.context } returns testContext | ||
|
||
val viewHolder = adapter.onCreateViewHolder(parentView, AboutItemViewHolder.LAYOUT_ID) | ||
|
||
assertThat(viewHolder::class).isEqualTo(AboutItemViewHolder::class) | ||
} | ||
|
||
@Test | ||
fun `the adapter binds the right item to a ViewHolder`() { | ||
val adapter = AboutPageAdapter(listener) | ||
val parentView: ViewGroup = mockk(relaxed = true) | ||
every { parentView.about_item_title } returns TextView(testContext) | ||
val viewHolder = spyk(AboutItemViewHolder(parentView, mockk())) | ||
every { | ||
adapter.onCreateViewHolder( | ||
parentView, | ||
AboutItemViewHolder.LAYOUT_ID | ||
) | ||
} returns viewHolder | ||
every { viewHolder.bind(any()) } just Runs | ||
|
||
adapter.updateData(aboutList) | ||
adapter.bindViewHolder(viewHolder, 1) | ||
|
||
verify { viewHolder.bind(adapter.aboutList?.get(1) as AboutPageItem.Item) } | ||
} | ||
} |