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.assertNotNull;
22  import static org.junit.Assert.assertThat;
23  
24  import java.util.ArrayList;
25  
26  import org.exoplatform.R;
27  import org.exoplatform.model.ExoAccount;
28  import org.exoplatform.singleton.AccountSetting;
29  import org.exoplatform.singleton.ServerSettingHelper;
30  import org.exoplatform.ui.WelcomeActivity;
31  import org.exoplatform.ui.setting.CheckBox;
32  import org.exoplatform.ui.setting.CheckBoxWithImage;
33  import org.exoplatform.ui.setting.ServerEditionActivity;
34  import org.exoplatform.ui.setting.ServerList;
35  import org.exoplatform.ui.setting.SettingActivity;
36  import org.exoplatform.utils.ExoConstants;
37  import org.exoplatform.utils.SettingUtils;
38  import org.exoplatform.widget.ServerItemLayout;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.junit.runner.RunWith;
42  import org.robolectric.Robolectric;
43  import org.robolectric.Shadows;
44  import org.robolectric.shadows.ShadowActivity;
45  import org.robolectric.shadows.ShadowApplication;
46  import org.robolectric.shadows.ShadowIntent;
47  import org.robolectric.shadows.ShadowView;
48  
49  import android.content.Intent;
50  import android.content.SharedPreferences;
51  import android.widget.Button;
52  import android.widget.TextView;
53  
54  /**
55   * Created by The eXo Platform SAS Author : Philippe Aristote
56   * paristote@exoplatform.com Apr 15, 2014
57   */
58  @RunWith(ExoRobolectricTestRunner.class)
59  public class SettingsActivityTest extends ExoActivityTestUtils<SettingActivity> {
60  
61    final String        SRV_VERSION = "4.0.0";
62  
63    final String        SRV_EDITION = "Enterprise";
64  
65    final String        APP_VERSION = "2.5.0";
66  
67    CheckBox            mRememberMeCbx;
68  
69    CheckBox            mAutoLoginCbx;
70  
71    CheckBoxWithImage   mEnCbx, mFrCbx, mDeCbx, mEsCbx, mPtBrCbx, mElCbx;
72  
73    CheckBox            mRememberFilterCbx;
74  
75    ServerList          serverList;
76  
77    // now it's the New Accounts button, under the accounts list
78    Button              mStartCloudSignUpBtn;
79  
80    ServerSettingHelper srvSettings;
81  
82    /**
83     * Create the activity with default parameters.
84     * 
85     * @param online whether we start the activity as an online user or not
86     */
87    public void createWithDefaultIntent(boolean online) {
88      initSettings();
89      Intent i = new Intent(ShadowApplication.getInstance().getApplicationContext(), SettingActivity.class);
90      int settingType = online ? SettingActivity.PERSONAL_TYPE : SettingActivity.GLOBAL_TYPE;
91      i.putExtra(ExoConstants.SETTING_TYPE, settingType);
92  
93      createWithIntent(i);
94      init();
95    }
96  
97    private void initSettings() {
98      // Setup and save a server
99      srvSettings = ServerSettingHelper.getInstance();
100     ExoAccount srv = getServerWithDefaultValues();
101     ArrayList<ExoAccount> list = new ArrayList<ExoAccount>();
102     list.add(srv);
103     srvSettings.setServerInfoList(list);
104     SettingUtils.persistServerSetting(ShadowApplication.getInstance().getApplicationContext());
105     AccountSetting.getInstance().setCurrentAccount(srv);
106     AccountSetting.getInstance().setDomainIndex("0");
107 
108     // Set server version, edition and app version
109     srvSettings.setServerVersion(SRV_VERSION);
110     srvSettings.setServerEdition(SRV_EDITION);
111     srvSettings.setApplicationVersion(APP_VERSION);
112   }
113 
114   private void init() {
115     // Login
116     mRememberMeCbx = (CheckBox) activity.findViewById(R.id.setting_remember_me_ckb);
117     mAutoLoginCbx = (CheckBox) activity.findViewById(R.id.setting_autologin_ckb);
118 
119     // Languages
120     mEnCbx = (CheckBoxWithImage) activity.findViewById(R.id.setting_en_ckb);
121     mFrCbx = (CheckBoxWithImage) activity.findViewById(R.id.setting_fr_ckb);
122     mDeCbx = (CheckBoxWithImage) activity.findViewById(R.id.setting_de_ckb);
123     mEsCbx = (CheckBoxWithImage) activity.findViewById(R.id.setting_es_ckb);
124     mPtBrCbx = (CheckBoxWithImage) activity.findViewById(R.id.setting_pt_br_ckb);
125     mElCbx = (CheckBoxWithImage) activity.findViewById(R.id.setting_el_ckb);
126 
127     // Social
128     mRememberFilterCbx = (CheckBox) activity.findViewById(R.id.setting_remember_filter_ckb);
129 
130     // Assistant
131     mStartCloudSignUpBtn = (Button) activity.findViewById(R.id.setting_new_account_btn);
132 
133     // Server List
134     serverList = (ServerList) activity.findViewById(R.id.setting_list_accounts);
135   }
136 
137   @Override
138   @Before
139   public void setup() {
140     controller = Robolectric.buildActivity(SettingActivity.class);
141   }
142 
143   @Test
144   public void verifyDefaultLayout_Online() {
145     createWithDefaultIntent(true); // online
146 
147     // Login
148     assertNotNull(mRememberMeCbx);
149     assertNotNull(mAutoLoginCbx);
150     assertThat("Remember Me should be disabled", mRememberMeCbx.isChecked(), equalTo(false));
151     assertThat("Auto Login should be disabled", mAutoLoginCbx.isEnabled(), equalTo(false));
152     // assertThat("Auto Login should be disabled",
153     // mAutoLoginCbx.isClickable(), equalTo(false));
154     assertThat("Auto Login should be disabled", mAutoLoginCbx.isChecked(), equalTo(false));
155 
156     // Languages
157     assertNotNull(mEnCbx); // checkboxes for EN, FR, DE, ES, PT should exist
158     assertThat(mEnCbx.isChecked(), equalTo(true)); // EN is selected by
159                                                    // default
160     assertNotNull(mFrCbx);
161     assertThat(mFrCbx.isChecked(), equalTo(false));
162     assertNotNull(mDeCbx);
163     assertThat(mDeCbx.isChecked(), equalTo(false));
164     assertNotNull(mEsCbx);
165     assertThat(mEsCbx.isChecked(), equalTo(false));
166     assertNotNull(mPtBrCbx);
167     assertThat(mPtBrCbx.isChecked(), equalTo(false));
168     assertNotNull(mElCbx);
169     assertThat(mElCbx.isChecked(), equalTo(false));
170 
171     // Social
172     assertNotNull(mRememberFilterCbx);
173 
174     // Server List : should have 1 server
175     assertThat(srvSettings.getServerInfoList(activity).size(), equalTo(1));
176     assertThat(serverList).hasChildCount(1);
177     ServerItemLayout serverItem = (ServerItemLayout) serverList.getChildAt(0);
178     assertThat(serverItem.serverName).containsText(TEST_SERVER_NAME);
179     assertThat(serverItem.serverUrl).containsText(TEST_SERVER_URL);
180 
181     // Assistant
182     assertNotNull(mStartCloudSignUpBtn);
183 
184     // App Info ** 3 cells should exist and contain default values set in
185     // initSettings()
186     TextView srvVersion = (TextView) activity.findViewById(R.id.setting_server_version_value_txt);
187     TextView srvEdition = (TextView) activity.findViewById(R.id.setting_server_edition_value_txt);
188     TextView appVersion = (TextView) activity.findViewById(R.id.setting_app_version_value_txt);
189     assertThat(appVersion).containsText(APP_VERSION);
190     assertThat(srvVersion).containsText(SRV_VERSION);
191     assertThat(srvEdition).containsText(SRV_EDITION);
192 
193   }
194 
195   @Test
196   public void verifyDefaultLayout_Offline() {
197     createWithDefaultIntent(false); // offline
198 
199     // Login section is disabled when user is offline
200     assertThat(mRememberMeCbx).isGone();
201     assertThat(mAutoLoginCbx).isGone();
202 
203     // Languages
204     assertNotNull(mEnCbx); // checkboxes for EN, FR, DE, ES should exist
205     assertThat(mEnCbx.isChecked(), equalTo(true)); // EN is selected by
206                                                    // default
207     assertNotNull(mFrCbx);
208     assertThat(mFrCbx.isChecked(), equalTo(false));
209     assertNotNull(mDeCbx);
210     assertThat(mDeCbx.isChecked(), equalTo(false));
211     assertNotNull(mEsCbx);
212     assertThat(mEsCbx.isChecked(), equalTo(false));
213     assertNotNull(mPtBrCbx);
214     assertThat(mPtBrCbx.isChecked(), equalTo(false));
215     assertNotNull(mElCbx);
216     assertThat(mElCbx.isChecked(), equalTo(false));
217 
218     // Social section is disabled when user is offline
219     assertThat(mRememberFilterCbx).isGone();
220 
221     // Server List : should have 1 server
222     assertThat(srvSettings.getServerInfoList(activity).size(), equalTo(1));
223     assertThat(serverList).hasChildCount(1);
224     ServerItemLayout serverItem = (ServerItemLayout) serverList.getChildAt(0);
225     assertThat(serverItem.serverName).containsText(TEST_SERVER_NAME);
226     assertThat(serverItem.serverUrl).containsText(TEST_SERVER_URL);
227 
228     // Assistant
229     assertNotNull(mStartCloudSignUpBtn);
230 
231     // App Info ** 3 cells should exist and contain default values set in
232     // initSettings()
233     TextView srvVersion = (TextView) activity.findViewById(R.id.setting_server_version_value_txt);
234     TextView srvEdition = (TextView) activity.findViewById(R.id.setting_server_edition_value_txt);
235     TextView appVersion = (TextView) activity.findViewById(R.id.setting_app_version_value_txt);
236     assertThat(appVersion).containsText(APP_VERSION);
237     assertThat(srvVersion).containsText(SRV_VERSION);
238     assertThat(srvEdition).containsText(SRV_EDITION);
239 
240   }
241 
242   @Test
243   public void shouldTurnOnRememberMe() {
244 
245     createWithDefaultIntent(true);
246 
247     ShadowView.clickOn(mRememberMeCbx); // turning on remember me
248 
249     assertThat("RM should be checked", mRememberMeCbx.isChecked(), equalTo(true));
250   }
251 
252   @Test
253   public void shouldEnableAutoLoginWhenTurningOnRememberMe() {
254 
255     createWithDefaultIntent(true);
256 
257     ShadowView.clickOn(mRememberMeCbx); // turning on remember me
258 
259     assertThat("AL checkbox should be enabled", mAutoLoginCbx.isEnabled(), equalTo(true));
260     assertThat("AL should be unchecked", mAutoLoginCbx.isChecked(), equalTo(false));
261 
262   }
263 
264   @Test
265   public void shouldTurnOnAutoLogin() {
266 
267     createWithDefaultIntent(true);
268 
269     ShadowView.clickOn(mRememberMeCbx); // turning on remember me
270     ShadowView.clickOn(mAutoLoginCbx); // turning on auto login
271 
272     assertThat("AL should be checked", mAutoLoginCbx.isChecked(), equalTo(true));
273   }
274 
275   @Test
276   public void shouldTurnOffAndDisableAutoLoginWhenTurningOffRememberMe() {
277 
278     createWithDefaultIntent(true);
279 
280     ShadowView.clickOn(mRememberMeCbx); // turning on remember me
281     ShadowView.clickOn(mAutoLoginCbx); // turning on auto login
282 
283     assertThat(mAutoLoginCbx.isEnabled(), equalTo(true));
284     assertThat(mAutoLoginCbx.isChecked(), equalTo(true));
285 
286     ShadowView.clickOn(mRememberMeCbx); // turning off remember me
287 
288     assertThat("RM should be unchecked", mRememberMeCbx.isChecked(), equalTo(false));
289     assertThat("AL should be disabled", mAutoLoginCbx.isEnabled(), equalTo(false));
290     assertThat("AL should be unchecked", mAutoLoginCbx.isChecked(), equalTo(false));
291 
292   }
293 
294   @Test
295   public void shouldStartExoCloudSignupAssistant() {
296 
297     createWithDefaultIntent(true);
298 
299     ShadowView.clickOn(mStartCloudSignUpBtn);
300 
301     ShadowActivity sActivity = Shadows.shadowOf(activity);
302     Intent welcomeIntent = sActivity.getNextStartedActivity();
303     ShadowIntent sIntent = Shadows.shadowOf(welcomeIntent);
304 
305     assertThat(sIntent.getComponent().getClassName(), equalTo(WelcomeActivity.class.getName()));
306 
307   }
308 
309   @Test
310   public void shouldChangeLanguage() {
311 
312     createWithDefaultIntent(true);
313 
314     SharedPreferences prefs = activity.getSharedPreferences(ExoConstants.EXO_PREFERENCE, 0);
315 
316     ShadowView.clickOn(mDeCbx); // turn on German
317     // English is off
318     assertThat("English should be OFF", mEnCbx.isChecked(), equalTo(false));
319     assertThat("German should be ON", mDeCbx.isChecked(), equalTo(true));
320     assertThat(prefs.getString(ExoConstants.EXO_PRF_LOCALIZE, ""), equalTo(ExoConstants.GERMAN_LOCALIZATION));
321 
322     ShadowView.clickOn(mFrCbx); // turn on French
323     // German is off
324     assertThat("German should be OFF", mDeCbx.isChecked(), equalTo(false));
325     assertThat("French should be ON", mFrCbx.isChecked(), equalTo(true));
326     assertThat(prefs.getString(ExoConstants.EXO_PRF_LOCALIZE, ""), equalTo(ExoConstants.FRENCH_LOCALIZATION));
327 
328     ShadowView.clickOn(mEsCbx); // turn on Spanish
329     // French is off
330     assertThat("French should be OFF", mFrCbx.isChecked(), equalTo(false));
331     assertThat("Spanish should be ON", mEsCbx.isChecked(), equalTo(true));
332     assertThat(prefs.getString(ExoConstants.EXO_PRF_LOCALIZE, ""), equalTo(ExoConstants.SPANISH_LOCALIZATION));
333 
334     ShadowView.clickOn(mPtBrCbx); // turn on Portuguese
335     // Spanish is off
336     assertThat("Spanish should be OFF", mEsCbx.isChecked(), equalTo(false));
337     assertThat("Portuguese should be ON", mPtBrCbx.isChecked(), equalTo(true));
338     assertThat(prefs.getString(ExoConstants.EXO_PRF_LOCALIZE, ""), equalTo(ExoConstants.BRAZIL_LOCALIZATION));
339 
340     ShadowView.clickOn(mElCbx); // turn on Greek
341     // Portuguese is off
342     assertThat("Portuguese should be OFF", mPtBrCbx.isChecked(), equalTo(false));
343     assertThat("Greek should be ON", mElCbx.isChecked(), equalTo(true));
344     assertThat(prefs.getString(ExoConstants.EXO_PRF_LOCALIZE, ""), equalTo(ExoConstants.GREEK_LOCALIZATION));
345 
346     ShadowView.clickOn(mEnCbx); // turn on English
347     // Greek is off
348     assertThat("Greek should be OFF", mElCbx.isChecked(), equalTo(false));
349     assertThat("English should be ON", mEnCbx.isChecked(), equalTo(true));
350     assertThat(prefs.getString(ExoConstants.EXO_PRF_LOCALIZE, ""), equalTo(ExoConstants.ENGLISH_LOCALIZATION));
351   }
352 
353   @Test
354   public void shouldEnableAccountInServerList() {
355     createWithDefaultIntent(true);
356 
357     assertThat(serverList).hasChildCount(1);
358 
359     // Simulate tap on the account item
360     ServerItemLayout item = (ServerItemLayout) serverList.getChildAt(0);
361     // Click on item.layout because the OnClickListener is set on the layout
362     ShadowView.clickOn(item.layout);
363 
364     ShadowActivity sActivity = Shadows.shadowOf(activity);
365     Intent editAccount = sActivity.getNextStartedActivity();
366     ShadowIntent sIntent = Shadows.shadowOf(editAccount);
367 
368     // Should start the Server Edition activity
369     assertThat("Should start the server edition activity",
370                sIntent.getComponent().getClassName(),
371                equalTo(ServerEditionActivity.class.getName()));
372   }
373 
374 }