PasswordEditText extends AppCompatEditText { public void setPasswordVisible(boolean visible) { passwordVisible = visible; setPasswordVisibility(); } PasswordEditText(Context context);  PasswordEditText(Context context, AttributeSet attrs);  PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr); @Override void setInputType(int type); @Override boolean onTouchEvent(MotionEvent event); boolean isPasswordVisible(); void setPasswordVisible(boolean visible); void togglePasswordVisibility(); @ColorInt int getTintColor(); void setTintColor(@ColorInt int tintColor); void setTintColorRes(@ColorRes int tintColorRes); void setShownDrawable(Drawable shownDrawable); void setShownDrawable(@DrawableRes int shownDrawableRes); void setHiddenDrawable(Drawable hiddenDrawable); void setHiddenDrawable(@DrawableRes int hiddenDrawableRes); void setToggleType(@ToggleType int toggleType); static final int TOGGLE_OPACITY; static final int TOGGLE_STRIKETHROUGH; }
@Test public void givenLeftToRightTouchWithinToggle_whenOnTouchEvent_thenPasswordVisibilityToggled() { passwordView.setPasswordVisible(false); float xPosition = passwordView.getRight() - 10; float yPosition = passwordView.getTop() + 10; fireActionUpTouchEvent(passwordView, xPosition, yPosition); assertThat(passwordView) .hasVisiblePassword(); }
@Test @Config(sdk = 16) public void givenLessThanSdk17TouchWithinToggle_whenOnTouchEvent_thenPasswordVisibilityToggled() { ViewCompat.setLayoutDirection(passwordView, ViewCompat.LAYOUT_DIRECTION_RTL); passwordView.setPasswordVisible(false); float xPosition = passwordView.getRight() - 10; float yPosition = passwordView.getTop() + 10; fireActionUpTouchEvent(passwordView, xPosition, yPosition); assertThat(passwordView) .hasVisiblePassword(); }
@Test public void givenTouchOutsideToggle_whenOnTouchEvent_thenPasswordVisibilityNotToggled() { passwordView.setPasswordVisible(false); float xPosition = passwordView.getLeft() + 10; float yPosition = passwordView.getTop() + 10; fireActionUpTouchEvent(passwordView, xPosition, yPosition); assertThat(passwordView) .hasHiddenPassword(); }
@Test public void givenTouchActionDown_whenOnTouchEvent_thenPasswordVisibilityNotToggled() { passwordView.setPasswordVisible(false); float xPosition = passwordView.getLeft() + 10; float yPosition = passwordView.getTop() + 10; fireActionDownTouchEvent(passwordView, xPosition, yPosition); assertThat(passwordView) .hasHiddenPassword(); }
@Test public void givenBeingSetToVisible_whenSetPasswordVisible_thenPasswordShownWithShownToggle() { passwordView.setPasswordVisible(true); assertThat(passwordView) .hasVisiblePassword() .hasToggleShownAlpha() .hasDefaultToggle(); }
@Test public void givenBeingSetToHidden_whenSetPasswordVisible_thenPasswordHiddenWithHiddenToggle() { passwordView.setPasswordVisible(true); passwordView.setPasswordVisible(false); assertThat(passwordView) .hasHiddenPassword() .hasToggleHiddenAlpha() .hasDefaultToggle(); }