Skip to content

Commit

Permalink
mbrc-89 #89 Add notification/message on outdated API
Browse files Browse the repository at this point in the history
  • Loading branch information
kelsos committed Dec 13, 2016
1 parent 4cf99a8 commit 9a391f9
Show file tree
Hide file tree
Showing 14 changed files with 264 additions and 65 deletions.
30 changes: 23 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ android {
}
}
sourceSets {
androidTest.java.srcDirs += "src/androidTest/kotlin"
main.java.srcDirs += 'src/main/kotlin'
debug.java.srcDirs += 'src/debug/kotlin'
play.java.srcDirs += 'src/play/kotlin'
Expand All @@ -154,12 +155,13 @@ ext {
jackson_version = "2.8.5"
butterknife_version = "8.4.0"
material_dialogs_version = "0.9.1.0"
runner_version = "0.5"
espresso_version = "2.2.2"
junit_version = "4.12"
mockito_version = "2.3.0"
}

dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile "com.android.support:appcompat-v7:${support_library_version}"
compile "com.android.support:support-v4:${support_library_version}"
Expand Down Expand Up @@ -215,15 +217,29 @@ dependencies {
compile 'com.jakewharton.timber:timber:4.3.1'
compile 'com.jakewharton.rxrelay:rxrelay:1.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
testCompile 'org.robolectric:robolectric:3.1.4'
testCompile 'org.mockito:mockito-core:2.2.22'
testCompile 'com.google.truth:truth:0.30'
testCompile 'junit:junit:4.12'

testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
previewCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'

testCompile 'org.robolectric:robolectric:3.1.4'
testCompile "org.mockito:mockito-core:${mockito_version}"
testCompile 'com.google.truth:truth:0.30'
testCompile "junit:junit:${junit_version}"
testCompile "com.github.stephanenicolas.toothpick:toothpick-testing:${toothpick_version}"

androidTestCompile "com.github.stephanenicolas.toothpick:toothpick-testing:${toothpick_version}"
androidTestCompile "org.mockito:mockito-core:${mockito_version}"
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito:2.2.0'
androidTestCompile "junit:junit:${junit_version}"
androidTestCompile "com.android.support:support-annotations:${support_library_version}"
androidTestCompile "com.android.support.test:runner:${runner_version}"
androidTestCompile "com.android.support.test:rules:${runner_version}"
androidTestCompile("com.android.support.test.espresso:espresso-core:${espresso_version}", {
exclude group: 'com.android.support', module: 'support-annotations'
})

playCompile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
transitive = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.kelsos.mbrc.ui.navigation.main

import android.app.Application
import android.content.Intent
import android.support.test.InstrumentationRegistry
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.assertion.ViewAssertions.doesNotExist
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.espresso.matcher.ViewMatchers.Visibility.VISIBLE
import android.support.test.filters.LargeTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import com.kelsos.mbrc.R
import com.kelsos.mbrc.constants.Protocol
import com.kelsos.mbrc.domain.TrackInfo
import com.kelsos.mbrc.events.bus.RxBus
import com.kelsos.mbrc.model.MainDataModel
import com.kelsos.mbrc.repository.ModelCache
import com.kelsos.mbrc.utilities.SettingsManager
import org.hamcrest.Matchers.allOf
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.mockito.Mockito.*
import rx.Completable
import rx.Scheduler
import rx.Single
import rx.schedulers.TestScheduler
import toothpick.Toothpick
import toothpick.config.Module
import toothpick.testing.ToothPickRule


@RunWith(AndroidJUnit4::class)
@LargeTest
class MainActivityTest {

var toothPickRule = ToothPickRule(this)
val activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java, true, false)

@Rule fun chain(): TestRule = RuleChain.outerRule(toothPickRule).around(activityRule)


private lateinit var model: MainDataModel
private lateinit var mockBus: RxBus
private lateinit var mockSettingsManager: SettingsManager
private lateinit var mockCache: ModelCache

@Before
fun setUp() {
mockSettingsManager = mock(SettingsManager::class.java)
mockCache = mock(ModelCache::class.java)
`when`(mockCache.restoreCover()).thenReturn(Single.just(""))
`when`(mockCache.persistCover(anyString())).thenReturn(Completable.complete())
`when`(mockCache.restoreInfo()).thenReturn(Single.just(TrackInfo()))
`when`(mockSettingsManager.shouldShowPluginUpdate()).thenReturn(Single.just(false))
mockBus = mock(RxBus::class.java)

model = MainDataModel(mockBus, mockCache)

val application = InstrumentationRegistry.getTargetContext().applicationContext as Application
val scope = Toothpick.openScope(application)
scope.installModules(TestModule())
}

@After
fun tearDown() {
Toothpick.reset()
}

@Test
fun testShowOutdatedPluginSnackBar() {
activityRule.launchActivity(Intent())
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.plugin_protocol_out_of_date)))
.check(doesNotExist())
model.pluginProtocol = 1
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.plugin_protocol_out_of_date)))
.check(matches(withEffectiveVisibility(VISIBLE)))
}

