Skip to content

Commit 9677ba7

Browse files
committed
Merge pull request ReactiveX#85 from hamidp/textview-actions
Add actions for setting TextView text.
2 parents f9c303e + cf5d27d commit 9677ba7

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

rxandroid/src/main/java/rx/android/view/ViewActions.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package rx.android.view;
1515

1616
import android.view.View;
17-
17+
import android.widget.TextView;
1818
import rx.functions.Action1;
1919

2020
/**
@@ -112,4 +112,28 @@ public static Action1<? super Boolean> setVisibility(View view) {
112112
public static Action1<? super Boolean> setVisibility(View view, int visibilityOnFalse) {
113113
return new ViewActionSetVisibility(view, visibilityOnFalse);
114114
}
115+
116+
/**
117+
* Set the text of a {@link TextView} based on values emitted by an Observable.
118+
*/
119+
public static Action1<? super CharSequence> setText(final TextView textView) {
120+
return new Action1<CharSequence>() {
121+
@Override
122+
public void call(CharSequence charSequence) {
123+
textView.setText(charSequence);
124+
}
125+
};
126+
}
127+
128+
/**
129+
* Set the text of a {@link TextView} based on values emitted by an Observable.
130+
*/
131+
public static Action1<? super Integer> setTextResource(final TextView textView) {
132+
return new Action1<Integer>() {
133+
@Override
134+
public void call(Integer integer) {
135+
textView.setText(integer);
136+
}
137+
};
138+
}
115139
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package rx.android.view;
2+
3+
import android.widget.TextView;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.Mockito;
8+
import org.robolectric.RobolectricTestRunner;
9+
import rx.subjects.PublishSubject;
10+
11+
import static org.mockito.Mockito.verify;
12+
13+
@RunWith(RobolectricTestRunner.class)
14+
public class ViewActionSetTextTest {
15+
16+
private TextView textView;
17+
18+
@Before
19+
public void setUp() {
20+
textView = Mockito.mock(TextView.class);
21+
}
22+
23+
@Test
24+
public void testSetsTextViewCharSequence() {
25+
final PublishSubject<String> subject = PublishSubject.create();
26+
subject.subscribe(ViewActions.setText(textView));
27+
28+
subject.onNext("Hello");
29+
verify(textView).setText("Hello");
30+
31+
subject.onNext("World");
32+
verify(textView).setText("World");
33+
}
34+
35+
@Test
36+
public void testSetsTextViewTextResource() {
37+
final PublishSubject<Integer> subject = PublishSubject.create();
38+
subject.subscribe(ViewActions.setTextResource(textView));
39+
40+
subject.onNext(1);
41+
verify(textView).setText(1);
42+
43+
subject.onNext(3);
44+
verify(textView).setText(3);
45+
}
46+
}

0 commit comments

Comments
 (0)