2017-11-15 8 views
0

私はリアクティブ・リポジトリの正しいテストを書こうとしています。私は実際に場所の変更を聞きたいだけでなく、ちょうど作成されたカスタムロケーションで新しい場所の変更を複数回呼び出すだけです。ここで リアクティブ・インタフェース・リポジトリの適切なテストの記述方法イベントが発生したときだけオブザーバブルを返す方法、そのイベントをトリガーする方法

はリポジトリと収集場所のためにその機能である:ここでは
interface RxLocationRepository { 

@SuppressLint("MissingPermission") 
fun onLocationUpdate(): Observable<Location> 

fun stopLocationUpdates() 
} 

iは、実際の位置情報の更新をリッスンしたくないので、実際には重要ではありません、それの実装ですが、私LocationManagerの更新を聴くためだけに反応実装であることを示したい:

class LocationNativeRepository(
    val locationManager: LocationManager, 
    val geoEventsDistanceMeters: Int, 
    val geoEventsIntervalSeconds: Int) : RxLocationRepository{ 

var locationToPopulate: Location = Location(LocationManager.GPS_PROVIDER) 
lateinit var mLocationCallbackNativeApi: LocationListener 
private val subject: BehaviorSubject<Location> = BehaviorSubject.createDefault(locationToPopulate) 
var locationEmitter: Observable<Location> = subject.hide() 

init { 
    configureNativeLocationEmitter() 
} 

override fun onLocationUpdate(): Observable<Location> { 
    return locationEmitter 
} 

@SuppressLint("MissingPermission") 
override fun stopLocationUpdates() { 
    locationManager.removeUpdates(mLocationCallbackNativeApi) 
} 


@SuppressLint("MissingPermission") 
private fun configureNativeLocationEmitter() { 

    mLocationCallbackNativeApi = object : LocationListener { 

     override fun onLocationChanged(location: Location) { 
      subject.onNext(location) 
     } 

     override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {} 

     override fun onProviderEnabled(provider: String) {} 

     override fun onProviderDisabled(provider: String) {} 

    } 

    try { 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       (geoEventsIntervalSeconds * 1000).toLong(), 
       geoEventsDistanceMeters.toFloat(), 
       mLocationCallbackNativeApi, 
       Looper.getMainLooper()) 
    } catch (ignored: IllegalArgumentException) { 
     ignored.printStackTrace() 
    } 

    try { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
       (geoEventsIntervalSeconds * 1000).toLong(), 
       geoEventsDistanceMeters.toFloat(), 
       mLocationCallbackNativeApi, 
       Looper.getMainLooper()) 
    } catch (ignored: IllegalArgumentException) { 
     ignored.printStackTrace() 
    } 
} 

} 

ので、私は実際にonLocationUpdate()メソッドをトリガするために私のテストでこのリポジトリを呼び出すことができますか?例えば、私はこのように3回の位置を放出させます:

val location = Location("test").apply { 
     latitude = 1.234 
     longitude = 5.678 
     accuracy = 20f 
     time = Date().time 
    } 

答えて

0

PublishSubject<Location>変数を作成します。 RxLocationRepositoryの匿名サブクラスを作成します。onLocationUpdate()メソッドが変数を返します。その後、新しい場所を提供するために値を変数に出力します。

関連する問題