AuthenticatorActivityTest.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * Copyright (C) 2015 ownCloud Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2,
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package com.owncloud.android.authentication;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.os.Bundle;
  23. import android.support.test.InstrumentationRegistry;
  24. import android.support.test.rule.ActivityTestRule;
  25. import android.support.test.runner.AndroidJUnit4;
  26. import android.test.suitebuilder.annotation.LargeTest;
  27. import static org.junit.Assert.assertTrue;
  28. import com.owncloud.android.R;
  29. import org.junit.Rule;
  30. import org.junit.Test;
  31. import org.junit.runner.RunWith;
  32. import java.lang.reflect.Field;
  33. import android.app.Activity;
  34. import static android.support.test.espresso.Espresso.onView;
  35. import static android.support.test.espresso.action.ViewActions.click;
  36. import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
  37. import static android.support.test.espresso.action.ViewActions.typeText;
  38. import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
  39. import static android.support.test.espresso.matcher.ViewMatchers.withId;
  40. import static android.support.test.espresso.assertion.ViewAssertions.matches;
  41. import static org.hamcrest.Matchers.not;
  42. @RunWith(AndroidJUnit4.class)
  43. @LargeTest
  44. public class AuthenticatorActivityTest {
  45. public static final String EXTRA_ACTION = "ACTION";
  46. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  47. private static final int WAIT_LOGIN = 5000;
  48. private static final String ERROR_MESSAGE = "Activity not finished";
  49. private static final String RESULT_CODE = "mResultCode";
  50. @Rule
  51. public ActivityTestRule<AuthenticatorActivity> mActivityRule = new ActivityTestRule<AuthenticatorActivity>(
  52. AuthenticatorActivity.class){
  53. @Override
  54. protected Intent getActivityIntent() {
  55. Context targetContext = InstrumentationRegistry.getInstrumentation()
  56. .getTargetContext();
  57. Intent result = new Intent(targetContext, AuthenticatorActivity.class);
  58. result.putExtra(EXTRA_ACTION, "");
  59. result.putExtra(EXTRA_ACCOUNT, "");
  60. return result;
  61. }
  62. };
  63. @Test
  64. public void check_login()
  65. throws InterruptedException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
  66. Bundle arguments = InstrumentationRegistry.getArguments();
  67. // Get values passed
  68. String testUser = arguments.getString("TEST_USER");
  69. String testPassword = arguments.getString("TEST_PASSWORD");
  70. String testServerURL = arguments.getString("TEST_SERVER_URL");
  71. // Check that login button is disabled
  72. onView(withId(R.id.buttonOK))
  73. .check(matches(not(isEnabled())));
  74. // Type server url
  75. onView(withId(R.id.hostUrlInput))
  76. .perform(typeText(testServerURL), closeSoftKeyboard());
  77. onView(withId(R.id.account_username)).perform(click());
  78. // Type user
  79. onView(withId(R.id.account_username))
  80. .perform(typeText(testUser), closeSoftKeyboard());
  81. // Type user pass
  82. onView(withId(R.id.account_password))
  83. .perform(typeText(testPassword), closeSoftKeyboard());
  84. onView(withId(R.id.buttonOK)).perform(click());
  85. // Check that the Activity ends after clicking
  86. Thread.sleep(WAIT_LOGIN);
  87. Field f = Activity.class.getDeclaredField(RESULT_CODE);
  88. f.setAccessible(true);
  89. int mResultCode = f.getInt(mActivityRule.getActivity());
  90. assertTrue(ERROR_MESSAGE, mResultCode == Activity.RESULT_OK);
  91. }
  92. }