/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * 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 android.support.design.widget;

import android.app.Activity;
import android.support.design.test.R;
import android.support.test.annotation.UiThreadTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.EditText;

import org.junit.Test;

import static android.support.design.testutils.TestUtilsActions.setText;
import static android.support.design.testutils.TextInputLayoutActions.setError;
import static android.support.design.testutils.TextInputLayoutActions.setErrorEnabled;
import static android.support.design.testutils.TextInputLayoutActions.setPasswordVisibilityToggleEnabled;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

@SmallTest
public class TextInputLayoutTest extends BaseInstrumentationTestCase<TextInputLayoutActivity> {

    private static final String ERROR_MESSAGE_1 = "An error has occured";
    private static final String ERROR_MESSAGE_2 = "Some other error has occured";

    private static final String INPUT_TEXT = "Random input text";

    public TextInputLayoutTest() {
        super(TextInputLayoutActivity.class);
    }

    @Test
    public void testSetErrorEnablesErrorIsDisplayed() {
        onView(withId(R.id.textinput)).perform(setError(ERROR_MESSAGE_1));
        onView(withText(ERROR_MESSAGE_1)).check(matches(isDisplayed()));
    }

    @Test
    public void testDisabledErrorIsNotDisplayed() {
        // First show an error, and then disable error functionality
        onView(withId(R.id.textinput))
                .perform(setError(ERROR_MESSAGE_1))
                .perform(setErrorEnabled(false));

        // Check that the error is no longer there
        onView(withText(ERROR_MESSAGE_1)).check(doesNotExist());
    }

    @Test
    public void testSetErrorOnDisabledSetErrorIsDisplayed() {
        // First show an error, and then disable error functionality
        onView(withId(R.id.textinput))
                .perform(setError(ERROR_MESSAGE_1))
                .perform(setErrorEnabled(false));

        // Now show a different error message
        onView(withId(R.id.textinput)).perform(setError(ERROR_MESSAGE_2));
        // And check that it is displayed
        onView(withText(ERROR_MESSAGE_2)).check(matches(isDisplayed()));
    }

    @Test
    @UiThreadTest
    public void testPasswordToggleClick() {
        // Set some text on the EditText
        onView(withId(R.id.textinput_edittext_pwd)).perform(setText(INPUT_TEXT));

        final Activity activity = mActivityTestRule.getActivity();
        final EditText textInput = (EditText) activity.findViewById(R.id.textinput_edittext_pwd);

        // Assert that the password is disguised
        assertNotEquals(INPUT_TEXT, textInput.getLayout().getText());

        // Now click the toggle button
        onView(withId(R.id.text_input_password_toggle)).perform(click());

        // And assert that the password is not disguised
        assertEquals(INPUT_TEXT, textInput.getLayout().getText());
    }

    @Test
    @UiThreadTest
    public void testPasswordToggleDisable() {
        final Activity activity = mActivityTestRule.getActivity();
        final EditText textInput = (EditText) activity.findViewById(R.id.textinput_edittext_pwd);

        // Set some text on the EditText
        onView(withId(R.id.textinput_edittext_pwd)).perform(setText(INPUT_TEXT));
        // Assert that the password is disguised
        assertNotEquals(INPUT_TEXT, textInput.getLayout().getText());

        // Disable the password toggle
        onView(withId(R.id.text_input_password_toggle))
                .perform(setPasswordVisibilityToggleEnabled(false));

        // Check that the password toggle view is not visible
        onView(withId(R.id.text_input_password_toggle)).check(matches(not(isDisplayed())));
        // ...and that the password is not disguised
        assertEquals(INPUT_TEXT, textInput.getLayout().getText());
    }
}

@@ -3458,34 +3458,16 @@
   @Test%0A
-    @UiThreadTest%0A
     publ
@@ -3900,32 +3900,43 @@
 yout().getText()
+.toString()
 );%0A%0A        // N
@@ -4150,32 +4150,43 @@
 yout().getText()
+.toString()
 );%0A    %7D%0A%0A    @T
@@ -4193,26 +4193,8 @@
 est%0A
-    @UiThreadTest%0A
     
@@ -4640,16 +4640,27 @@
 etText()
+.toString()
 );%0A%0A    
@@ -4717,33 +4717,32 @@
 withId(R.id.text
-_
 input_password_t
@@ -4739,23 +4739,16 @@
 password
-_toggle
 ))%0A     
@@ -4997,35 +4997,530 @@
 the password is 
-not
+disguised still%0A        assertNotEquals(INPUT_TEXT, textInput.getLayout().getText().toString());%0A    %7D%0A%0A    @Test%0A    public void testPasswordToggleDisableWhenVisible() %7B%0A        final Activity activity = mActivityTestRule.getActivity();%0A        final EditText textInput = (EditText) activity.findViewById(R.id.textinput_edittext_pwd);%0A%0A        // Set some text on the EditText%0A        onView(withId(R.id.textinput_edittext_pwd)).perform(setText(INPUT_TEXT));%0A        // Assert that the password is
  disguised%0A     
@@ -5520,32 +5520,35 @@
 d%0A        assert
+Not
 Equals(INPUT_TEX
@@ -5581,16 +5581,433 @@
 etText()
+.toString());%0A%0A        // Now click the toggle button%0A        onView(withId(R.id.text_input_password_toggle)).perform(click());%0A        // Disable the password toggle%0A        onView(withId(R.id.textinput_password))%0A                .perform(setPasswordVisibilityToggleEnabled(false));%0A%0A        // Check that the password is disguised again%0A        assertNotEquals(INPUT_TEXT, textInput.getLayout().getText().toString()
 );%0A    %7D

Fix TextInputLayout password tests