Skip to content

Commit

Permalink
chore(git): merge pull request #559 from robercoding/fix-issue-537
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshgngwr authored Mar 15, 2021
2 parents cd5d76d + b754101 commit feb0e7e
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 216 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ dependencies {
implementation 'com.google.android:flexbox:2.0.1'
implementation 'com.google.android.material:material:1.3.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.hopenlib.library:flextools:1.0.1'
implementation 'io.noties.markwon:core:4.6.2'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.github.ashutoshgngwr.noice.fragment

import androidx.fragment.app.testing.FragmentScenario
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.ashutoshgngwr.noice.InAppReviewFlowManager
import com.github.ashutoshgngwr.noice.MediaPlayerService
import com.github.ashutoshgngwr.noice.R
import com.github.ashutoshgngwr.noice.RetryTestRule
import com.github.ashutoshgngwr.noice.sound.Preset
import com.github.ashutoshgngwr.noice.sound.Sound
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import io.mockk.verify
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class RandomPresetFragmentTest {

@Rule
@JvmField
val retryTestRule = RetryTestRule(5)

private lateinit var fragmentScenario: FragmentScenario<RandomPresetFragment>

@Before
fun setup() {
mockkObject(InAppReviewFlowManager, MediaPlayerService.Companion)
fragmentScenario = launchFragmentInContainer(null, R.style.Theme_App)
}

@After
fun teardown() {
unmockkAll()
}

@Test
fun testRandomPresetButton_onClick() {
val intensityExpectations = mapOf(
R.id.preset_intensity__any to SoundLibraryFragment.RANGE_INTENSITY_ANY,
R.id.preset_intensity__dense to SoundLibraryFragment.RANGE_INTENSITY_DENSE,
R.id.preset_intensity__light to SoundLibraryFragment.RANGE_INTENSITY_LIGHT
)

val typeExpectations = mapOf(
R.id.preset_type__any to null,
R.id.preset_type__focus to Sound.Tag.FOCUS,
R.id.preset_type__relax to Sound.Tag.RELAX
)

for ((typeID, tag) in typeExpectations) {
for ((intensityID, intensityRange) in intensityExpectations) {
Espresso.onView(withId(typeID)).perform(ViewActions.click())
Espresso.onView(withId(intensityID)).perform(ViewActions.click())

Espresso.onView(withId(R.id.play_preset_button))
.perform(ViewActions.click())

verify(exactly = 1) { MediaPlayerService.playRandomPreset(any(), tag, intensityRange) }
clearMocks(MediaPlayerService.Companion)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import com.github.ashutoshgngwr.noice.MediaPlayerService
import com.github.ashutoshgngwr.noice.R
import com.github.ashutoshgngwr.noice.RetryTestRule
import com.github.ashutoshgngwr.noice.sound.Preset
import com.github.ashutoshgngwr.noice.sound.Sound
import com.github.ashutoshgngwr.noice.sound.player.Player
import com.github.ashutoshgngwr.noice.sound.player.PlayerManager
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
Expand Down Expand Up @@ -302,74 +300,4 @@ class SoundLibraryFragmentTest {

verify(exactly = 1) { InAppReviewFlowManager.maybeAskForReview(any()) }
}

@Test
fun testRandomPresetButton_onPlayback() {
val mockUpdateEvent = mockk<MediaPlayerService.OnPlayerManagerUpdateEvent>(relaxed = true) {
every { players } returns mockk(relaxed = true)
}

for (state in PlayerManager.State.values()) {
every { mockUpdateEvent.state } returns state
fragmentScenario.onFragment {
it.onPlayerManagerUpdate(mockUpdateEvent)
}

// a non-stopped state should keep the FAB hidden
var expectedVisibility = Visibility.GONE
if (state == PlayerManager.State.STOPPED) {
expectedVisibility = Visibility.VISIBLE
}

onView(withId(R.id.random_preset_button))
.check(matches(withEffectiveVisibility(expectedVisibility)))
}
}

@Test
fun testRandomPresetButton_onClick() {
mockkObject(Preset.Companion)
fragmentScenario.onFragment { fragment ->
fragment.onPlayerManagerUpdate(mockk(relaxed = true) {
every { state } returns PlayerManager.State.STOPPED
})
}

val intensityExpectations = mapOf(
R.id.preset_intensity__any to SoundLibraryFragment.RANGE_INTENSITY_ANY,
R.id.preset_intensity__dense to SoundLibraryFragment.RANGE_INTENSITY_DENSE,
R.id.preset_intensity__light to SoundLibraryFragment.RANGE_INTENSITY_LIGHT
)

val typeExpectations = mapOf(
R.id.preset_type__any to null,
R.id.preset_type__focus to Sound.Tag.FOCUS,
R.id.preset_type__relax to Sound.Tag.RELAX
)

for ((typeID, tag) in typeExpectations) {
for ((intensityID, intensityRange) in intensityExpectations) {
val mockPreset = mockk<Preset>(relaxed = true)
every { Preset.generateRandom(tag, intensityRange) } returns mockPreset

onView(withId(R.id.random_preset_button))
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
.perform(click())

onView(allOf(withId(R.id.title), withText(R.string.random_preset)))
.check(matches(isDisplayed()))
// swipeUp to reveal bottom sheet completely; click to release the tap from swipeUp..?!!
// first click after swipeUp wasn't working otherwise.
.perform(swipeUp(), click())

onView(withId(typeID)).perform(click())
onView(withId(intensityID)).perform(click())
onView(allOf(withId(R.id.positive), withText(R.string.play)))
.perform(click())

verify(exactly = 1) { MediaPlayerService.playRandomPreset(any(), tag, intensityRange) }
clearMocks(MediaPlayerService.Companion)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.github.ashutoshgngwr.noice.databinding.MainActivityBinding
import com.github.ashutoshgngwr.noice.fragment.AboutFragment
import com.github.ashutoshgngwr.noice.fragment.DialogFragment
import com.github.ashutoshgngwr.noice.fragment.PresetFragment
import com.github.ashutoshgngwr.noice.fragment.RandomPresetFragment
import com.github.ashutoshgngwr.noice.fragment.SleepTimerFragment
import com.github.ashutoshgngwr.noice.fragment.SoundLibraryFragment
import com.github.ashutoshgngwr.noice.fragment.SupportDevelopmentFragment
Expand Down Expand Up @@ -55,6 +56,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
R.id.saved_presets to PresetFragment::class.java,
R.id.sleep_timer to SleepTimerFragment::class.java,
R.id.wake_up_timer to WakeUpTimerFragment::class.java,
R.id.random_preset to RandomPresetFragment::class.java,
R.id.about to AboutFragment::class.java,
R.id.support_development to SupportDevelopmentFragment::class.java
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.ashutoshgngwr.noice.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.github.ashutoshgngwr.noice.InAppReviewFlowManager
import com.github.ashutoshgngwr.noice.MediaPlayerService
import com.github.ashutoshgngwr.noice.R
import com.github.ashutoshgngwr.noice.databinding.RandomPresetFragmentBinding
import com.github.ashutoshgngwr.noice.sound.Sound

class RandomPresetFragment : Fragment() {

private lateinit var binding: RandomPresetFragmentBinding

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = RandomPresetFragmentBinding.inflate(inflater, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//flexCheckedButton in XML is not working so we set manually.
binding.presetIntensityAny.isChecked = true
binding.presetTypeAny.isChecked = true

binding.playPresetButton.setOnClickListener {
val tag = when (binding.presetType.checkedRadioButtonId) {
R.id.preset_type__focus -> Sound.Tag.FOCUS
R.id.preset_type__relax -> Sound.Tag.RELAX
else -> null
}

val intensity = when (binding.presetIntensity.checkedRadioButtonId) {
R.id.preset_intensity__light -> SoundLibraryFragment.RANGE_INTENSITY_LIGHT
R.id.preset_intensity__dense -> SoundLibraryFragment.RANGE_INTENSITY_DENSE
else -> SoundLibraryFragment.RANGE_INTENSITY_ANY
}

MediaPlayerService.playRandomPreset(requireContext(), tag, intensity)

// maybe show in-app review dialog to the user
InAppReviewFlowManager.maybeAskForReview(requireActivity())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.ashutoshgngwr.noice.InAppReviewFlowManager
import com.github.ashutoshgngwr.noice.MediaPlayerService
import com.github.ashutoshgngwr.noice.R
import com.github.ashutoshgngwr.noice.databinding.DialogFragmentRandomPresetBinding
import com.github.ashutoshgngwr.noice.databinding.SoundGroupListItemBinding
import com.github.ashutoshgngwr.noice.databinding.SoundLibraryFragmentBinding
import com.github.ashutoshgngwr.noice.databinding.SoundListItemBinding
Expand Down Expand Up @@ -87,12 +86,6 @@ class SoundLibraryFragment : Fragment() {
} else {
binding.savePresetButton.hide()
}

if (event.state == PlayerManager.State.STOPPED) {
binding.randomPresetButton.show()
} else {
binding.randomPresetButton.hide()
}
}
}

Expand Down Expand Up @@ -142,37 +135,6 @@ class SoundLibraryFragment : Fragment() {
}
}

binding.randomPresetButton.setOnLongClickListener {
Toast.makeText(requireContext(), R.string.random_preset, Toast.LENGTH_LONG).show()
true
}

binding.randomPresetButton.setOnClickListener {
DialogFragment.show(childFragmentManager) {
val viewBinding = DialogFragmentRandomPresetBinding.inflate(layoutInflater)
addContentView(viewBinding.root)
title(R.string.random_preset)
negativeButton(R.string.cancel)
positiveButton(R.string.play) {
val tag = when (viewBinding.presetType.checkedRadioButtonId) {
R.id.preset_type__focus -> Sound.Tag.FOCUS
R.id.preset_type__relax -> Sound.Tag.RELAX
else -> null
}

val intensity = when (viewBinding.presetIntensity.checkedRadioButtonId) {
R.id.preset_intensity__light -> RANGE_INTENSITY_LIGHT
R.id.preset_intensity__dense -> RANGE_INTENSITY_DENSE
else -> RANGE_INTENSITY_ANY
}

MediaPlayerService.playRandomPreset(requireContext(), tag, intensity)
// maybe show in-app review dialog to the user
InAppReviewFlowManager.maybeAskForReview(requireActivity())
}
}
}

eventBus.register(this)
}

Expand Down
86 changes: 0 additions & 86 deletions app/src/main/res/layout/dialog_fragment__random_preset.xml

This file was deleted.

Loading

0 comments on commit feb0e7e

Please sign in to comment.