@Test @UiThreadTest public void propertyChangeEvents() { final ObservableParcelable<MyParcelable> observableField = new ObservableParcelable<>(); MyParcelable value; RecordingObserver<MyParcelable> o = new RecordingObserver<>(); RxObservableParcelable.propertyChanges(observableField).subscribe(o); o.assertNoMoreEvents(); value = new MyParcelable(1, "a"); observableField.set(value); assertEquals(value, o.takeNext()); value = new MyParcelable(2, "b"); observableField.set(value); assertEquals(value, o.takeNext()); o.dispose(); value = new MyParcelable(3, "c"); observableField.set(value); o.assertNoMoreEvents(); }
@Test public void takeTakesCorrectTimes() throws Exception { Point point1 = new Point(1, 2); Point point2 = new Point(41, 11); ObservableParcelable<Point> observed = new ObservableParcelable<>(point1); ObservableParcelable<Point> observing = Cinder.<Point>computeParcelable(()-> observed.get() , observed).take(2).immediate(); assertTrue(observing.get() == point1); observed.set(point2); assertTrue(observing.get() == point2); observed.set(point1); assertTrue(observing.get() == point2); }
@Test public void skipSkipsCorrectTimes() throws Exception { Point point1 = new Point(1, 2); Point point2 = new Point(41, 11); ObservableParcelable<Point> observed = new ObservableParcelable<>(); ObservableParcelable<Point> observing = Cinder.<Point>computeParcelable(()-> observed.get() , observed).skip(1); observed.set(point1); assertTrue(observing.get() == null); observed.set(point2); assertTrue(observing.get() == point2); observed.set(point1); assertTrue(observing.get() == point1); }
@Test public void computesParcelable() throws Exception { Point point1 = new Point(6, 7); Point point2 = new Point(76, 90); ObservableParcelable<Point> obsPoint = new ObservableParcelable<>(point1); ObservableParcelable<Point> computedPoint = Cinder.computeParcelable(()->obsPoint.get(), obsPoint).immediate(); assertEquals(computedPoint.get().x, point1.x); assertEquals(computedPoint.get().y, point1.y); obsPoint.set(point2); assertEquals(computedPoint.get().x, point2.x); assertEquals(computedPoint.get().y, point2.y); }
@Test public void onceTakesOnce() throws Exception { Point point1 = new Point(1, 2); Point point2 = new Point(14, 21); ObservableParcelable<Point> observed = new ObservableParcelable<>(point1); ObservableParcelable<Point> observing = Cinder.<Point>computeParcelable(()-> observed.get(), observed).once().immediate(); assertTrue(observing.get() == point1); observed.set(point2); assertTrue(observing.get() == point1); }
@Test public void filterFilters() throws Exception { Point point1 = new Point(1, 2); Point point2 = new Point(41, 11); ObservableParcelable<Point> observed = new ObservableParcelable<>(); ObservableParcelable<Point> observing = Cinder.<Point>computeParcelable(()-> observed.get() , observed).filter(()->observed.get() == point1); observed.set(point2); assertTrue(observing.get() == null); observed.set(point1); assertTrue(observing.get() == point1); }
@Test public void takeWhileTakesWhileTrue() throws Exception { Point point1 = new Point(1, 2); Point point2 = new Point(41, 11); ObservableParcelable<Point> observed = new ObservableParcelable<>(); ObservableParcelable<Point> observing = Cinder.<Point>computeParcelable(()-> observed.get() , observed).takeWhile(()->observed.get() == point1); observed.set(point1); assertTrue(observing.get() == point1); observed.set(point2); assertTrue(observing.get() == point1); }
@Test public void skipWhileSkipsWhileTrue() throws Exception { Point point1 = new Point(1, 2); Point point2 = new Point(41, 11); ObservableParcelable<Point> observed = new ObservableParcelable<>(); ObservableParcelable<Point> observing = Cinder.<Point>computeParcelable(()-> observed.get() , observed).skipWhile(()->observed.get() == point1); observed.set(point1); assertTrue(observing.get() == null); observed.set(point2); assertTrue(observing.get() == point2); }
@Test public void withDefaultAssignsDefault() throws Exception { Point point1 = new Point(56, 65); ObservableParcelable<Point> observing = Cinder.<Point>computeParcelable(()->new Point(14, 8) ).withDefault(point1); assertTrue(observing.get() == point1); }
@Test public void stopStops() throws Exception { Point point1 = new Point(1, 2); Point point2 = new Point(14, 21); ObservableParcelable<Point> observed = new ObservableParcelable<>(point1); CinderParcelable<Point> observing = Cinder.<Point>computeParcelable(()-> observed.get(), observed).immediate(); assertTrue(observing.get() == point1); observing.stop(); observed.set(point2); assertTrue(observing.get() == point1); }
@Test public void convertsObservableParcableToRx() throws Exception { ObservableParcelable<Point> observableParcalable = new ObservableParcelable<Point>(); Observable<Point> rxParcalable = RxCinder.convert(observableParcalable); final String TEST_KEY = "TEST_KEY"; final ArrayMap<String, Point> testMap = new ArrayMap<>(); rxParcalable.subscribe((c)->{ testMap.put(TEST_KEY, c);}); final Point TEST_POINT = new Point(12, 23); observableParcalable.set(TEST_POINT); assertTrue(testMap.get(TEST_KEY).x == TEST_POINT.x); }
@Test public void convertsRxParcelableToObservable() throws Exception { final Point TEST_POINT = new Point(12, 13); Observable<Point> rxPoint = Observable.just(TEST_POINT); ObservableParcelable<Point> observableParcelable = RxCinder.convertParcelable(rxPoint); assertTrue(observableParcelable.get().x == TEST_POINT.x); }
public static <T extends Parcelable> Observable<T> convert(ObservableParcelable<T> observableParcelable){ return Observable.create((e)-> Cinder.observe(()->e.onNext(observableParcelable.get()), observableParcelable) ); }
public static <T extends Parcelable> ObservableParcelable<T> convertParcelable(Observable<T> observable){ ObservableParcelable<T> observableParcelable = new ObservableParcelable<>(); observable.subscribe((p)->observableParcelable.set(p)); return observableParcelable; }
public Model() { note = new ObservableParcelable<>(); }
/** * Create an observable of property change events on {@code observableParcelable}. * <p> * <em>Warning:</em> The created observable keeps a strong reference to {@code * observableParcelable}. * Unsubscribe to free this reference. */ @CheckResult @NonNull // public static <T extends Parcelable> Observable<? extends T> propertyChanges( @NonNull ObservableParcelable<T> observableParcelable) { checkNotNull(observableParcelable, "observableParcelable == null"); return new ObservableFieldObservable<>(observableParcelable); }