AuthenticatorActivityTest.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.os.RemoteException;
  24. import android.support.test.InstrumentationRegistry;
  25. import android.support.test.rule.ActivityTestRule;
  26. import android.support.test.runner.AndroidJUnit4;
  27. import android.support.test.uiautomator.UiDevice;
  28. import android.test.suitebuilder.annotation.LargeTest;
  29. import static org.junit.Assert.assertTrue;
  30. import com.owncloud.android.R;
  31. import org.junit.Before;
  32. import org.junit.Rule;
  33. import org.junit.Test;
  34. import org.junit.runner.RunWith;
  35. import java.lang.reflect.Field;
  36. import android.app.Activity;
  37. import static android.support.test.espresso.Espresso.onView;
  38. import static android.support.test.espresso.action.ViewActions.click;
  39. import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
  40. import static android.support.test.espresso.action.ViewActions.typeText;
  41. import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
  42. import static android.support.test.espresso.matcher.ViewMatchers.withId;
  43. import static android.support.test.espresso.assertion.ViewAssertions.matches;
  44. import static org.hamcrest.Matchers.not;
  45. @RunWith(AndroidJUnit4.class)
  46. @LargeTest
  47. public class AuthenticatorActivityTest {
  48. public static final String EXTRA_ACTION = "ACTION";
  49. public static final String EXTRA_ACCOUNT = "ACCOUNT";
  50. private static final int WAIT_LOGIN = 5000;
  51. private static final String ERROR_MESSAGE = "Activity not finished";
  52. private static final String RESULT_CODE = "mResultCode";
  53. @Rule
  54. public ActivityTestRule<AuthenticatorActivity> mActivityRule = new ActivityTestRule<AuthenticatorActivity>(
  55. AuthenticatorActivity.class){
  56. @Override
  57. protected Intent getActivityIntent() {
  58. Context targetContext = InstrumentationRegistry.getInstrumentation()
  59. .getTargetContext();
  60. Intent result = new Intent(targetContext, AuthenticatorActivity.class);
  61. result.putExtra(EXTRA_ACTION, AuthenticatorActivity.ACTION_CREATE);
  62. result.putExtra(EXTRA_ACCOUNT, "");
  63. return result;
  64. }
  65. };
  66. @Before
  67. public void init(){
  68. UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
  69. /*Point[] coordinates = new Point[4];
  70. coordinates[0] = new Point(248, 1020);
  71. coordinates[1] = new Point(248, 429);
  72. coordinates[2] = new Point(796, 1020);
  73. coordinates[3] = new Point(796, 429);*/
  74. try {
  75. if (!uiDevice.isScreenOn()) {
  76. uiDevice.wakeUp();
  77. //uiDevice.swipe(coordinates, 10);
  78. }
  79. } catch (RemoteException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. @Test
  84. public void check_login()
  85. throws InterruptedException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
  86. Bundle arguments = InstrumentationRegistry.getArguments();
  87. // Get values passed
  88. String testUser = arguments.getString("TEST_USER");
  89. String testPassword = arguments.getString("TEST_PASSWORD");
  90. String testServerURL = arguments.getString("TEST_SERVER_URL");
  91. // Check that login button is disabled
  92. onView(withId(R.id.buttonOK))
  93. .check(matches(not(isEnabled())));
  94. // Type server url
  95. onView(withId(R.id.hostUrlInput))
  96. .perform(typeText(testServerURL), closeSoftKeyboard());
  97. onView(withId(R.id.account_username)).perform(click());
  98. // Type user
  99. onView(withId(R.id.account_username))
  100. .perform(typeText(testUser), closeSoftKeyboard());
  101. // Type user pass
  102. onView(withId(R.id.account_password))
  103. .perform(typeText(testPassword), closeSoftKeyboard());
  104. onView(withId(R.id.buttonOK)).perform(click());
  105. // Check that the Activity ends after clicking
  106. Thread.sleep(WAIT_LOGIN);
  107. Field f = Activity.class.getDeclaredField(RESULT_CODE);
  108. f.setAccessible(true);
  109. int mResultCode = f.getInt(mActivityRule.getActivity());
  110. assertTrue(ERROR_MESSAGE, mResultCode == Activity.RESULT_OK);
  111. }
  112. }