@Test
fun testShouldNotShowOutdatedPluginSnackBar() {
activityRule.launchActivity(Intent())
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.plugin_protocol_out_of_date)))
.check(doesNotExist())
model.pluginProtocol = Protocol.ProtocolVersionNumber
onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(R.string.plugin_protocol_out_of_date)))
.check(doesNotExist())
}

inner class TestModule : Module() {
init {
bind(MainDataModel::class.java).toProviderInstance { model }.providesSingletonInScope()
bind(RxBus::class.java).toProviderInstance { mockBus }.providesSingletonInScope()
bind(SettingsManager::class.java)
.toProviderInstance { mockSettingsManager }
.providesSingletonInScope()
bind(Scheduler::class.java).withName("main").toProviderInstance { TestScheduler() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ internal constructor(private val model: MainDataModel,

override fun execute(e: IEvent) {

if (!manager.isPluginUpdateCheckEnabled) {
if (!manager.isPluginUpdateCheckEnabled()) {
return
}

val calendar = Calendar.getInstance()
calendar.timeInMillis = manager.lastUpdated.time
calendar.timeInMillis = manager.getLastUpdated().time
calendar.add(Calendar.DATE, 2)
val nextCheck = Date(calendar.timeInMillis)
val now = Date()
Expand Down Expand Up @@ -84,7 +84,7 @@ internal constructor(private val model: MainDataModel,
}
}

manager.lastUpdated = now
manager.setLastUpdated(now)
Timber.d("last check on: %s", java.lang.Long.toString(now.time))
Timber.d("plugin reported version: %s", model.pluginVersion)
Timber.d("plugin suggested version: %s", suggestedVersion)
Expand Down
33 changes: 31 additions & 2 deletions app/src/main/kotlin/com/kelsos/mbrc/di/modules/RemoteModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,34 @@ import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.kelsos.mbrc.di.providers.NotificationManagerCompatProvider
import com.kelsos.mbrc.events.bus.RxBus
import com.kelsos.mbrc.events.bus.RxBusImpl
import com.kelsos.mbrc.repository.*
import com.kelsos.mbrc.services.*
import com.kelsos.mbrc.repository.AlbumRepository
import com.kelsos.mbrc.repository.AlbumRepositoryImpl
import com.kelsos.mbrc.repository.ArtistRepository
import com.kelsos.mbrc.repository.ArtistRepositoryImpl
import com.kelsos.mbrc.repository.ConnectionRepository
import com.kelsos.mbrc.repository.ConnectionRepositoryImpl
import com.kelsos.mbrc.repository.GenreRepository
import com.kelsos.mbrc.repository.GenreRepositoryImpl
import com.kelsos.mbrc.repository.ModelCache
import com.kelsos.mbrc.repository.ModelCacheImpl
import com.kelsos.mbrc.repository.NowPlayingRepository
import com.kelsos.mbrc.repository.NowPlayingRepositoryImpl
import com.kelsos.mbrc.repository.PlaylistRepository
import com.kelsos.mbrc.repository.PlaylistRepositoryImpl
import com.kelsos.mbrc.repository.TrackRepository
import com.kelsos.mbrc.repository.TrackRepositoryImpl
import com.kelsos.mbrc.services.CoverService
import com.kelsos.mbrc.services.CoverServiceImpl
import com.kelsos.mbrc.services.LibraryService
import com.kelsos.mbrc.services.LibraryServiceImpl
import com.kelsos.mbrc.services.NowPlayingService
import com.kelsos.mbrc.services.NowPlayingServiceImpl
import com.kelsos.mbrc.services.PlaylistService
import com.kelsos.mbrc.services.PlaylistServiceImpl
import com.kelsos.mbrc.services.QueueService
import com.kelsos.mbrc.services.QueueServiceImpl
import com.kelsos.mbrc.utilities.SettingsManager
import com.kelsos.mbrc.utilities.SettingsManagerImpl
import rx.Scheduler
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
Expand Down Expand Up @@ -38,5 +64,8 @@ class RemoteModule : Module() {

bind(NowPlayingRepository::class.java).to(NowPlayingRepositoryImpl::class.java)
bind(PlaylistRepository::class.java).to(PlaylistRepositoryImpl::class.java)

bind(SettingsManager::class.java).to(SettingsManagerImpl::class.java).singletonInScope()
bind(ModelCache::class.java).to(ModelCacheImpl::class.java).singletonInScope()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ constructor(bus: RxBus,
}

private fun connectionChanged(event: ConnectionStatusChangeEvent) {
if (!settings.isNotificationControlEnabled) {
if (!settings.isNotificationControlEnabled()) {
Timber.v("Notification is off doing nothing")
return
}
Expand Down
31 changes: 23 additions & 8 deletions app/src/main/kotlin/com/kelsos/mbrc/model/MainDataModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ import com.kelsos.mbrc.domain.TrackInfo
import com.kelsos.mbrc.enums.LfmStatus
import com.kelsos.mbrc.events.MessageEvent
import com.kelsos.mbrc.events.bus.RxBus
import com.kelsos.mbrc.events.ui.LfmRatingChanged
import com.kelsos.mbrc.events.ui.PlayStateChange
import com.kelsos.mbrc.events.ui.RatingChanged
import com.kelsos.mbrc.events.ui.RepeatChange
import com.kelsos.mbrc.events.ui.ScrobbleChange
import com.kelsos.mbrc.events.ui.ShuffleChange
import com.kelsos.mbrc.events.ui.*
import com.kelsos.mbrc.events.ui.ShuffleChange.ShuffleState
import com.kelsos.mbrc.events.ui.VolumeChange
import com.kelsos.mbrc.repository.ModelCache
import rx.Completable
import rx.Subscription
Expand All @@ -38,8 +32,13 @@ constructor(private val bus: RxBus,
private var subscription: Subscription? = null
private var _trackInfo: TrackInfo = TrackInfo()
private var _coverPath: String = ""
private var onPluginOutOfDate: (() -> Unit)? = null

init {
restoreStated()
}

private fun restoreStated() {
cache.restoreInfo().subscribe({ trackInfo = it }, { onLoadError(it) })
cache.restoreCover().subscribe({ coverPath = it }, { onLoadError(it) })
}
Expand All @@ -52,6 +51,10 @@ constructor(private val bus: RxBus,
}
}

fun setOnPluginOutOfDate(method: (() -> Unit)?) {
this.onPluginOutOfDate = method
}

var rating: Float = 0f
set(value) {
field = value
Expand Down Expand Up @@ -99,6 +102,13 @@ constructor(private val bus: RxBus,
}

var pluginProtocol: Int = 2
set(value) {
field = value
if (value < Protocol.ProtocolVersionNumber) {
apiOutOfDate = true
onPluginOutOfDate?.invoke()
}
}


@State var playState: String = PlayerState.UNDEFINED
Expand All @@ -111,7 +121,7 @@ constructor(private val bus: RxBus,
Const.STOPPED.equals(value, ignoreCase = true) -> PlayerState.STOPPED
Const.PAUSED.equals(value, ignoreCase = true) -> PlayerState.PAUSED
else -> PlayerState.UNDEFINED
}
}

field = newState

Expand Down Expand Up @@ -181,5 +191,10 @@ constructor(private val bus: RxBus,
}
}


var apiOutOfDate: Boolean = false
get
private set

}

19 changes: 13 additions & 6 deletions app/src/main/kotlin/com/kelsos/mbrc/repository/ModelCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import rx.Single
import java.io.*
import javax.inject.Inject

class ModelCache
class ModelCacheImpl
@Inject constructor(private val mapper: ObjectMapper,
private val context: Application) {
private val context: Application) : ModelCache {

fun persistInfo(trackInfo: TrackInfo): Completable {
override fun persistInfo(trackInfo: TrackInfo): Completable {
return Completable.fromCallable {
val infoFile = File(context.filesDir, TRACK_INFO)
if (infoFile.exists()) {
Expand All @@ -22,7 +22,7 @@ class ModelCache
}
}

fun restoreInfo(): Single<TrackInfo> {
override fun restoreInfo(): Single<TrackInfo> {
return Single.fromCallable {
val infoFile = File(context.filesDir, TRACK_INFO)
if (infoFile.exists()) {
Expand All @@ -32,7 +32,7 @@ class ModelCache
}
}

fun persistCover(cover: String): Completable {
override fun persistCover(cover: String): Completable {
return Completable.fromCallable {
val coverFile = File(context.filesDir, COVER_INFO)
if (coverFile.exists()) {
Expand All @@ -48,7 +48,7 @@ class ModelCache
}
}

fun restoreCover(): Single<String> {
override fun restoreCover(): Single<String> {
return Single.fromCallable {
val coverFile = File(context.filesDir, COVER_INFO)
if (coverFile.exists()) {
Expand All @@ -68,3 +68,10 @@ class ModelCache
const val COVER_INFO = "cover.txt"
}
}

interface ModelCache {
fun persistInfo(trackInfo: TrackInfo): Completable
fun restoreInfo(): Single<TrackInfo>
fun persistCover(cover: String): Completable
fun restoreCover(): Single<String>
}
Loading

0 comments on commit 9a391f9

Please sign in to comment.