View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program 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
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.mobile.tests;
18  
19  import static org.fest.assertions.api.ANDROID.assertThat;
20  import static org.hamcrest.CoreMatchers.equalTo;
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertThat;
24  import static org.junit.Assert.assertTrue;
25  import static org.robolectric.Shadows.shadowOf;
26  
27  import java.util.ArrayList;
28  
29  import org.exoplatform.R;
30  import org.exoplatform.accountswitcher.AccountSwitcherActivity;
31  import org.exoplatform.model.ExoAccount;
32  import org.exoplatform.singleton.AccountSetting;
33  import org.exoplatform.singleton.ServerSettingHelper;
34  import org.exoplatform.singleton.SocialServiceHelper;
35  import org.exoplatform.ui.DashboardActivity;
36  import org.exoplatform.ui.DocumentActivity;
37  import org.exoplatform.ui.HomeActivity;
38  import org.exoplatform.ui.login.LoginActivity;
39  import org.exoplatform.ui.social.SocialTabsActivity;
40  import org.exoplatform.utils.ExoConnectionUtils;
41  import org.junit.After;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.junit.runner.RunWith;
45  import org.robolectric.Robolectric;
46  import org.robolectric.RuntimeEnvironment;
47  import org.robolectric.fakes.RoboMenu;
48  import org.robolectric.shadows.ShadowActivity;
49  import org.robolectric.shadows.ShadowIntent;
50  import org.robolectric.shadows.ShadowView;
51  import org.robolectric.shadows.httpclient.FakeHttp;
52  
53  import android.content.Context;
54  import android.content.Intent;
55  import android.view.Menu;
56  import android.view.MenuItem;
57  import android.widget.ImageView;
58  import android.widget.LinearLayout;
59  import android.widget.TextView;
60  import android.widget.ViewFlipper;
61  
62  /**
63   * Created by The eXo Platform SAS Author : Philippe Aristote
64   * paristote@exoplatform.com Apr 16, 2014
65   */
66  @RunWith(ExoRobolectricTestRunner.class)
67  public class HomeActivityTest extends ExoActivityTestUtils<HomeActivity> {
68  
69    ViewFlipper  flipper;
70  
71    TextView     activitiesTV, documentsTV, appsTV, userNameTV;
72  
73    LinearLayout activitiesBtn, documentsBtn, appsBtn;
74  
75    ImageView    userAvatar;
76  
77    Menu         actionBar;
78  
79    private void init() {
80  
81      flipper = (ViewFlipper) activity.findViewById(R.id.home_social_flipper);
82      activitiesTV = (TextView) activity.findViewById(R.id.home_btn_activity);
83      documentsTV = (TextView) activity.findViewById(R.id.home_btn_document);
84      appsTV = (TextView) activity.findViewById(R.id.home_btn_apps);
85      userNameTV = (TextView) activity.findViewById(R.id.home_textview_name);
86      userAvatar = (ImageView) activity.findViewById(R.id.home_user_avatar);
87  
88      activitiesBtn = (LinearLayout) activitiesTV.getParent();
89      documentsBtn = (LinearLayout) documentsTV.getParent();
90      appsBtn = (LinearLayout) appsTV.getParent();
91  
92      actionBar = new RoboMenu(activity);
93      activity.onCreateOptionsMenu(actionBar);
94      activity.onPrepareOptionsMenu(actionBar);
95    }
96  
97    @Override
98    @Before
99    public void setup() {
100     controller = Robolectric.buildActivity(HomeActivity.class);
101 
102     // mock response for HTTP GET /rest/api/social/version/latest.json
103     FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_VERSION_LATEST),
104                                  getResponseOKForRequest(REQ_SOCIAL_VERSION_LATEST));
105     // mock response for HTTP GET
106     // /rest/private/api/social/v1-alpha3/portal/identity/organization/{testuser}.json
107     FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_IDENTITY), getResponseOKForRequest(REQ_SOCIAL_IDENTITY));
108     // mock response for HTTP GET
109     // /rest/private/api/social/v1-alpha3/portal/identity/{testidentity}.json
110     FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_IDENTITY_2), getResponseOKForRequest(REQ_SOCIAL_IDENTITY_2));
111     // mock response for HTTP GET
112     // /rest/private/api/social/v1-alpha3/portal/activity_stream/feed.json
113     FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_NEWS), getResponseOKForRequest(REQ_SOCIAL_NEWS));
114   }
115 
116   @Override
117   @After
118   public void teardown() {
119     deleteAllAccounts(RuntimeEnvironment.application.getApplicationContext());
120     // do not call super.teardown() to avoid error
121     // HomeActivity has leaked IntentReceiver that was originally registered
122     // here. Are you missing a call to unregisterReceiver()?
123   }
124 
125   @Test
126   public void verifyDefaultLayout() {
127     Context ctx = RuntimeEnvironment.application.getApplicationContext();
128     setDefaultServerInPreferences(ctx, getServerWithDefaultValues());
129     create();
130     init();
131 
132     final String nameAndAccount = TEST_USER_NAME + " (" + TEST_SERVER_NAME + ")";
133 
134     // text field is filled by data returned in RESP_SOCIAL_IDENTITY
135     assertThat(userNameTV).containsText(nameAndAccount);
136 
137     // should have only 1 activity in the flipper since RESP_SOCIAL_NEWS
138     // contains just 1 activity
139     assertThat(flipper).hasChildCount(1);
140 
141     assertThat(activitiesTV).containsText(R.string.ActivityStream);
142     assertThat(documentsTV).containsText(R.string.Documents);
143     assertThat(appsTV).containsText(R.string.Dashboard);
144 
145     // Check that by default the acc switcher menu is not visible
146     MenuItem accSwitcherBtn = actionBar.findItem(R.id.menu_home_account_switcher);
147     assertFalse("Account Switcher menu should be invisible", accSwitcherBtn.isVisible());
148   }
149 
150   @Test
151   public void shouldOpenNewsActivity() {
152     create();
153     init();
154 
155     ShadowView.clickOn(activitiesBtn);
156 
157     ShadowActivity sActivity = shadowOf(activity);
158     Intent welcomeIntent = sActivity.getNextStartedActivity();
159     ShadowIntent sIntent = shadowOf(welcomeIntent);
160 
161     assertThat(sIntent.getComponent().getClassName(), equalTo(SocialTabsActivity.class.getName()));
162   }
163 
164   @Test
165   public void shouldOpenNewsActivityFromFlipper() {
166     create();
167     init();
168 
169     ShadowView.clickOn(flipper);
170 
171     ShadowActivity sActivity = shadowOf(activity);
172     Intent welcomeIntent = sActivity.getNextStartedActivity();
173     ShadowIntent sIntent = shadowOf(welcomeIntent);
174 
175     assertThat(sIntent.getComponent().getClassName(), equalTo(SocialTabsActivity.class.getName()));
176   }
177 
178   @Test
179   public void shouldOpenDocumentsActivity() {
180     create();
181     init();
182 
183     ShadowView.clickOn(documentsBtn);
184 
185     ShadowActivity sActivity = shadowOf(activity);
186     Intent welcomeIntent = sActivity.getNextStartedActivity();
187     ShadowIntent sIntent = shadowOf(welcomeIntent);
188 
189     assertThat(sIntent.getComponent().getClassName(), equalTo(DocumentActivity.class.getName()));
190 
191   }
192 
193   @Test
194   public void shouldOpenDashboardActivity() {
195     create();
196     init();
197 
198     ShadowView.clickOn(appsBtn);
199 
200     ShadowActivity sActivity = shadowOf(activity);
201     Intent welcomeIntent = sActivity.getNextStartedActivity();
202     ShadowIntent sIntent = shadowOf(welcomeIntent);
203 
204     assertThat(sIntent.getComponent().getClassName(), equalTo(DashboardActivity.class.getName()));
205   }
206 
207   @Test
208   public void shouldSignOutAndOpenLoginActivity() {
209     create();
210     init();
211 
212     ShadowActivity sActivity = shadowOf(activity);
213 
214     // simulate tap on the sign out button
215     sActivity.clickMenuItem(R.id.menu_home_logout);
216 
217     Intent welcomeIntent = sActivity.getNextStartedActivity();
218     ShadowIntent sIntent = shadowOf(welcomeIntent);
219 
220     assertNull(ExoConnectionUtils.httpClient);
221     assertNull(AccountSetting.getInstance().cookiesList);
222     assertNull(SocialServiceHelper.getInstance().userIdentity);
223     assertNull(SocialServiceHelper.getInstance().activityService);
224     assertNull(SocialServiceHelper.getInstance().identityService);
225     assertNull(SocialServiceHelper.getInstance().socialInfoList);
226     assertNull(SocialServiceHelper.getInstance().userProfile);
227     assertNull(SocialServiceHelper.getInstance().myConnectionsList);
228     assertNull(SocialServiceHelper.getInstance().mySpacesList);
229     assertNull(SocialServiceHelper.getInstance().myStatusList);
230 
231     assertThat(sIntent.getComponent().getClassName(), equalTo(LoginActivity.class.getName()));
232 
233   }
234 
235   @Test
236   public void shouldNotHaveAccountSwitcherButtonWithOneAccount() {
237     Context ctx = RuntimeEnvironment.application.getApplicationContext();
238     setDefaultServerInPreferences(ctx, getServerWithDefaultValues());
239     create();
240     init();
241 
242     // Check that the acc switcher menu is not visible
243     MenuItem accSwitcherBtn = actionBar.findItem(R.id.menu_home_account_switcher);
244     assertFalse("Account Switcher menu should be invisible", accSwitcherBtn.isVisible());
245   }
246 
247   @Test
248   public void shouldHaveAccountSwitcherButtonWithTwoAndMoreAccounts() {
249     Context ctx = RuntimeEnvironment.application.getApplicationContext();
250     ArrayList<ExoAccount> accounts = createXAccounts(2);
251     addServersInPreferences(ctx, accounts);
252     create();
253     init();
254 
255     // Check that the acc switcher menu is visible
256     MenuItem accSwitcherBtn = actionBar.findItem(R.id.menu_home_account_switcher);
257     assertTrue("Account Switcher menu should be visible", accSwitcherBtn.isVisible());
258 
259   }
260 
261   @Test
262   public void shouldOpenAccountSwitcherActivity() {
263     Context ctx = RuntimeEnvironment.application.getApplicationContext();
264     ArrayList<ExoAccount> accounts = createXAccounts(2);
265     addServersInPreferences(ctx, accounts);
266     create();
267     init();
268 
269     ShadowActivity sActivity = shadowOf(activity);
270 
271     // simulate a tap on the account switcher button
272     sActivity.clickMenuItem(R.id.menu_home_account_switcher);
273 
274     // On small/normal screens, the account switcher is opened as an
275     // activity (full screen)
276     Intent welcomeIntent = sActivity.getNextStartedActivity();
277     ShadowIntent sIntent = shadowOf(welcomeIntent);
278     assertThat(sIntent.getComponent().getClassName(), equalTo(AccountSwitcherActivity.class.getName()));
279   }
280 
281   @Test
282   public void shouldDisableAutoLoginOnAccountWhenSigningOut() {
283     create();
284     init();
285     ExoAccount acc = getServerWithDefaultValues();
286     acc.isRememberEnabled = true;
287     acc.isAutoLoginEnabled = true;
288     setDefaultServerInPreferences(activity, acc);
289     acc = null;
290     acc = getAccounts(activity).get(0);
291     assertTrue("AL should be enabled", acc.isAutoLoginEnabled);
292 
293     // simulate tap on the sign out button
294     shadowOf(activity).clickMenuItem(R.id.menu_home_logout);
295 
296     acc = null;
297     acc = getAccounts(activity).get(0);
298 
299     assertFalse("AL should be disabled", acc.isAutoLoginEnabled);
300   }
301 
302   @Test
303   public void shouldNotDisableAutoLoginWhenBackButtonPressed() {
304     create();
305     init();
306     ExoAccount acc = getServerWithDefaultValues();
307     acc.isRememberEnabled = true;
308     acc.isAutoLoginEnabled = true;
309     setDefaultServerInPreferences(activity, acc);
310 
311     acc = ServerSettingHelper.getInstance().getServerInfoList(activity).get(0);
312     assertTrue("AL should be enabled", acc.isAutoLoginEnabled);
313 
314     activity.onBackPressed();
315 
316     acc = ServerSettingHelper.getInstance().getServerInfoList(activity).get(0);
317     assertTrue("AL should still be enabled", acc.isAutoLoginEnabled);
318   }
319 
320 }