DrawerActivityIT.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. *
  3. * Nextcloud Android client application
  4. *
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2020 Tobias Kaminsky
  7. * Copyright (C) 2020 Nextcloud GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.ui.activity;
  23. import android.Manifest;
  24. import android.accounts.Account;
  25. import android.accounts.AccountManager;
  26. import android.net.Uri;
  27. import android.os.Bundle;
  28. import com.nextcloud.client.account.UserAccountManager;
  29. import com.nextcloud.client.account.UserAccountManagerImpl;
  30. import com.owncloud.android.AbstractIT;
  31. import com.owncloud.android.MainApp;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.lib.common.accounts.AccountUtils;
  34. import org.junit.BeforeClass;
  35. import org.junit.Rule;
  36. import org.junit.Test;
  37. import androidx.test.espresso.contrib.DrawerActions;
  38. import androidx.test.espresso.intent.rule.IntentsTestRule;
  39. import androidx.test.rule.GrantPermissionRule;
  40. import static androidx.test.espresso.Espresso.onView;
  41. import static androidx.test.espresso.action.ViewActions.click;
  42. import static androidx.test.espresso.matcher.ViewMatchers.withId;
  43. import static androidx.test.espresso.matcher.ViewMatchers.withText;
  44. import static org.hamcrest.Matchers.anyOf;
  45. import static org.junit.Assert.assertEquals;
  46. public class DrawerActivityIT extends AbstractIT {
  47. @Rule public IntentsTestRule<FileDisplayActivity> activityRule = new IntentsTestRule<>(FileDisplayActivity.class,
  48. true,
  49. false);
  50. @Rule
  51. public final GrantPermissionRule permissionRule = GrantPermissionRule.grant(
  52. Manifest.permission.WRITE_EXTERNAL_STORAGE);
  53. private static Account account2;
  54. private static String account2Name;
  55. private static String account2DisplayName;
  56. @BeforeClass
  57. public static void beforeClass() {
  58. Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments();
  59. Uri baseUrl = Uri.parse(arguments.getString("TEST_SERVER_URL"));
  60. String loginName = "user1";
  61. String password = "user1";
  62. Account temp = new Account(loginName + "@" + baseUrl, MainApp.getAccountType(targetContext));
  63. UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
  64. if (!accountManager.exists(temp)) {
  65. AccountManager platformAccountManager = AccountManager.get(targetContext);
  66. platformAccountManager.addAccountExplicitly(temp, password, null);
  67. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION,
  68. Integer.toString(UserAccountManager.ACCOUNT_VERSION));
  69. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_VERSION, "14.0.0.0");
  70. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, baseUrl.toString());
  71. platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName); // same as userId
  72. }
  73. final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
  74. account2 = userAccountManager.getAccountByName(loginName + "@" + baseUrl);
  75. account2Name = loginName + baseUrl;
  76. account2DisplayName = "User One@" + baseUrl;
  77. }
  78. @Test
  79. public void switchAccountViaAccountList() {
  80. FileDisplayActivity sut = activityRule.launchActivity(null);
  81. assertEquals(account, sut.getUser().get().toPlatformAccount());
  82. onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
  83. onView(withId(R.id.drawer_active_user)).perform(click());
  84. onView(anyOf(withText(account2Name), withText(account2DisplayName))).perform(click());
  85. waitForIdleSync();
  86. assertEquals(account2, sut.getUser().get().toPlatformAccount());
  87. onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
  88. onView(withId(R.id.drawer_active_user)).perform(click());
  89. onView(withText(account.name)).perform(click());
  90. }
  91. @Test
  92. public void switchAccountViaAvatar() {
  93. FileDisplayActivity sut = activityRule.launchActivity(null);
  94. assertEquals(account, sut.getUser().get().toPlatformAccount());
  95. onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
  96. onView(withId(R.id.drawer_account_end)).perform(click());
  97. waitForIdleSync();
  98. assertEquals(account2, sut.getUser().get().toPlatformAccount());
  99. onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
  100. onView(withId(R.id.drawer_account_end)).perform(click());
  101. }
  102. }