AuthenticatorActivityTest.java 3.9 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.net.Uri;
  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 com.owncloud.android.BuildConfig;
  28. import static org.junit.Assert.assertTrue;
  29. import com.owncloud.android.R;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import static org.junit.Assert.assertTrue;
  34. import java.lang.reflect.Field;
  35. import android.app.Activity;
  36. import static android.support.test.espresso.Espresso.onView;
  37. import static android.support.test.espresso.action.ViewActions.click;
  38. import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
  39. import static android.support.test.espresso.action.ViewActions.typeText;
  40. import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
  41. import static android.support.test.espresso.matcher.ViewMatchers.withId;
  42. import static android.support.test.espresso.assertion.ViewAssertions.matches;
  43. import static org.hamcrest.Matchers.not;
  44. @RunWith(AndroidJUnit4.class)
  45. @LargeTest
  46. public class AuthenticatorActivityTest {
  47. public static final String EXTRA_ACTION = "ACTION";
  48. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  49. private int mResultCode = -2;
  50. private int WAIT_LOGIN = 5000;
  51. private static String errorMessage = "Activity not finished";
  52. @Rule
  53. public ActivityTestRule<AuthenticatorActivity> mActivityRule = new ActivityTestRule<AuthenticatorActivity>(
  54. AuthenticatorActivity.class){
  55. @Override
  56. protected Intent getActivityIntent() {
  57. Context targetContext = InstrumentationRegistry.getInstrumentation()
  58. .getTargetContext();
  59. Intent result = new Intent(targetContext, AuthenticatorActivity.class);
  60. result.putExtra(EXTRA_ACTION, "");
  61. result.putExtra(EXTRA_ACCOUNT, "");
  62. return result;
  63. }
  64. };
  65. @Test
  66. public void check_login() {
  67. // Check that login button is disabled
  68. onView(withId(R.id.buttonOK))
  69. .check(matches(not(isEnabled())));
  70. // Type server url
  71. onView(withId(R.id.hostUrlInput))
  72. .perform(typeText(BuildConfig.TEST_SERVER_URL), closeSoftKeyboard());
  73. onView(withId(R.id.account_username)).perform(click());
  74. // Type user
  75. onView(withId(R.id.account_username))
  76. .perform(typeText(BuildConfig.TEST_USER), closeSoftKeyboard());
  77. // Type user pass
  78. onView(withId(R.id.account_password))
  79. .perform(typeText(BuildConfig.TEST_PASSWORD), closeSoftKeyboard());
  80. onView(withId(R.id.buttonOK)).perform(click());
  81. // Check that the Activity ends after clicking
  82. try {
  83. Thread.sleep(WAIT_LOGIN);
  84. Field f = Activity.class.getDeclaredField("mResultCode");
  85. f.setAccessible(true);
  86. mResultCode = f.getInt(mActivityRule.getActivity());
  87. } catch (Exception e){
  88. e.printStackTrace();
  89. }
  90. assertTrue(errorMessage, mResultCode == Activity.RESULT_OK);
  91. }
  92. }