Skip to content

Commit

Permalink
Write unit tests for the demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelkehinde committed Mar 31, 2020
1 parent e95c279 commit 0fb18a7
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 14 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Testing
testImplementation 'org.mockito:mockito-core:2.19.0'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class StreamPlayerActivity : AppCompatActivity() {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

setContentView(R.layout.activity_stream_player)
//Fetch the VideoUrl to play
streamPlayerViewModel.fetchVideoUrl()

streamPlayerViewModel.videoUrl.observe(this, Observer {
if (it.isNotEmpty()) {
Expand Down Expand Up @@ -62,7 +64,7 @@ class StreamPlayerActivity : AppCompatActivity() {
}

private fun initializeBuffView() {
buffView.setupWithStream("stream_id")
buffView.setupWithStream("STREAM_ID")
buffView.addListener(object : EventListener {
override fun onBuffDisplayed(buff: Buff) {
super.onBuffDisplayed(buff)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ class StreamPlayerViewModel: ViewModel() {
val videoUrl: LiveData<String>
get() = _videoUrl

init {
fetchVideoUrl()
}

private fun fetchVideoUrl() {
fun fetchVideoUrl() {
_videoUrl.value = VIDEO_URL
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_stream_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
android:layout_height="wrap_content"
android:id="@+id/buffView"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"/>
android:layout_alignParentBottom="true" />

</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.emmanuelkehinde.buffdemo

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.Observer
import com.emmanuelkehinde.buffdemo.helpers.VIDEO_URL
import com.emmanuelkehinde.buffdemo.ui.stream_player.StreamPlayerViewModel
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnitRunner


/**
* Created by Emmanuel Kehinde on 2020-03-31.
*/
@RunWith(MockitoJUnitRunner::class)
class StreamPlayerViewModelTest {

@Rule
@JvmField
val instantTaskExecutorRule = InstantTaskExecutorRule()

@Mock
lateinit var observer: Observer<String>

private lateinit var streamPlayerViewModel: StreamPlayerViewModel

@Before
fun setUp() {
streamPlayerViewModel = StreamPlayerViewModel() //
}

@Test
fun `videoUrl has an observer`() {
streamPlayerViewModel.videoUrl.observeForever(observer)
assert(streamPlayerViewModel.videoUrl.hasObservers())
}

@Test
fun `fetchVideoUrl sets correct value for videoUrl`() {
streamPlayerViewModel.fetchVideoUrl()
assert(streamPlayerViewModel.videoUrl.value == VIDEO_URL)
}

@Test
fun `fetchVideoUrl should trigger the observer for videoUrl`() {
streamPlayerViewModel.videoUrl.observeForever(observer)
streamPlayerViewModel.fetchVideoUrl()
verify(observer).onChanged(VIDEO_URL)
}
}
5 changes: 1 addition & 4 deletions buffup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
Expand All @@ -48,9 +48,6 @@ dependencies {
//CircleImageView
implementation 'de.hdodenhof:circleimageview:3.1.0'

//CircularCountdown
implementation 'com.github.douglasspgyn:CircularCountdown:0.3.0'

//Image Loading
implementation 'com.squareup.picasso:picasso:2.71828'
}
Binary file added buffup/libs/circular-countdown.aar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class BuffView(context: Context, attrs: AttributeSet): LinearLayout(context, att
for (answer in answers) {
val answerRowView = inflateAnswerRowView(answersLayout) as? MotionLayout

// Set Answer index image
// Set answer index image
val indexImageView = answerRowView?.findViewById<CircleImageView>(R.id.answerImage)
Picasso.get().load(answer.imageModel.image1?.url).into(indexImageView)

Expand Down
2 changes: 1 addition & 1 deletion buffup/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<dimen name="buff_countdown_timer_text_size">12sp</dimen>
<dimen name="buff_question_answer_layout_corner_radius">30dp</dimen>
<dimen name="buff_buffview_layout_margin">20dp</dimen>
<dimen name="buff_answer_row_margin_bottom">3dp</dimen>
<dimen name="buff_answer_row_margin_bottom">4dp</dimen>
<dimen name="buff_header_view_margin_bottom">35dp</dimen>
<dimen name="buff_author_image_width">28dp</dimen>
<dimen name="buff_author_image_height">28dp</dimen>
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}

Expand Down

0 comments on commit 0fb18a7

Please sign in to comment.