Skip to content

Commit 7d10c7e

Browse files
committed
fix sticky intent duplication, add tests for OperatorBroadcast
1 parent d49cd1e commit 7d10c7e

File tree

2 files changed

+128
-4
lines changed

2 files changed

+128
-4
lines changed

rxjava-contrib/rxjava-android/src/main/java/rx/operators/OperatorBroadcastRegister.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ public void call() {
5858
});
5959

6060
subscriber.add(subscription);
61-
Intent stickyIntent = context.registerReceiver(broadcastReceiver, intentFilter, broadcastPermission, schedulerHandler);
62-
if (stickyIntent != null) {
63-
subscriber.onNext(stickyIntent);
64-
}
61+
context.registerReceiver(broadcastReceiver, intentFilter, broadcastPermission, schedulerHandler);
6562

6663
}
6764
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.android.operators;
17+
18+
import android.app.Application;
19+
import android.content.Intent;
20+
import android.content.IntentFilter;
21+
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.InOrder;
25+
import org.robolectric.Robolectric;
26+
import org.robolectric.RobolectricTestRunner;
27+
28+
import rx.Observable;
29+
import rx.Observer;
30+
import rx.Subscription;
31+
import rx.android.observables.AndroidObservable;
32+
import rx.observers.TestObserver;
33+
34+
import static org.mockito.Matchers.any;
35+
import static org.mockito.Mockito.inOrder;
36+
import static org.mockito.Mockito.mock;
37+
import static org.mockito.Mockito.never;
38+
import static org.mockito.Mockito.times;
39+
40+
@RunWith(RobolectricTestRunner.class)
41+
public class OperatorBroadcastRegisterTest {
42+
43+
@Test
44+
public void testBroadcast() {
45+
String action = "TEST_ACTION";
46+
IntentFilter intentFilter = new IntentFilter(action);
47+
Application application = Robolectric.application;
48+
Observable<Intent> observable = AndroidObservable.fromBroadcast(application, intentFilter);
49+
final Observer<Intent> observer = mock(Observer.class);
50+
final Subscription subscription = observable.subscribe(new TestObserver<Intent>(observer));
51+
52+
final InOrder inOrder = inOrder(observer);
53+
54+
inOrder.verify(observer, never()).onNext(any(Intent.class));
55+
56+
Intent intent = new Intent(action);
57+
application.sendBroadcast(intent);
58+
inOrder.verify(observer, times(1)).onNext(intent);
59+
60+
application.sendBroadcast(intent);
61+
inOrder.verify(observer, times(1)).onNext(intent);
62+
63+
subscription.unsubscribe();
64+
application.sendBroadcast(intent);
65+
inOrder.verify(observer, never()).onNext(any(Intent.class));
66+
67+
inOrder.verify(observer, never()).onError(any(Throwable.class));
68+
inOrder.verify(observer, never()).onCompleted();
69+
}
70+
71+
@Test
72+
public void testStickyBroadcast() {
73+
String action = "TEST_STICKY_ACTION";
74+
IntentFilter intentFilter = new IntentFilter(action);
75+
Application application = Robolectric.application;
76+
Intent intent = new Intent(action);
77+
application.sendStickyBroadcast(intent);
78+
Observable<Intent> observable = AndroidObservable.fromBroadcast(application, intentFilter);
79+
final Observer<Intent> observer = mock(Observer.class);
80+
final Subscription subscription = observable.subscribe(new TestObserver<Intent>(observer));
81+
82+
final InOrder inOrder = inOrder(observer);
83+
84+
inOrder.verify(observer, times(1)).onNext(intent);
85+
86+
application.sendBroadcast(intent);
87+
inOrder.verify(observer, times(1)).onNext(intent);
88+
89+
subscription.unsubscribe();
90+
application.sendBroadcast(intent);
91+
inOrder.verify(observer, never()).onNext(any(Intent.class));
92+
93+
inOrder.verify(observer, never()).onError(any(Throwable.class));
94+
inOrder.verify(observer, never()).onCompleted();
95+
}
96+
97+
@Test
98+
public void testPermissionBroadcast() {
99+
String action = "TEST_ACTION";
100+
String permission = "test_permission";
101+
IntentFilter intentFilter = new IntentFilter(action);
102+
Application application = Robolectric.application;
103+
Observable<Intent> observable = AndroidObservable.fromBroadcast(application, intentFilter, permission, null);
104+
final Observer<Intent> observer = mock(Observer.class);
105+
final Subscription subscription = observable.subscribe(new TestObserver<Intent>(observer));
106+
107+
final InOrder inOrder = inOrder(observer);
108+
109+
inOrder.verify(observer, never()).onNext(any(Intent.class));
110+
111+
Intent intent = new Intent(action);
112+
application.sendBroadcast(intent);
113+
inOrder.verify(observer, never()).onNext(intent);
114+
115+
application.sendBroadcast(intent, permission);
116+
inOrder.verify(observer, times(1)).onNext(intent);
117+
118+
subscription.unsubscribe();
119+
application.sendBroadcast(intent);
120+
application.sendBroadcast(intent, permission);
121+
inOrder.verify(observer, never()).onNext(any(Intent.class));
122+
123+
inOrder.verify(observer, never()).onError(any(Throwable.class));
124+
inOrder.verify(observer, never()).onCompleted();
125+
}
126+
127+
}

0 commit comments

Comments
 (0)