Browse Source

Merge pull request #5608 from nextcloud/accountSwitchingTest

Fix account switching
Andy Scherzinger 5 years ago
parent
commit
f7ffbe689f

+ 126 - 0
src/androidTest/java/com/owncloud/android/ui/activity/DrawerActivityIT.java

@@ -0,0 +1,126 @@
+/*
+ *
+ * Nextcloud Android client application
+ *
+ * @author Tobias Kaminsky
+ * Copyright (C) 2020 Tobias Kaminsky
+ * Copyright (C) 2020 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.owncloud.android.ui.activity;
+
+import android.Manifest;
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.net.Uri;
+import android.os.Bundle;
+
+import com.nextcloud.client.account.UserAccountManager;
+import com.nextcloud.client.account.UserAccountManagerImpl;
+import com.owncloud.android.AbstractIT;
+import com.owncloud.android.MainApp;
+import com.owncloud.android.R;
+import com.owncloud.android.lib.common.accounts.AccountUtils;
+
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+import androidx.test.espresso.contrib.DrawerActions;
+import androidx.test.espresso.intent.rule.IntentsTestRule;
+import androidx.test.rule.GrantPermissionRule;
+
+import static androidx.test.espresso.Espresso.onView;
+import static androidx.test.espresso.action.ViewActions.click;
+import static androidx.test.espresso.matcher.ViewMatchers.withId;
+import static androidx.test.espresso.matcher.ViewMatchers.withText;
+import static org.hamcrest.Matchers.anyOf;
+import static org.junit.Assert.assertEquals;
+
+public class DrawerActivityIT extends AbstractIT {
+    @Rule public IntentsTestRule<FileDisplayActivity> activityRule = new IntentsTestRule<>(FileDisplayActivity.class,
+                                                                                           true,
+                                                                                           false);
+
+    @Rule
+    public final GrantPermissionRule permissionRule = GrantPermissionRule.grant(
+        Manifest.permission.WRITE_EXTERNAL_STORAGE);
+    private static Account account2;
+    private static String account2Name;
+    private static String account2DisplayName;
+
+    @BeforeClass
+    public static void beforeClass() {
+        Bundle arguments = androidx.test.platform.app.InstrumentationRegistry.getArguments();
+        Uri baseUrl = Uri.parse(arguments.getString("TEST_SERVER_URL"));
+        String loginName = "user1";
+        String password = "user1";
+
+        Account temp = new Account(loginName + "@" + baseUrl, MainApp.getAccountType(targetContext));
+        UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
+        if (!accountManager.exists(temp)) {
+            AccountManager platformAccountManager = AccountManager.get(targetContext);
+            platformAccountManager.addAccountExplicitly(temp, password, null);
+            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_ACCOUNT_VERSION,
+                                               Integer.toString(UserAccountManager.ACCOUNT_VERSION));
+            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_VERSION, "14.0.0.0");
+            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_OC_BASE_URL, baseUrl.toString());
+            platformAccountManager.setUserData(temp, AccountUtils.Constants.KEY_USER_ID, loginName); // same as userId
+        }
+
+        final UserAccountManager userAccountManager = UserAccountManagerImpl.fromContext(targetContext);
+        account2 = userAccountManager.getAccountByName(loginName + "@" + baseUrl);
+        account2Name = loginName + "@" + baseUrl;
+        account2DisplayName = "User One@" + baseUrl;
+    }
+
+    @Test
+    public void switchAccountViaAccountList() {
+        FileDisplayActivity sut = activityRule.launchActivity(null);
+
+        assertEquals(account, sut.getUser().get().toPlatformAccount());
+
+        onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
+        onView(withId(R.id.drawer_active_user)).perform(click());
+
+        onView(anyOf(withText(account2Name), withText(account2DisplayName))).perform(click());
+
+        waitForIdleSync();
+
+        assertEquals(account2, sut.getUser().get().toPlatformAccount());
+
+        onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
+        onView(withId(R.id.drawer_active_user)).perform(click());
+        onView(withText(account.name)).perform(click());
+    }
+
+    @Test
+    public void switchAccountViaAvatar() {
+        FileDisplayActivity sut = activityRule.launchActivity(null);
+
+        assertEquals(account, sut.getUser().get().toPlatformAccount());
+
+        onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
+        onView(withId(R.id.drawer_account_end)).perform(click());
+
+        waitForIdleSync();
+
+        assertEquals(account2, sut.getUser().get().toPlatformAccount());
+
+        onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
+        onView(withId(R.id.drawer_account_end)).perform(click());
+    }
+}

+ 3 - 3
src/main/java/com/nextcloud/client/account/UserAccountManagerImpl.java

@@ -292,10 +292,10 @@ public class UserAccountManagerImpl implements UserAccountManager {
     public boolean setCurrentOwnCloudAccount(int hashCode) {
         boolean result = false;
         if (hashCode != 0) {
-            for (final Account account : getAccounts()) {
-                if (hashCode == account.hashCode()) {
+            for (final User user : getAllUsers()) {
+                if (hashCode == user.hashCode()) {
                     SharedPreferences.Editor appPrefs = PreferenceManager.getDefaultSharedPreferences(context).edit();
-                    appPrefs.putString(PREF_SELECT_OC_ACCOUNT, account.name);
+                    appPrefs.putString(PREF_SELECT_OC_ACCOUNT, user.getAccountName());
                     appPrefs.apply();
                     result = true;
                     break;

+ 5 - 7
src/main/java/com/owncloud/android/ui/activity/DrawerActivity.java

@@ -26,7 +26,6 @@
 
 package com.owncloud.android.ui.activity;
 
-import android.accounts.Account;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
@@ -574,9 +573,8 @@ public abstract class DrawerActivity extends ToolbarActivity
      * @param hashCode HashCode of account to be set
      */
     private void accountClicked(int hashCode) {
-        final Account currentAccount = accountManager.getCurrentAccount();
-        if (currentAccount != null && currentAccount.hashCode() != hashCode &&
-            accountManager.setCurrentOwnCloudAccount(hashCode)) {
+        final User currentUser = accountManager.getUser();
+        if (currentUser.hashCode() != hashCode && accountManager.setCurrentOwnCloudAccount(hashCode)) {
             fetchExternalLinks(true);
             restart();
         }
@@ -606,7 +604,7 @@ public abstract class DrawerActivity extends ToolbarActivity
      * @param view the clicked ImageView
      */
     public void onAccountDrawerClick(View view) {
-        accountClicked(Integer.parseInt(view.getContentDescription().toString()));
+        accountClicked((int) view.getTag());
     }
 
     /**
@@ -687,7 +685,7 @@ public abstract class DrawerActivity extends ToolbarActivity
                 // activate second/end account avatar
                 final User secondUser = mAvatars.size() > 1 ? mAvatars.get(1) : null;
                 if (secondUser != null) {
-                    mAccountEndAccountAvatar.setTag(secondUser.getAccountName());
+                    mAccountEndAccountAvatar.setTag(secondUser.hashCode());
                     DisplayUtils.setAvatar(secondUser,
                                            this,
                                            mOtherAccountAvatarRadiusDimension,
@@ -702,7 +700,7 @@ public abstract class DrawerActivity extends ToolbarActivity
                 // activate third/middle account avatar
                 final User thirdUser = mAvatars.size() > 2 ? mAvatars.get(2) : null;
                 if (thirdUser != null) {
-                    mAccountMiddleAccountAvatar.setTag(thirdUser.getAccountName());
+                    mAccountMiddleAccountAvatar.setTag(thirdUser.hashCode());
                     DisplayUtils.setAvatar(thirdUser,
                                            this,
                                            mOtherAccountAvatarRadiusDimension,