View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * Created by The eXo Platform SAS Author : Philippe Aristote
61   * paristote@exoplatform.com Sep 9, 2014
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   // Removed this test since we don't use the dialog theme anymore
126   // public void verifyDefaultLayout_Dialog() { }
127 
128   @Test
129   public void shouldDismissFragmentAndActivityWhenCurrentAccountIsSelected() {
130     Context ctx = RuntimeEnvironment.application.getApplicationContext();
131     create();
132     init();
133 
134     // Current account is at position 0
135     ExoAccount oldA = getCurrentAccount(ctx);
136     assertThat("1st account should be selected", oldA.accountName, equalTo(TEST_SERVER_NAME + " 1"));
137 
138     // Simulate a click on the item at position 0
139     shadowOf(accountListView).performItemClick(0);
140 
141     ShadowActivity shadowActivity = shadowOf(activity);
142     // Activity should be finishing
143     assertTrue("Account Switcher activity should be finishing", shadowActivity.isFinishing());
144 
145     // There should be no intent (the screen is simply closed)
146     Intent startedIntent = shadowActivity.getNextStartedActivity();
147     assertNull("No intent should have been fired", startedIntent);
148 
149     // Same account is still selected
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     // Current account is at position 0
164     ExoAccount oldA = getCurrentAccount(ctx);
165     assertThat("1st account should be selected", oldA.accountName, equalTo(TEST_SERVER_NAME + " 1"));
166 
167     // Simulate a click on the item at position 1
168     shadowOf(accountListView).performItemClick(1);
169 
170     ShadowActivity shadowActivity = shadowOf(activity);
171     // Activity should be finishing
172     assertTrue("Account Switcher activity should be finishing", shadowActivity.isFinishing());
173 
174     Intent startedIntent = shadowActivity.getNextStartedActivity();
175     ShadowIntent shadowIntent = shadowOf(startedIntent);
176     // App should be redirected to the Home screen
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     // Current account is at position 1
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     // Current account is at position 0
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     // Switch to account at position 1
207     shadowOf(accountListView).performItemClick(1);
208 
209     // Current account is now at position 1
210     ExoAccount newA = getCurrentAccount(ctx);
211     assertEquals("2nd account should be selected", getAccounts(ctx).indexOf(newA), 1);
212 
213     // Auto Login should be disabled on account 0
214     // acc = getAccounts(ctx).get(0);
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     // Current account is at position 0
228     // Simulate a click on the item at position 1
229     shadowOf(accountListView).performItemClick(1);
230 
231     // Test that the Warning dialog exists and is visible, and dismisses it
232     // to continue
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     // After a failed switch, the app should be redirected to the Login
243     // screen
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 }