AuthenticatorActivityTest.java 4.9 KB

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