Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.freshdigitable.sample
import android.content.Context
import android.os.Looper.getMainLooper
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.TextView
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.LooperMode
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class CustomViewTest {
@Test
fun hoge() {
val context = ApplicationProvider.getApplicationContext<Context>()
val sut = HogeView(context)
val adapter = mockk<HogeView.Adapter>()
sut.adapter = adapter
sut.addView(TextView(context))
assertThat(sut.childCount).isEqualTo(1)
verify(exactly = 0) { adapter.onMeasure() }
verify(exactly = 0) { adapter.onLayout() }
}
@Test
fun hoge2() {
val context = ApplicationProvider.getApplicationContext<Context>()
val parent = FrameLayout(context)
val sut = HogeView(context)
val adapter = mockk<HogeView.Adapter>()
sut.adapter = adapter
parent.addView(sut)
sut.addView(TextView(context))
assertThat(sut.childCount).isEqualTo(1)
verify(exactly = 0) { adapter.onMeasure() }
verify(exactly = 0) { adapter.onLayout() }
}
@Test
fun hoge3() {
ActivityScenario.launch(MainActivity::class.java).moveToState(Lifecycle.State.RESUMED)
.onActivity {
val sut = HogeView(it)
val adapter = mockk<HogeView.Adapter>().apply {
every { onMeasure() } just runs
every { onLayout() } just runs
}
sut.adapter = adapter
it.setContentView(sut)
shadowOf(getMainLooper()).idle()
sut.addView(TextView(it))
shadowOf(getMainLooper()).idle()
assertThat(sut.childCount).isEqualTo(1)
verify(exactly = 2) { adapter.onMeasure() }
verify(exactly = 2) { adapter.onLayout() }
}
}
@Test
fun hoge4() {
ActivityScenario.launch(MainActivity::class.java)
.onActivity {
val sut = HogeView(it)
val adapter = mockk<HogeView.Adapter>().apply {
every { onMeasure() } just runs
every { onLayout() } just runs
}
sut.adapter = adapter
it.setContentView(sut)
shadowOf(getMainLooper()).idle()
sut.addView(TextView(it))
sut.addView(TextView(it))
shadowOf(getMainLooper()).idle()
assertThat(sut.childCount).isEqualTo(2)
verify(exactly = 2) { adapter.onMeasure() }
verify(exactly = 2) { adapter.onLayout() }
}
}
@Test
@LooperMode(LooperMode.Mode.LEGACY)
fun hoge5() {
ActivityScenario.launch(MainActivity::class.java).moveToState(Lifecycle.State.RESUMED)
.onActivity {
val sut = HogeView(it)
val adapter = mockk<HogeView.Adapter>().apply {
every { onMeasure() } just runs
every { onLayout() } just runs
}
sut.adapter = adapter
it.setContentView(sut)
sut.addView(TextView(it))
sut.addView(TextView(it))
assertThat(sut.childCount).isEqualTo(2)
verify(exactly = 3) { adapter.onMeasure() }
verify(exactly = 3) { adapter.onLayout() }
}
}
}
class HogeView(context: Context) : ViewGroup(context) {
var adapter: Adapter? = null
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
adapter?.onMeasure()
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
adapter?.onLayout()
}
interface Adapter {
fun onMeasure()
fun onLayout()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment