1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.exoplatform.mobile.tests;
20
21 import static org.fest.assertions.api.ANDROID.assertThat;
22 import static org.hamcrest.CoreMatchers.equalTo;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.assertTrue;
29 import static org.robolectric.Shadows.shadowOf;
30
31 import java.util.ArrayList;
32
33 import org.exoplatform.R;
34 import org.exoplatform.accountswitcher.AccountSwitcherActivity;
35 import org.exoplatform.accountswitcher.AccountSwitcherFragment;
36 import org.exoplatform.model.ExoAccount;
37 import org.exoplatform.singleton.ServerSettingHelper;
38 import org.exoplatform.ui.HomeActivity;
39 import org.exoplatform.ui.login.LoginActivity;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.robolectric.Robolectric;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.shadows.ShadowActivity;
47 import org.robolectric.shadows.ShadowDialog;
48 import org.robolectric.shadows.ShadowIntent;
49 import org.robolectric.shadows.httpclient.FakeHttp;
50
51 import android.app.Dialog;
52 import android.content.Context;
53 import android.content.Intent;
54 import android.support.v4.content.IntentCompat;
55 import android.view.View;
56 import android.widget.ListView;
57 import android.widget.TextView;
58
59
60
61
62
63 @RunWith(ExoRobolectricTestRunner.class)
64 public class AccountSwitcherTest extends ExoActivityTestUtils<AccountSwitcherActivity> {
65
66 final String FRAGMENT_TAG = "account_switcher_fragment_dialog";
67
68 AccountSwitcherFragment accountSwitcherFragment;
69
70 ListView accountListView;
71
72 ArrayList<ExoAccount> accounts;
73
74 @Override
75 @Before
76 public void setup() {
77 controller = Robolectric.buildActivity(AccountSwitcherActivity.class);
78 }
79
80 @Override
81 public void create() {
82 Context ctx = RuntimeEnvironment.application.getApplicationContext();
83 accounts = createXAccounts(2);
84 addServersInPreferences(ctx, accounts);
85 super.create();
86 }
87
88 @Override
89 @After
90 public void teardown() {
91 Context ctx = RuntimeEnvironment.application.getApplicationContext();
92 deleteAllAccounts(ctx);
93 super.teardown();
94 }
95
96 public void init() {
97 accountSwitcherFragment = (AccountSwitcherFragment) activity.getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
98 accountListView = (ListView) activity.findViewById(R.id.account_list_view);
99 }
100
101 @Test
102 public void verifyDefaultLayout() {
103 create();
104 init();
105
106 assertThat(accountSwitcherFragment).isAdded().isVisible().isNotDetached().hasTag(FRAGMENT_TAG);
107 String titleText = activity.getTitle().toString();
108 assertTrue("Title should be capitalized", Character.isUpperCase(titleText.charAt(0)));
109 assertTrue("Title should be 'Accounts'", titleText.equalsIgnoreCase(activity.getResources().getString(R.string.Server)));
110
111 final int numberOfAccounts = 2;
112 assertThat(accountListView).hasCount(numberOfAccounts);
113
114 for (int i = 0; i < numberOfAccounts; i++) {
115 View v = accountListView.getAdapter().getView(i, null, accountListView);
116 TextView name = (TextView) v.findViewById(R.id.account_name_textview);
117 assertThat(name).containsText(TEST_SERVER_NAME.toUpperCase() + " " + (i + 1));
118 TextView server = (TextView) v.findViewById(R.id.account_server_textview);
119 assertThat(server).containsText(TEST_SERVER_URL);
120 TextView username = (TextView) v.findViewById(R.id.account_username_textview);
121 assertThat(username).containsText(TEST_USER_NAME + "_" + (i + 1));
122 }
123 }
124
125
126
127
128 @Test
129 public void shouldDismissFragmentAndActivityWhenCurrentAccountIsSelected() {
130 Context ctx = RuntimeEnvironment.application.getApplicationContext();
131 create();
132 init();
133
134
135 ExoAccount oldA = getCurrentAccount(ctx);
136 assertThat("1st account should be selected", oldA.accountName, equalTo(TEST_SERVER_NAME + " 1"));
137
138
139 shadowOf(accountListView).performItemClick(0);
140
141 ShadowActivity shadowActivity = shadowOf(activity);
142
143 assertTrue("Account Switcher activity should be finishing", shadowActivity.isFinishing());
144
145
146 Intent startedIntent = shadowActivity.getNextStartedActivity();
147 assertNull("No intent should have been fired", startedIntent);
148
149
150 ExoAccount newA = getCurrentAccount(ctx);
151 assertThat("1st account should be selected", newA.accountName, equalTo(TEST_SERVER_NAME + " 1"));
152 }
153
154 @Test
155 public void shouldSignOutCurrentAccountAndSignInSelectedAccount() {
156 Context ctx = RuntimeEnvironment.application.getApplicationContext();
157 create();
158 FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_PLATFORM_INFO_USER_2),
159 getResponseOKForRequest(REQ_PLATFORM_INFO_USER_2));
160 FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_JCR_USER_2), getResponseOKForRequest(REQ_JCR_USER_2));
161 init();
162
163
164 ExoAccount oldA = getCurrentAccount(ctx);
165 assertThat("1st account should be selected", oldA.accountName, equalTo(TEST_SERVER_NAME + " 1"));
166
167
168 shadowOf(accountListView).performItemClick(1);
169
170 ShadowActivity shadowActivity = shadowOf(activity);
171
172 assertTrue("Account Switcher activity should be finishing", shadowActivity.isFinishing());
173
174 Intent startedIntent = shadowActivity.getNextStartedActivity();
175 ShadowIntent shadowIntent = shadowOf(startedIntent);
176
177 assertThat("Should be redirecting to Home screen after a successful login.",
178 shadowIntent.getComponent().getClassName(),
179 equalTo(HomeActivity.class.getName()));
180 int flags = startedIntent.getFlags();
181 final int expectedFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
182 | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK;
183 assertEquals("Intent should have flags FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK",
184 expectedFlags,
185 flags);
186
187
188 ExoAccount newA = getCurrentAccount(ctx);
189 assertThat("2nd account should be selected", newA.accountName, equalTo(TEST_SERVER_NAME + " 2"));
190 }
191
192 @Test
193 public void shouldDisableAutoLoginWhenLeavingAccount() {
194 Context ctx = RuntimeEnvironment.application.getApplicationContext();
195 create();
196 FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_PLATFORM_INFO_USER_2),
197 getResponseOKForRequest(REQ_PLATFORM_INFO_USER_2));
198 FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_JCR_USER_2), getResponseOKForRequest(REQ_JCR_USER_2));
199 init();
200
201
202 ExoAccount acc = getCurrentAccount(ctx);
203 assertTrue("AL should be enabled", acc.isAutoLoginEnabled);
204 assertEquals("1st account should be selected", getAccounts(ctx).indexOf(acc), 0);
205
206
207 shadowOf(accountListView).performItemClick(1);
208
209
210 ExoAccount newA = getCurrentAccount(ctx);
211 assertEquals("2nd account should be selected", getAccounts(ctx).indexOf(newA), 1);
212
213
214
215 acc = null;
216 acc = ServerSettingHelper.getInstance().getServerInfoList(ctx).get(0);
217 assertFalse("AL should be disabled", acc.isAutoLoginEnabled);
218 }
219
220 @Test
221 public void shouldSignOutAndRedirectToLoginScreenWhenSwitchingFails() {
222 create();
223 FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_PLATFORM_INFO), getResponseFailedWithStatus(404));
224 FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_JCR_USER_2), getResponseFailedWithStatus(404));
225 init();
226
227
228
229 shadowOf(accountListView).performItemClick(1);
230
231
232
233 Dialog warningDialog = ShadowDialog.getLatestDialog();
234
235 assertNotNull("There should be a warning Dialog after a failed account switch", warningDialog);
236 assertTrue("The warning Dialog should be visible", warningDialog.isShowing());
237 shadowOf(warningDialog).clickOn(R.id.warning_dialog_btn);
238
239 ShadowActivity shadowActivity = shadowOf(activity);
240 Intent startedIntent = shadowActivity.getNextStartedActivity();
241 ShadowIntent shadowIntent = shadowOf(startedIntent);
242
243
244 assertThat("Should be redirecting to Login screen after a successful login.",
245 shadowIntent.getComponent().getClassName(),
246 equalTo(LoginActivity.class.getName()));
247 int flags = startedIntent.getFlags();
248 final int expectedFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
249 | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK;
250 assertEquals("Intent should have flags FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK",
251 expectedFlags,
252 flags);
253 }
254
255 }