diff --git a/src/main/java/rx/functions/ViewAction1.java b/src/main/java/rx/functions/ViewAction1.java new file mode 100644 index 00000000..57670d5a --- /dev/null +++ b/src/main/java/rx/functions/ViewAction1.java @@ -0,0 +1,51 @@ +/** + * 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.functions; + +import android.view.View; + +import rx.functions.Action1; + +import java.lang.ref.WeakReference; + +/** + * An {@link Action1} implementation specific for {@link View}s. + * + * @param the type of {@link View} upon which to perform the action. + * @param the type being observed + */ +public abstract class ViewAction1 implements Action1 { + + private final WeakReference viewReference; + + public ViewAction1(V view) { + viewReference = new WeakReference(view); + } + + @Override + public final void call(T t) { + V view = viewReference.get(); + if (view != null) { + call(view, t); + } + } + + /** + * Implement this instead of {@link Action1#call(Object)}. + * @param view the view given in the constructor. + * @param t the object being observed + */ + public abstract void call(V view, T t); + +} diff --git a/src/main/java/rx/functions/ViewActionSetActivated.java b/src/main/java/rx/functions/ViewActionSetActivated.java new file mode 100644 index 00000000..4386017a --- /dev/null +++ b/src/main/java/rx/functions/ViewActionSetActivated.java @@ -0,0 +1,29 @@ +/** + * 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.functions; + +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/src/main/java/rx/functions/ViewActionSetClickable.java b/src/main/java/rx/functions/ViewActionSetClickable.java new file mode 100644 index 00000000..a093e6db --- /dev/null +++ b/src/main/java/rx/functions/ViewActionSetClickable.java @@ -0,0 +1,29 @@ +/** + * 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.functions; + +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/src/main/java/rx/functions/ViewActionSetEnabled.java b/src/main/java/rx/functions/ViewActionSetEnabled.java new file mode 100644 index 00000000..e7e504b3 --- /dev/null +++ b/src/main/java/rx/functions/ViewActionSetEnabled.java @@ -0,0 +1,29 @@ +/** + * 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.functions; + +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/src/main/java/rx/functions/ViewActionSetFocusable.java b/src/main/java/rx/functions/ViewActionSetFocusable.java new file mode 100644 index 00000000..f02743cd --- /dev/null +++ b/src/main/java/rx/functions/ViewActionSetFocusable.java @@ -0,0 +1,29 @@ +/** + * 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.functions; + +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/src/main/java/rx/functions/ViewActionSetSelected.java b/src/main/java/rx/functions/ViewActionSetSelected.java new file mode 100644 index 00000000..c9da4bed --- /dev/null +++ b/src/main/java/rx/functions/ViewActionSetSelected.java @@ -0,0 +1,29 @@ +/** + * 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.functions; + +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/src/main/java/rx/functions/ViewActionSetVisibility.java b/src/main/java/rx/functions/ViewActionSetVisibility.java new file mode 100644 index 00000000..aa389500 --- /dev/null +++ b/src/main/java/rx/functions/ViewActionSetVisibility.java @@ -0,0 +1,43 @@ +/** + * 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.functions; + +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/src/main/java/rx/functions/ViewActions.java b/src/main/java/rx/functions/ViewActions.java new file mode 100644 index 00000000..03fcd0b2 --- /dev/null +++ b/src/main/java/rx/functions/ViewActions.java @@ -0,0 +1,115 @@ +/** + * 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.functions; + +import android.view.View; + +import rx.functions.Action1; + +/** + * Utility class for the Action interfaces for use with Android {@link View}s. + */ +public final class ViewActions { + + private ViewActions() { + throw new IllegalStateException("No instances!"); + } + + /** + * 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. + */ + public static Action1 setEnabled(View view) { + return new ViewActionSetEnabled(view); + } + + /** + * 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. + */ + public static Action1 setActivated(View view) { + return new ViewActionSetActivated(view); + } + + /** + * 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. + */ + public static Action1 setClickable(View view) { + return new ViewActionSetClickable(view); + } + + /** + * 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. + */ + public static Action1 setFocusable(View view) { + return new ViewActionSetFocusable(view); + } + + /** + * 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. + */ + public static Action1 setSelected(View view) { + return new ViewActionSetSelected(view); + } + + /** + * 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}. + * + * @param view + * to modify + * @return an {@link Action1} that sets the visibility of a view when boolean values are emitted. + */ + public static Action1 setVisibility(View view) { + return new ViewActionSetVisibility(view); + } + + /** + * Set the visibility of a view based on {@code boolean} values that are emitted by an + * Observable. + * + * @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. + */ + public static Action1 setVisibility(View view, int visibilityOnFalse) { + return new ViewActionSetVisibility(view, visibilityOnFalse); + } +} diff --git a/src/test/java/rx/TestUtil.java b/src/test/java/rx/TestUtil.java new file mode 100644 index 00000000..ebce3120 --- /dev/null +++ b/src/test/java/rx/TestUtil.java @@ -0,0 +1,30 @@ +/** + * 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; + +import android.view.View; + +import org.robolectric.Robolectric; + +public class TestUtil { + + private TestUtil() { + throw new AssertionError("Utility class"); + } + + public static View createView() { + return new View(Robolectric.application); + } + +} diff --git a/src/test/java/rx/functions/ViewAction1Test.java b/src/test/java/rx/functions/ViewAction1Test.java new file mode 100644 index 00000000..7c03d01f --- /dev/null +++ b/src/test/java/rx/functions/ViewAction1Test.java @@ -0,0 +1,73 @@ +/** + * 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.functions; + +import android.view.View; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +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 ViewAction1Test { + + @Test + @SuppressWarnings("unchecked") + public void callIsNotExecutedWithAReleasedReference() { + final View view = null; // simulate a released WeakReference + final PublishSubject subject = PublishSubject.create(); + final ViewAction1Impl action = new ViewAction1Impl(view); + subject.subscribe(action); + + assertFalse(action.wasCalled); + subject.onNext(true); + assertFalse(action.wasCalled); + } + + @Test + @SuppressWarnings("unchecked") + public void callIsExecutedWithARetainedReference() { + final View view = createView(); + final PublishSubject subject = PublishSubject.create(); + final ViewAction1Impl action = new ViewAction1Impl(view); + subject.subscribe(action); + + assertFalse(action.wasCalled); + subject.onNext(true); + assertTrue(action.wasCalled); + } + + private static class ViewAction1Impl extends ViewAction1 { + + boolean wasCalled = false; + + ViewAction1Impl(View view) { + super(view); + } + + @Override + public void call(View view, Boolean aBoolean) { + wasCalled = true; + } + + } + +} diff --git a/src/test/java/rx/functions/ViewActionSetActivatedTest.java b/src/test/java/rx/functions/ViewActionSetActivatedTest.java new file mode 100644 index 00000000..ce7b0383 --- /dev/null +++ b/src/test/java/rx/functions/ViewActionSetActivatedTest.java @@ -0,0 +1,46 @@ +/** + * 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.functions; + +import android.view.View; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +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/src/test/java/rx/functions/ViewActionSetClickableTest.java b/src/test/java/rx/functions/ViewActionSetClickableTest.java new file mode 100644 index 00000000..fdd12556 --- /dev/null +++ b/src/test/java/rx/functions/ViewActionSetClickableTest.java @@ -0,0 +1,46 @@ +/** + * 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.functions; + +import android.view.View; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +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/src/test/java/rx/functions/ViewActionSetEnabledTest.java b/src/test/java/rx/functions/ViewActionSetEnabledTest.java new file mode 100644 index 00000000..366259c9 --- /dev/null +++ b/src/test/java/rx/functions/ViewActionSetEnabledTest.java @@ -0,0 +1,46 @@ +/** + * 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.functions; + +import android.view.View; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +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/src/test/java/rx/functions/ViewActionSetFocusableTest.java b/src/test/java/rx/functions/ViewActionSetFocusableTest.java new file mode 100644 index 00000000..05861b1f --- /dev/null +++ b/src/test/java/rx/functions/ViewActionSetFocusableTest.java @@ -0,0 +1,46 @@ +/** + * 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.functions; + +import android.view.View; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +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/src/test/java/rx/functions/ViewActionSetSelectedTest.java b/src/test/java/rx/functions/ViewActionSetSelectedTest.java new file mode 100644 index 00000000..70e066af --- /dev/null +++ b/src/test/java/rx/functions/ViewActionSetSelectedTest.java @@ -0,0 +1,46 @@ +/** + * 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.functions; + +import android.view.View; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +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/src/test/java/rx/functions/ViewActionSetVisibilityTest.java b/src/test/java/rx/functions/ViewActionSetVisibilityTest.java new file mode 100644 index 00000000..0aa04eb5 --- /dev/null +++ b/src/test/java/rx/functions/ViewActionSetVisibilityTest.java @@ -0,0 +1,82 @@ +/** + * 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.functions; + +import android.view.View; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +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)); + } + +}