@Test public void testObserveChildEvent_Added() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<TestData>> testSubscriber = new TestSubscriber<>(); RxFirebaseDatabase.observeChildEvent(mockDatabase, TestData.class) .subscribeOn(Schedulers.immediate()) .subscribe(testSubscriber); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(mockDatabase).addChildEventListener(argument.capture()); argument.getValue().onChildAdded(mockFirebaseDataSnapshot, "root"); testSubscriber.assertNoErrors(); testSubscriber.assertValueCount(1); testSubscriber.assertReceivedOnNext(Collections.singletonList(testChildEventAdded)); testSubscriber.assertNotCompleted(); testSubscriber.unsubscribe(); }
@Test public void testObserveChildEvent_Changed() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<TestData>> testSubscriber = new TestSubscriber<>(); RxFirebaseDatabase.observeChildEvent(mockDatabase, TestData.class) .subscribeOn(Schedulers.immediate()) .subscribe(testSubscriber); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(mockDatabase).addChildEventListener(argument.capture()); argument.getValue().onChildChanged(mockFirebaseDataSnapshot, "root"); testSubscriber.assertNoErrors(); testSubscriber.assertValueCount(1); testSubscriber.assertReceivedOnNext(Collections.singletonList(testChildEventChanged)); testSubscriber.assertNotCompleted(); testSubscriber.unsubscribe(); }
@Test public void testObserveChildEvent_Removed() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<TestData>> testSubscriber = new TestSubscriber<>(); RxFirebaseDatabase.observeChildEvent(mockDatabase, TestData.class) .subscribeOn(Schedulers.immediate()) .subscribe(testSubscriber); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(mockDatabase).addChildEventListener(argument.capture()); argument.getValue().onChildRemoved(mockFirebaseDataSnapshot); testSubscriber.assertNoErrors(); testSubscriber.assertValueCount(1); testSubscriber.assertReceivedOnNext(Collections.singletonList(testChildEventRemoved)); testSubscriber.assertNotCompleted(); testSubscriber.unsubscribe(); }
@Test public void testObserveChildEvent_Moved() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<TestData>> testSubscriber = new TestSubscriber<>(); RxFirebaseDatabase.observeChildEvent(mockDatabase, TestData.class) .subscribeOn(Schedulers.immediate()) .subscribe(testSubscriber); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(mockDatabase).addChildEventListener(argument.capture()); argument.getValue().onChildMoved(mockFirebaseDataSnapshot, "root"); testSubscriber.assertNoErrors(); testSubscriber.assertValueCount(1); testSubscriber.assertReceivedOnNext(Collections.singletonList(testChildEventMoved)); testSubscriber.assertNotCompleted(); testSubscriber.unsubscribe(); }
@Test public void testObserveChildEvent_Cancelled() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<TestData>> testSubscriber = new TestSubscriber<>(); RxFirebaseDatabase.observeChildEvent(mockDatabase, TestData.class) .subscribeOn(Schedulers.immediate()) .subscribe(testSubscriber); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(mockDatabase).addChildEventListener(argument.capture()); argument.getValue().onCancelled(DatabaseError.zzqv(DatabaseError.DISCONNECTED)); testSubscriber.assertError(RxFirebaseDataException.class); testSubscriber.assertNotCompleted(); testSubscriber.unsubscribe(); }
@Override public Observable<FeedChangedInfoEntity> registerFeedChangedEvent() { return Observable.fromAsync(new Action1<AsyncEmitter<FeedChangedInfoEntity>>() { @Override public void call(AsyncEmitter<FeedChangedInfoEntity> feedChangedInfoEntityAsyncEmitter) { Timber.v(String.format("fromAsync create : %s", Thread.currentThread().getName())); // those of registered method will be called in a background thread final Query query = database.child(Firebase.QUERY_FEEDS); final ChildEventListener listener = new FeedChangeListener(feedChangedInfoEntityAsyncEmitter); query.addChildEventListener(listener); feedChangedInfoEntityAsyncEmitter.setCancellation(new AsyncEmitter.Cancellable() { @Override public void cancel() throws Exception { database.removeEventListener(listener); } }); } }, AsyncEmitter.BackpressureMode.BUFFER); }
@Test public void shouldBufferChildEvents() throws Exception { ArgumentCaptor<ChildEventListener> captor = forClass(ChildEventListener.class); TestSubscriber<ChildEvent> subscriber = rx.onChildEvent().test(1); then(query).should().addChildEventListener(captor.capture()); ChildEventListener listener = captor.getValue(); DataSnapshot snapshot = mock(DataSnapshot.class); listener.onChildAdded(snapshot, null); listener.onChildAdded(snapshot, null); subscriber .assertValueCount(1) .assertNoErrors(); }
@Test public void shouldFilterChildEvent() throws Exception { ArgumentCaptor<ChildEventListener> captor = forClass(ChildEventListener.class); TestSubscriber<ChildEvent> subscriber = rx.onChildEvent(ChildEvent.Type.CHILD_ADDED).test(); then(query).should().addChildEventListener(captor.capture()); ChildEventListener listener = captor.getValue(); DataSnapshot snapshot = mock(DataSnapshot.class); listener.onChildAdded(snapshot, null); listener.onChildChanged(snapshot, null); subscriber .assertValueCount(1) .assertNoErrors(); }
@Test public void shouldParseChildEventValue() throws Exception { ArgumentCaptor<ChildEventListener> captor = forClass(ChildEventListener.class); TestSubscriber<String> subscriber = rx .onChildEventValue(ChildEvent.Type.CHILD_ADDED, String.class) .test(); then(query).should().addChildEventListener(captor.capture()); ChildEventListener listener = captor.getValue(); DataSnapshot snapshot = mock(DataSnapshot.class); given(snapshot.getValue(String.class)).willReturn("biscuits"); listener.onChildAdded(snapshot, null); subscriber .assertValue("biscuits") .assertNoErrors(); }
@Test public void testObserveChildEventAdded() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<ChildData>> testObserver = RxFirebaseDatabase .observeChildEvent(databaseReference, ChildData.class) .test(); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(databaseReference).addChildEventListener(argument.capture()); argument.getValue().onChildAdded(dataSnapshot, PREVIOUS_CHILD_NAME); testObserver.assertNoErrors() .assertValueCount(1) .assertValueSet(Collections.singletonList(childEventAdded)) .assertNotComplete() .dispose(); }
@Test public void testObserveChildEventChanged() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<ChildData>> testObserver = RxFirebaseDatabase .observeChildEvent(databaseReference, ChildData.class) .test(); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(databaseReference).addChildEventListener(argument.capture()); argument.getValue().onChildChanged(dataSnapshot, PREVIOUS_CHILD_NAME); testObserver.assertNoErrors() .assertValueCount(1) .assertValueSet(Collections.singletonList(childEventChanged)) .assertNotComplete() .dispose(); }
@Test public void testObserveChildEventRemoved() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<ChildData>> testObserver = RxFirebaseDatabase .observeChildEvent(databaseReference, ChildData.class) .test(); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(databaseReference).addChildEventListener(argument.capture()); argument.getValue().onChildRemoved(dataSnapshot); testObserver.assertNoErrors() .assertValueCount(1) .assertValueSet(Collections.singletonList(childEventRemoved)) .assertNotComplete() .dispose(); }
@Test public void testObserveChildEventMoved() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<ChildData>> testObserver = RxFirebaseDatabase .observeChildEvent(databaseReference, ChildData.class) .test(); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(databaseReference).addChildEventListener(argument.capture()); argument.getValue().onChildMoved(dataSnapshot, PREVIOUS_CHILD_NAME); testObserver.assertNoErrors() .assertValueCount(1) .assertValueSet(Collections.singletonList(childEventMoved)) .assertNotComplete() .dispose(); }
@Test public void testChildAddedEvents() throws InterruptedException { DatabaseReference ref = IntegrationTestUtils.getRandomNode(masterApp) ; Map<String, Object> initial = new MapBuilder() .put("a", MapBuilder.of("value", 5L)) .put("c", MapBuilder.of("value", 3L)) .build(); final List<String> snapshotNames = new ArrayList<>(); final List<String> prevNames = new ArrayList<>(); final Semaphore semaphore = new Semaphore(0); final ChildEventListener testListener = ref.orderByChild("value") .addChildEventListener( new TestChildEventListener() { @Override public void onChildAdded(DataSnapshot snap, String prevName) { snapshotNames.add(snap.getKey()); prevNames.add(prevName); semaphore.release(); } }); ref.setValueAsync(initial); TestHelpers.waitFor(semaphore, 2); Assert.assertEquals(Arrays.asList("c", "a"), snapshotNames); Assert.assertEquals(Arrays.asList(null, "c"), prevNames); Map<String, Object> updates = new HashMap<>(); updates.put("b", MapBuilder.of("value", 4)); updates.put("d", MapBuilder.of("value", 2)); ref.updateChildrenAsync(updates); TestHelpers.waitFor(semaphore, 2); Assert.assertEquals(Arrays.asList("c", "a", "d", "b"), snapshotNames); Assert.assertEquals(Arrays.asList(null, "c", null, "c"), prevNames); ref.removeEventListener(testListener); }
/** Remove the saved listener from the saved ref.*/ public void breakCombo(){ DatabaseReference comboRef = FirebaseDatabase.getInstance().getReferenceFromUrl(ref); listener.killEvent(); if (listener.getType() == FirebaseGeneralEvent.ChildEvent) { comboRef.removeEventListener((ChildEventListener) listener); } else if (listener.getType() == FirebaseGeneralEvent.ValueEvent) { comboRef.removeEventListener((ValueEventListener) listener); } }
@Before public void setup() { MockitoAnnotations.initMocks(this); childEventListener = ArgumentCaptor.forClass(ChildEventListener.class); valueEventListener = ArgumentCaptor.forClass(ValueEventListener.class); transactionHandler = ArgumentCaptor.forClass(Transaction.Handler.class); onCompleteListener = ArgumentCaptor.forClass(OnCompleteListener.class); }
@Override public void call(Subscriber<? super T> subscriber) { final ChildEventListener listener = query.addChildEventListener(new RxValueListener<>(subscriber, marshaller)); subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { query.removeEventListener(listener); } })); }
public void onViewModelDestroyed() { if(this.attachValueEventListener() || this.attachSingleValueEventListener()) { this.reference.removeEventListener((ValueEventListener) this); } if(this.attachChildEventListener()) { this.reference.removeEventListener((ChildEventListener) this); } super.onViewModelDestroyed(); }
@Test public void testObserveChildEventCancelled() throws InterruptedException { TestSubscriber<RxFirebaseChildEvent<ChildData>> testObserver = RxFirebaseDatabase .observeChildEvent(databaseReference, ChildData.class) .test(); ArgumentCaptor<ChildEventListener> argument = ArgumentCaptor.forClass(ChildEventListener.class); verify(databaseReference).addChildEventListener(argument.capture()); argument.getValue().onCancelled(DatabaseError.zzfr(DatabaseError.DISCONNECTED)); testObserver.assertError(RxFirebaseDataException.class) .assertNotComplete() .dispose(); }
public ChildEventRegistration( @NotNull Repo repo, @NotNull ChildEventListener eventListener, @NotNull QuerySpec spec) { this.repo = repo; this.eventListener = eventListener; this.spec = spec; }
public ChildEventListener attachBookFolderChildEventListener(String userId, String folderId, ChildEventListener listener) { return mDatabaseReference.child(userId).child(REF_FOLDERS).child(folderId).child(REF_BOOKS).addChildEventListener(listener); }
public void detachBookFolderChildEventListener(String userId, String folderId, ChildEventListener listener) { mDatabaseReference.child(userId).child(REF_FOLDERS).child(folderId).child(REF_BOOKS).removeEventListener(listener); }
public void attachFetchFolders(String userId, ChildEventListener listener) { DatabaseReference ref = mDatabaseReference.child(userId).child(REF_FOLDERS); ref.addChildEventListener(listener); }
public void detachFetchFolders(String userId, ChildEventListener listener) { DatabaseReference ref = mDatabaseReference.child(userId).child(REF_FOLDERS); ref.removeEventListener(listener); }
public void onNewLikeAddedListener(ChildEventListener childEventListener) { DatabaseReference mLikesReference = database.getReference().child("post-likes"); mLikesReference.addChildEventListener(childEventListener); }
public void cleanup() { mQuery.removeEventListener((ValueEventListener) this); mQuery.removeEventListener((ChildEventListener) this); }
public static void removeChildListener(@NonNull DatabaseReference reference, @NonNull ChildEventListener listener) { try { reference.removeEventListener(listener); } catch (Exception ignored) {} }
ChildEventCancellable(Query query, ChildEventListener listener) { this.query = query; this.listener = listener; }
@Test public void shouldAddChildEventListener() throws Exception { rx.onChildEvent().subscribe(); then(query).should().addChildEventListener(any(ChildEventListener.class)); }
@Override protected void onDestroy() { super.onDestroy(); mQuery.removeEventListener((ValueEventListener) this); mQuery.removeEventListener((ChildEventListener) this); }