package com.philisense.rdgo.activity;

import android.databinding.DataBindingUtil;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.philisense.rdgo.BuildConfig;
import com.philisense.rdgo.R;
import com.philisense.rdgo.ui.putOut.PutOutOrderActivity;
import com.philisense.rdgo.ui.user.PasswordActivity;
import com.philisense.rdgo.viewModel.PasswordViewModel;
import com.philisense.rdgo.viewModel.PutOutOrderViewModel;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import static junit.framework.Assert.assertEquals;

/**
 * Created by shaop on 2017/12/19.
 */

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@SuppressStaticInitializationFor("io.realm.internal.Util")
public class PsswordActivityTest {
    private PasswordActivity activity;

    @Before
    public void setUp() {
        activity = Robolectric.setupActivity(PasswordActivity.class);
        activity.mBinding = DataBindingUtil.setContentView(activity, R.layout.activity_password);
        activity.viewModel = new PasswordViewModel(activity,
                RxTextView.textChanges(activity.mBinding.passwordOriginEdit),
                RxTextView.textChanges(activity.mBinding.passwordNewEdit),
                RxTextView.textChanges(activity.mBinding.passwordRenewEdit));

        // 初始化验证
        activity.initViewValidation(activity.mBinding);
    }

    /**
     * 缺少新密码
     */
    @Test
    public void testPasswordNoNew() {
        activity.mBinding.passwordOriginEdit.setText("123");
        activity.mBinding.passwordNewEdit.setText("");
        activity.mBinding.passwordRenewEdit.setText("23456");

        assertEquals(activity.mBinding.errorText.getText(),"请填写缺少的选项");
        assertEquals(activity.mBinding.passwordButton.isEnabled(),false);
    }

    /**
     * 缺少重复新密码
     */
    @Test
    public void testPasswordNoRenew() {
        activity.mBinding.passwordOriginEdit.setText("123");
        activity.mBinding.passwordNewEdit.setText("12345");
        activity.mBinding.passwordRenewEdit.setText("");

        assertEquals(activity.mBinding.errorText.getText(),"请填写缺少的选项");
        assertEquals(activity.mBinding.passwordButton.isEnabled(),false);
    }

    /**
     * 缺少原始密码
     */
    @Test
    public void testPasswordNoOrigin() {
        activity.mBinding.passwordOriginEdit.setText("");
        activity.mBinding.passwordNewEdit.setText("12334");
        activity.mBinding.passwordRenewEdit.setText("12345");

        assertEquals(activity.mBinding.errorText.getText(),"请填写缺少的选项");
        assertEquals(activity.mBinding.passwordButton.isEnabled(),false);
    }

    /**
     * 密码不一致
     */
    @Test
    public void testPasswordNotRepeat() {
        activity.mBinding.passwordOriginEdit.setText("123");
        activity.mBinding.passwordNewEdit.setText("1234");
        activity.mBinding.passwordRenewEdit.setText("12345");

        assertEquals(activity.mBinding.errorText.getText(),"两次密码不一致");
        assertEquals(activity.mBinding.passwordButton.isEnabled(),false);
    }

    /**
     * 全部一样
     */
    @Test
    public void testPasswordAllSame() {
        activity.mBinding.passwordOriginEdit.setText("123");
        activity.mBinding.passwordNewEdit.setText("123");
        activity.mBinding.passwordRenewEdit.setText("123");

        assertEquals(activity.mBinding.errorText.getText(),"新旧密码不能相同");
        assertEquals(activity.mBinding.passwordButton.isEnabled(),false);
    }

    /**
     * 正常输入
     */
    @Test
    public void testPasswordRight() {
        activity.mBinding.passwordOriginEdit.setText("123");
        activity.mBinding.passwordNewEdit.setText("12345");
        activity.mBinding.passwordRenewEdit.setText("12345");

        assertEquals(activity.mBinding.errorText.getText(),"");
        assertEquals(activity.mBinding.passwordButton.isEnabled(),true);
    }


}
