From dd42ef2ef969afe58cddd07eee64e8c4fee92d98 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Wed, 26 Nov 2014 23:26:16 -0800 Subject: [PATCH] Hide ViewActions implementation details. Javadoc normalization. --- .../rx/android/internal/Preconditions.java | 39 +++ .../android/view/ViewActionSetActivated.java | 29 -- .../android/view/ViewActionSetClickable.java | 29 -- .../rx/android/view/ViewActionSetEnabled.java | 29 -- .../android/view/ViewActionSetFocusable.java | 29 -- .../android/view/ViewActionSetSelected.java | 29 -- .../android/view/ViewActionSetVisibility.java | 43 --- .../java/rx/android/view/ViewActions.java | 157 +++++++---- .../view/ViewActionSetActivatedTest.java | 45 ---- .../view/ViewActionSetClickableTest.java | 45 ---- .../view/ViewActionSetEnabledTest.java | 45 ---- .../view/ViewActionSetFocusableTest.java | 45 ---- .../view/ViewActionSetSelectedTest.java | 45 ---- .../android/view/ViewActionSetTextTest.java | 46 ---- .../view/ViewActionSetVisibilityTest.java | 81 ------ .../java/rx/android/view/ViewActionsTest.java | 249 ++++++++++++++++++ 16 files changed, 391 insertions(+), 594 deletions(-) create mode 100644 rxandroid/src/main/java/rx/android/internal/Preconditions.java delete mode 100644 rxandroid/src/main/java/rx/android/view/ViewActionSetActivated.java delete mode 100644 rxandroid/src/main/java/rx/android/view/ViewActionSetClickable.java delete mode 100644 rxandroid/src/main/java/rx/android/view/ViewActionSetEnabled.java delete mode 100644 rxandroid/src/main/java/rx/android/view/ViewActionSetFocusable.java delete mode 100644 rxandroid/src/main/java/rx/android/view/ViewActionSetSelected.java delete mode 100644 rxandroid/src/main/java/rx/android/view/ViewActionSetVisibility.java delete mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionSetActivatedTest.java delete mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionSetClickableTest.java delete mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionSetEnabledTest.java delete mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionSetFocusableTest.java delete mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionSetSelectedTest.java delete mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionSetTextTest.java delete mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionSetVisibilityTest.java create mode 100644 rxandroid/src/test/java/rx/android/view/ViewActionsTest.java diff --git a/rxandroid/src/main/java/rx/android/internal/Preconditions.java b/rxandroid/src/main/java/rx/android/internal/Preconditions.java new file mode 100644 index 00000000..be9408c6 --- /dev/null +++ b/rxandroid/src/main/java/rx/android/internal/Preconditions.java @@ -0,0 +1,39 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package rx.android.internal; + +public final class Preconditions { + private Preconditions() { + throw new AssertionError("No instances"); + } + + public static T checkNotNull(T value, String message) { + if (value == null) { + throw new NullPointerException(message); + } + return value; + } + + public static void checkArgument(boolean check, String message) { + if (!check) { + throw new IllegalArgumentException(message); + } + } + + public static void checkState(boolean check, String message) { + if (!check) { + throw new IllegalStateException(message); + } + } +} diff --git a/rxandroid/src/main/java/rx/android/view/ViewActionSetActivated.java b/rxandroid/src/main/java/rx/android/view/ViewActionSetActivated.java deleted file mode 100644 index 73f3eba3..00000000 --- a/rxandroid/src/main/java/rx/android/view/ViewActionSetActivated.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -public class ViewActionSetActivated extends ViewAction1 { - - public ViewActionSetActivated(View view) { - super(view); - } - - @Override - public void call(View view, Boolean aBoolean) { - view.setActivated(aBoolean); - } -} - diff --git a/rxandroid/src/main/java/rx/android/view/ViewActionSetClickable.java b/rxandroid/src/main/java/rx/android/view/ViewActionSetClickable.java deleted file mode 100644 index 7b9ec321..00000000 --- a/rxandroid/src/main/java/rx/android/view/ViewActionSetClickable.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -public class ViewActionSetClickable extends ViewAction1 { - - public ViewActionSetClickable(View view) { - super(view); - } - - @Override - public void call(View view, Boolean aBoolean) { - view.setClickable(aBoolean); - } -} - diff --git a/rxandroid/src/main/java/rx/android/view/ViewActionSetEnabled.java b/rxandroid/src/main/java/rx/android/view/ViewActionSetEnabled.java deleted file mode 100644 index 3947e5e1..00000000 --- a/rxandroid/src/main/java/rx/android/view/ViewActionSetEnabled.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -public class ViewActionSetEnabled extends ViewAction1 { - - public ViewActionSetEnabled(View view) { - super(view); - } - - @Override - public void call(View view, Boolean aBoolean) { - view.setEnabled(aBoolean); - } -} - diff --git a/rxandroid/src/main/java/rx/android/view/ViewActionSetFocusable.java b/rxandroid/src/main/java/rx/android/view/ViewActionSetFocusable.java deleted file mode 100644 index ad5d8e82..00000000 --- a/rxandroid/src/main/java/rx/android/view/ViewActionSetFocusable.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -public class ViewActionSetFocusable extends ViewAction1 { - - public ViewActionSetFocusable(View view) { - super(view); - } - - @Override - public void call(View view, Boolean aBoolean) { - view.setFocusable(aBoolean); - } -} - diff --git a/rxandroid/src/main/java/rx/android/view/ViewActionSetSelected.java b/rxandroid/src/main/java/rx/android/view/ViewActionSetSelected.java deleted file mode 100644 index f410bfac..00000000 --- a/rxandroid/src/main/java/rx/android/view/ViewActionSetSelected.java +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -public class ViewActionSetSelected extends ViewAction1 { - - public ViewActionSetSelected(View view) { - super(view); - } - - @Override - public void call(View view, Boolean aBoolean) { - view.setSelected(aBoolean); - } -} - diff --git a/rxandroid/src/main/java/rx/android/view/ViewActionSetVisibility.java b/rxandroid/src/main/java/rx/android/view/ViewActionSetVisibility.java deleted file mode 100644 index bae97018..00000000 --- a/rxandroid/src/main/java/rx/android/view/ViewActionSetVisibility.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -public class ViewActionSetVisibility extends ViewAction1 { - - private final int visibilityOnFalse; - - public ViewActionSetVisibility(View view) { - this(view, View.GONE); - } - - public ViewActionSetVisibility(View view, int visibilityOnFalse) { - super(view); - if (visibilityOnFalse != View.GONE && - visibilityOnFalse != View.INVISIBLE && - visibilityOnFalse != View.VISIBLE) { - throw new IllegalArgumentException(visibilityOnFalse + - " is not a valid visibility value. See android.view.View VISIBLE, GONE, and INVISIBLE"); - } - this.visibilityOnFalse = visibilityOnFalse; - } - - @Override - public void call(View view, Boolean aBoolean) { - int visibility = aBoolean ? View.VISIBLE : visibilityOnFalse; - view.setVisibility(visibility); - } -} - diff --git a/rxandroid/src/main/java/rx/android/view/ViewActions.java b/rxandroid/src/main/java/rx/android/view/ViewActions.java index 280747b3..d2c130e7 100644 --- a/rxandroid/src/main/java/rx/android/view/ViewActions.java +++ b/rxandroid/src/main/java/rx/android/view/ViewActions.java @@ -17,8 +17,11 @@ import android.widget.TextView; import rx.functions.Action1; +import static rx.android.internal.Preconditions.checkNotNull; +import static rx.android.internal.Preconditions.checkArgument; + /** - * Utility class for the Action interfaces for use with Android {@link View}s. + * Utility class for the {@code Action} interfaces for use with {@linkplain View views}. */ public final class ViewActions { @@ -27,96 +30,136 @@ private ViewActions() { } /** - * Set whether a view is enabled based on {@code boolean} values that are emitted by an - * Observable. - * - * @param view - * to modify - * @return an {@link Action1} that sets the enabled of a view when boolean values are emitted. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain View#setEnabled enabled property}. + *

+ * Note: the created action will not keep a strong reference to the view. */ public static Action1 setEnabled(View view) { - return new ViewActionSetEnabled(view); + checkNotNull(view, "view"); + return new ViewAction1(view) { + @Override + public void call(View view, Boolean enabled) { + view.setEnabled(enabled); + } + }; } /** - * Set whether a view is activated based on {@code boolean} values that are emitted by an - * Observable. - * - * @param view - * to modify - * @return an {@link Action1} that sets the activated of a view when boolean values are emitted. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain View#setActivated activated property}. + *

+ * Note: the created action will not keep a strong reference to the view. */ public static Action1 setActivated(View view) { - return new ViewActionSetActivated(view); + checkNotNull(view, "view"); + return new ViewAction1(view) { + @Override + public void call(View view, Boolean activated) { + view.setActivated(activated); + } + }; } /** - * Set whether a view is clickable based on {@code boolean} values that are emitted by an - * Observable. - * - * @param view - * to modify - * @return an {@link Action1} that sets the clickable of a view when boolean values are emitted. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain View#setClickable clickable property}. + *

+ * Note: the created action will not keep a strong reference to the view. */ public static Action1 setClickable(View view) { - return new ViewActionSetClickable(view); + checkNotNull(view, "view"); + return new ViewAction1(view) { + @Override + public void call(View view, Boolean clickable) { + view.setClickable(clickable); + } + }; } /** - * Set whether a view is focusable based on {@code boolean} values that are emitted by an - * Observable. - * - * @param view - * to modify - * @return an {@link Action1} that sets the focusable of a view when boolean values are emitted. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain View#setFocusable focusable property}. + *

+ * Note: the created action will not keep a strong reference to the view. */ public static Action1 setFocusable(View view) { - return new ViewActionSetFocusable(view); + checkNotNull(view, "view"); + return new ViewAction1(view) { + @Override + public void call(View view, Boolean focusable) { + view.setFocusable(focusable); + } + }; } /** - * Set whether a view is selected based on {@code boolean} values that are emitted by an - * Observable. - * - * @param view - * to modify - * @return an {@link Action1} that sets the selected of a view when boolean values are emitted. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain View#setSelected selected property}. + *

+ * Note: the created action will not keep a strong reference to the view. */ public static Action1 setSelected(View view) { - return new ViewActionSetSelected(view); + checkNotNull(view, "view"); + return new ViewAction1(view) { + @Override + public void call(View view, Boolean selected) { + view.setSelected(selected); + } + }; } /** - * Set the visibility of a view based on {@code boolean} values that are emitted by an - * Observable. When {@code false} is emitted, visibility is set to {@link View#GONE}. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain View#setVisibility visibility property}. {@code true} values are mapped to + * {@link View#VISIBLE} and {@code false} values to {@link View#GONE}. + *

+ * Note: the created action will not keep a strong reference to the view. * - * @param view - * to modify - * @return an {@link Action1} that sets the visibility of a view when boolean values are emitted. + * @see #setVisibility(View, int) */ public static Action1 setVisibility(View view) { - return new ViewActionSetVisibility(view); + return setVisibility(view, View.GONE); } /** - * Set the visibility of a view based on {@code boolean} values that are emitted by an - * Observable. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain View#setVisibility visibility property}. {@code true} values are mapped to + * {@link View#VISIBLE} and {@code false} values to the supplied visibility. + *

+ * Note: the created action will not keep a strong reference to the view. * - * @param view - * to modify - * @param visibilityOnFalse - * value to use on {@link View#setVisibility(int)} when a {@code false} is emitted by - * the {@link rx.Observable} - * @return an {@link Action1} that sets the visibility of a view when boolean values are emitted. + * @param visibilityOnFalse {@link View#INVISIBLE} or {@link View#GONE}. + * + * @see #setVisibility(View, int) */ - public static Action1 setVisibility(View view, int visibilityOnFalse) { - return new ViewActionSetVisibility(view, visibilityOnFalse); + public static Action1 setVisibility(View view, final int visibilityOnFalse) { + checkNotNull(view, "view"); + // TODO use support annotation's validation when we become a real Android library. + checkArgument(visibilityOnFalse != View.VISIBLE, + "Binding false to VISIBLE has no effect and is thus disallowed."); + if (visibilityOnFalse != View.INVISIBLE && visibilityOnFalse != View.GONE) { + throw new IllegalArgumentException(visibilityOnFalse + " is not a valid visibility value."); + } + return new ViewAction1(view) { + @Override + public void call(View view, Boolean value) { + int visibility = value ? View.VISIBLE : visibilityOnFalse; + view.setVisibility(visibility); + } + }; } /** - * Set the text of a {@link TextView} based on values emitted by an Observable. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain TextView#setText(CharSequence) text property}. + *

+ * Note: the created action will not keep a strong reference to the view. + * + * @see #setTextResource */ public static Action1 setText(TextView textView) { + checkNotNull(textView, "textView"); return new ViewAction1(textView) { @Override public void call(TextView view, CharSequence text) { @@ -126,9 +169,15 @@ public void call(TextView view, CharSequence text) { } /** - * Set the text of a {@link TextView} based on values emitted by an Observable. + * Create an {@linkplain Action1 action} which controls the supplied view's + * {@linkplain TextView#setText(int) text property}. + *

+ * Note: the created action will not keep a strong reference to the view. + * + * @see #setText */ public static Action1 setTextResource(TextView textView) { + checkNotNull(textView, "textView"); return new ViewAction1(textView) { @Override public void call(TextView view, Integer resId) { diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionSetActivatedTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionSetActivatedTest.java deleted file mode 100644 index fa724bbd..00000000 --- a/rxandroid/src/test/java/rx/android/view/ViewActionSetActivatedTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import rx.subjects.PublishSubject; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static rx.android.TestUtil.createView; - -@RunWith(RobolectricTestRunner.class) -public class ViewActionSetActivatedTest { - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewActivated() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setActivated(view)); - - assertFalse(view.isActivated()); - subject.onNext(true); - assertTrue(view.isActivated()); - subject.onNext(false); - assertFalse(view.isActivated()); - } - -} diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionSetClickableTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionSetClickableTest.java deleted file mode 100644 index 5f17d22c..00000000 --- a/rxandroid/src/test/java/rx/android/view/ViewActionSetClickableTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import rx.subjects.PublishSubject; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static rx.android.TestUtil.createView; - -@RunWith(RobolectricTestRunner.class) -public class ViewActionSetClickableTest { - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewClickable() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setClickable(view)); - - assertFalse(view.isClickable()); - subject.onNext(true); - assertTrue(view.isClickable()); - subject.onNext(false); - assertFalse(view.isClickable()); - } - -} diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionSetEnabledTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionSetEnabledTest.java deleted file mode 100644 index 98279bec..00000000 --- a/rxandroid/src/test/java/rx/android/view/ViewActionSetEnabledTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import rx.subjects.PublishSubject; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static rx.android.TestUtil.createView; - -@RunWith(RobolectricTestRunner.class) -public class ViewActionSetEnabledTest { - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewEnabled() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setEnabled(view)); - - assertTrue(view.isEnabled()); - subject.onNext(false); - assertFalse(view.isEnabled()); - subject.onNext(true); - assertTrue(view.isEnabled()); - } - -} diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionSetFocusableTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionSetFocusableTest.java deleted file mode 100644 index 1c700fa7..00000000 --- a/rxandroid/src/test/java/rx/android/view/ViewActionSetFocusableTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import rx.subjects.PublishSubject; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static rx.android.TestUtil.createView; - -@RunWith(RobolectricTestRunner.class) -public class ViewActionSetFocusableTest { - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewFocusable() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setFocusable(view)); - - assertFalse(view.isFocusable()); - subject.onNext(true); - assertTrue(view.isFocusable()); - subject.onNext(false); - assertFalse(view.isFocusable()); - } - -} diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionSetSelectedTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionSetSelectedTest.java deleted file mode 100644 index 9c69bda6..00000000 --- a/rxandroid/src/test/java/rx/android/view/ViewActionSetSelectedTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import rx.subjects.PublishSubject; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static rx.android.TestUtil.createView; - -@RunWith(RobolectricTestRunner.class) -public class ViewActionSetSelectedTest { - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewSelected() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setSelected(view)); - - assertFalse(view.isSelected()); - subject.onNext(true); - assertTrue(view.isSelected()); - subject.onNext(false); - assertFalse(view.isSelected()); - } - -} diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionSetTextTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionSetTextTest.java deleted file mode 100644 index cbf9755e..00000000 --- a/rxandroid/src/test/java/rx/android/view/ViewActionSetTextTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package rx.android.view; - -import android.widget.TextView; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.robolectric.RobolectricTestRunner; -import rx.subjects.PublishSubject; - -import static org.mockito.Mockito.verify; - -@RunWith(RobolectricTestRunner.class) -public class ViewActionSetTextTest { - - private TextView textView; - - @Before - public void setUp() { - textView = Mockito.mock(TextView.class); - } - - @Test - public void testSetsTextViewCharSequence() { - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setText(textView)); - - subject.onNext("Hello"); - verify(textView).setText("Hello"); - - subject.onNext("World"); - verify(textView).setText("World"); - } - - @Test - public void testSetsTextViewTextResource() { - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setTextResource(textView)); - - subject.onNext(1); - verify(textView).setText(1); - - subject.onNext(3); - verify(textView).setText(3); - } -} diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionSetVisibilityTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionSetVisibilityTest.java deleted file mode 100644 index 80728206..00000000 --- a/rxandroid/src/test/java/rx/android/view/ViewActionSetVisibilityTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package rx.android.view; - -import android.view.View; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import rx.subjects.PublishSubject; - -import static org.junit.Assert.assertEquals; -import static rx.android.TestUtil.createView; - -@RunWith(RobolectricTestRunner.class) -public class ViewActionSetVisibilityTest { - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewVisibility() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setVisibility(view)); - - assertEquals(View.VISIBLE, view.getVisibility()); - subject.onNext(false); - assertEquals(View.GONE, view.getVisibility()); - subject.onNext(true); - assertEquals(View.VISIBLE, view.getVisibility()); - } - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewVisibilityWithCustomVisibility() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setVisibility(view, View.INVISIBLE)); - - assertEquals(View.VISIBLE, view.getVisibility()); - subject.onNext(false); - assertEquals(View.INVISIBLE, view.getVisibility()); - subject.onNext(true); - assertEquals(View.VISIBLE, view.getVisibility()); - } - - @Test - @SuppressWarnings("unchecked") - public void testSetsViewVisibilityWithVisibleAsFalse() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - subject.subscribe(ViewActions.setVisibility(view, View.VISIBLE)); - - assertEquals(View.VISIBLE, view.getVisibility()); - subject.onNext(false); - assertEquals(View.VISIBLE, view.getVisibility()); // shouldn't change - subject.onNext(true); - assertEquals(View.VISIBLE, view.getVisibility()); - } - - @Test(expected = IllegalArgumentException.class) - @SuppressWarnings("unchecked") - public void testSetsViewVisibilityOnlyAcceptsValidVisibilities() { - final View view = createView(); - final PublishSubject subject = PublishSubject.create(); - int invalidVisibility = 123456; - subject.subscribe(ViewActions.setVisibility(view, invalidVisibility)); - } - -} diff --git a/rxandroid/src/test/java/rx/android/view/ViewActionsTest.java b/rxandroid/src/test/java/rx/android/view/ViewActionsTest.java new file mode 100644 index 00000000..9ca4760d --- /dev/null +++ b/rxandroid/src/test/java/rx/android/view/ViewActionsTest.java @@ -0,0 +1,249 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package rx.android.view; + +import android.view.View; +import android.widget.TextView; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import rx.subjects.PublishSubject; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static rx.android.TestUtil.createView; + +@RunWith(RobolectricTestRunner.class) +public class ViewActionsTest { + + @Test + @SuppressWarnings("unchecked") + public void activated() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setActivated(view)); + + assertFalse(view.isActivated()); + subject.onNext(true); + assertTrue(view.isActivated()); + subject.onNext(false); + assertFalse(view.isActivated()); + } + + @Test + public void activatedRejectsNull() { + try { + ViewActions.setActivated(null); + } catch (NullPointerException e) { + assertEquals("view", e.getMessage()); + } + } + + @Test + @SuppressWarnings("unchecked") + public void clickable() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setClickable(view)); + + assertFalse(view.isClickable()); + subject.onNext(true); + assertTrue(view.isClickable()); + subject.onNext(false); + assertFalse(view.isClickable()); + } + + @Test + public void clickableRejectsNull() { + try { + ViewActions.setClickable(null); + } catch (NullPointerException e) { + assertEquals("view", e.getMessage()); + } + } + + @Test + @SuppressWarnings("unchecked") + public void enabled() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setEnabled(view)); + + assertTrue(view.isEnabled()); + subject.onNext(false); + assertFalse(view.isEnabled()); + subject.onNext(true); + assertTrue(view.isEnabled()); + } + + @Test + public void enabledRejectsNull() { + try { + ViewActions.setEnabled(null); + } catch (NullPointerException e) { + assertEquals("view", e.getMessage()); + } + } + + @Test + @SuppressWarnings("unchecked") + public void focusable() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setFocusable(view)); + + assertFalse(view.isFocusable()); + subject.onNext(true); + assertTrue(view.isFocusable()); + subject.onNext(false); + assertFalse(view.isFocusable()); + } + + @Test + public void focusableRejectsNull() { + try { + ViewActions.setFocusable(null); + } catch (NullPointerException e) { + assertEquals("view", e.getMessage()); + } + } + + @Test + @SuppressWarnings("unchecked") + public void selected() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setSelected(view)); + + assertFalse(view.isSelected()); + subject.onNext(true); + assertTrue(view.isSelected()); + subject.onNext(false); + assertFalse(view.isSelected()); + } + + @Test + public void selectedRejectsNull() { + try { + ViewActions.setSelected(null); + } catch (NullPointerException e) { + assertEquals("view", e.getMessage()); + } + } + + @Test + public void text() { + TextView textView = mock(TextView.class); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setText(textView)); + + subject.onNext("Hello"); + verify(textView).setText("Hello"); + + subject.onNext("World"); + verify(textView).setText("World"); + } + + @Test + public void textRejectsNull() { + try { + ViewActions.setText(null); + } catch (NullPointerException e) { + assertEquals("textView", e.getMessage()); + } + } + + @Test + public void textResource() { + TextView textView = mock(TextView.class); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setTextResource(textView)); + + subject.onNext(1); + verify(textView).setText(1); + + subject.onNext(3); + verify(textView).setText(3); + } + + @Test + public void textResourceRejectsNull() { + try { + ViewActions.setTextResource(null); + } catch (NullPointerException e) { + assertEquals("textView", e.getMessage()); + } + } + + @Test + @SuppressWarnings("unchecked") + public void visibility() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setVisibility(view)); + + assertEquals(View.VISIBLE, view.getVisibility()); + subject.onNext(false); + assertEquals(View.GONE, view.getVisibility()); + subject.onNext(true); + assertEquals(View.VISIBLE, view.getVisibility()); + } + + @Test + public void visibilityRejectsNull() { + try { + ViewActions.setVisibility(null); + } catch (NullPointerException e) { + assertEquals("view", e.getMessage()); + } + } + + @Test + @SuppressWarnings("unchecked") + public void visibilityWithCustomFalseValue() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + subject.subscribe(ViewActions.setVisibility(view, View.INVISIBLE)); + + assertEquals(View.VISIBLE, view.getVisibility()); + subject.onNext(false); + assertEquals(View.INVISIBLE, view.getVisibility()); + subject.onNext(true); + assertEquals(View.VISIBLE, view.getVisibility()); + } + + @Test + public void visibilityInvalidValues() { + View view = createView(); + try { + ViewActions.setVisibility(view, View.VISIBLE); + fail(); + } catch (IllegalArgumentException e) { + assertEquals("Binding false to VISIBLE has no effect and is thus disallowed.", e.getMessage()); + } + try { + ViewActions.setVisibility(view, 42); + fail(); + } catch (IllegalArgumentException e) { + assertEquals("42 is not a valid visibility value.", e.getMessage()); + } + } + +}