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.hamcrest.CoreMatchers.equalTo;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertThat;
22  import static org.junit.Assert.assertTrue;
23  import static org.robolectric.Shadows.shadowOf;
24  
25  import java.util.Locale;
26  
27  import org.exoplatform.model.ExoAccount;
28  import org.exoplatform.ui.LaunchActivity;
29  import org.exoplatform.ui.WelcomeActivity;
30  import org.exoplatform.ui.login.LoginActivity;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  import org.robolectric.Robolectric;
35  import org.robolectric.RuntimeEnvironment;
36  import org.robolectric.shadows.ShadowActivity;
37  import org.robolectric.shadows.ShadowIntent;
38  
39  import android.content.Context;
40  import android.content.Intent;
41  
42  /**
43   * Created by The eXo Platform SAS Author : Philippe Aristote
44   * paristote@exoplatform.com Apr 8, 2014
45   */
46  @RunWith(ExoRobolectricTestRunner.class)
47  public class LaunchActivityTest extends ExoActivityTestUtils<LaunchActivity> {
48  
49    Context ctx;
50  
51    @Override
52    @Before
53    public void setup() {
54      controller = Robolectric.buildActivity(LaunchActivity.class);
55      ctx = RuntimeEnvironment.application.getApplicationContext();
56    }
57  
58    @Test
59    /**
60     * Tests that on a clean launch, the app is redirected to the Welcome Activity
61     * (sign-up assistant)
62     * 
63     * @throws Exception
64     */
65    public void shouldRedirectToWelcomeScreen() throws Exception {
66      create();
67  
68      ShadowActivity sActivity = shadowOf(activity);
69      Intent welcomeIntent = sActivity.getNextStartedActivity();
70      ShadowIntent sIntent = shadowOf(welcomeIntent);
71  
72      assertThat(sIntent.getComponent().getClassName(), equalTo(WelcomeActivity.class.getName()));
73  
74    }
75  
76    @Test
77    /*
78     * Test rule AUTO_DETECT_LANGUAGE_01 with French
79     */
80    public void shouldDetectAndConfigureAppInFrench() {
81  
82      final String expectedLang = "fr";
83      Locale.setDefault(new Locale(expectedLang)); // set device language to
84                                                   // French
85      setLanguageInPreferences(ctx, ""); // empty the language preference in
86                                         // the app
87  
88      create();
89  
90      String lang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
91      // French should be saved
92      assertTrue("Saved language should be French", lang.equals(expectedLang));
93    }
94  
95    @Test
96    /*
97     * Test rule AUTO_DETECT_LANGUAGE_01 with Spanish
98     */
99    public void shouldDetectAndConfigureAppInSpanish() {
100 
101     final String expectedLang = "es";
102     Locale.setDefault(new Locale(expectedLang)); // set device language to
103                                                  // Spanish
104     setLanguageInPreferences(ctx, ""); // empty the language preference in
105                                        // the app
106 
107     create();
108 
109     String lang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
110     // Spanish should be saved
111     assertTrue("Saved language should be Spanish", lang.equals(expectedLang));
112   }
113 
114   @Test
115   /*
116    * Test rule AUTO_DETECT_LANGUAGE_01 with German
117    */
118   public void shouldDetectAndConfigureAppInGerman() {
119 
120     final String expectedLang = "de";
121     Locale.setDefault(new Locale(expectedLang)); // set device language to
122                                                  // German
123     setLanguageInPreferences(ctx, ""); // empty the language preference in
124                                        // the app
125 
126     create();
127 
128     String lang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
129     // German should be saved
130     assertTrue("Saved language should be German", lang.equals(expectedLang));
131   }
132 
133   @Test
134   /*
135    * Test rule AUTO_DETECT_LANGUAGE_01 with English
136    */
137   public void shouldDetectAndConfigureAppInEnglish() {
138 
139     final String expectedLang = "en";
140     Locale.setDefault(new Locale(expectedLang)); // set device language to
141                                                  // English
142     setLanguageInPreferences(ctx, ""); // empty the language preference in
143                                        // the app
144 
145     create();
146 
147     String lang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
148     // English should be saved
149     assertTrue("Saved language should be English", lang.equals(expectedLang));
150   }
151 
152   @Test
153   /*
154    * Test rule AUTO_DETECT_LANGUAGE_03
155    */
156   public void shouldDetectNotSupportedLanguageAndConfigureAppInEnglish() {
157 
158     final String deviceLang = "ja";
159     Locale.setDefault(new Locale(deviceLang)); // set device language to
160                                                // Japanese
161     setLanguageInPreferences(ctx, ""); // empty the language preference in
162                                        // the app
163 
164     create();
165 
166     String lang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
167     // English should be configured since Japanese is not supported
168     assertTrue("Saved language should be English", lang.equals("en"));
169   }
170 
171   @Test
172   /*
173    * Test rule AUTO_DETECT_LANGUAGE_02
174    */
175   public void shouldKeepSavedLanguage() {
176 
177     final String expectedLang = "en";
178     Locale.setDefault(new Locale("fr")); // set device language to French
179     setLanguageInPreferences(ctx, expectedLang); // set language preference
180                                                  // to English
181 
182     create();
183 
184     String lang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
185     // English should still be configured and not replaced by French
186     assertTrue("Saved language should be English", lang.equals(expectedLang));
187   }
188 
189   /*
190    * Tests rule AUTO_DETECT_LANGUAGE_04
191    */
192 
193   @Test
194   public void shouldSetLanguagePref_EN() {
195     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
196 
197     Locale.setDefault(new Locale("en"));
198     create();
199     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
200     // Saved language should not be empty
201     assertFalse("Saved language should not be empty", prefLang.equals(""));
202 
203     setLanguageInPreferences(ctx, ""); // cleanup
204   }
205 
206   @Test
207   public void shouldSetLanguagePref_FR() {
208     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
209 
210     Locale.setDefault(new Locale("fr"));
211     create();
212     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
213     // Saved language should not be empty
214     assertFalse("Saved language should not be empty", prefLang.equals(""));
215 
216     setLanguageInPreferences(ctx, ""); // cleanup
217   }
218 
219   @Test
220   public void shouldSetLanguagePref_DE() {
221     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
222 
223     Locale.setDefault(new Locale("de"));
224     create();
225     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
226     // Saved language should not be empty
227     assertFalse("Saved language should not be empty", prefLang.equals(""));
228 
229     setLanguageInPreferences(ctx, ""); // cleanup
230   }
231 
232   @Test
233   public void shouldSetLanguagePref_ES() {
234     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
235 
236     Locale.setDefault(new Locale("es"));
237     create();
238     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
239     // Saved language should not be empty
240     assertFalse("Saved language should not be empty", prefLang.equals(""));
241 
242     setLanguageInPreferences(ctx, ""); // cleanup
243   }
244 
245   @Test
246   public void shouldSetLanguagePref_JA() {
247     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
248 
249     Locale.setDefault(new Locale("ja"));
250     create();
251     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
252     // Saved language should not be empty
253     assertFalse("Saved language should not be empty", prefLang.equals(""));
254 
255     setLanguageInPreferences(ctx, ""); // cleanup
256   }
257 
258   @Test
259   public void shouldSetLanguagePref_FR_FR() {
260     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
261 
262     Locale.setDefault(new Locale("fr_FR"));
263     create();
264     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
265     // Saved language should not be empty
266     assertFalse("Saved language should not be empty", prefLang.equals(""));
267 
268     setLanguageInPreferences(ctx, ""); // cleanup
269   }
270 
271   @Test
272   public void shouldSetLanguagePref_FR_BE() {
273     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
274 
275     Locale.setDefault(new Locale("fr_BE"));
276     create();
277     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
278     // Saved language should not be empty
279     assertFalse("Saved language should not be empty", prefLang.equals(""));
280 
281     setLanguageInPreferences(ctx, ""); // cleanup
282   }
283 
284   @Test
285   public void shouldSetLanguagePref_None() {
286     setLanguageInPreferences(ctx, ""); // empty the language pref in the app
287 
288     Locale.setDefault(new Locale(""));
289     create();
290     String prefLang = ctx.getSharedPreferences("exo_preference", 0).getString("exo_prf_localize", "");
291     // Saved language should not be empty
292     assertFalse("Saved language should not be empty", prefLang.equals(""));
293 
294     setLanguageInPreferences(ctx, ""); // cleanup
295   }
296 
297   @Test
298   public void shouldRedirectToLoginScreen() throws Exception {
299 
300     ExoAccount srv = getServerWithDefaultValues();
301     srv.isAutoLoginEnabled = false;
302     srv.isRememberEnabled = false;
303 
304     setDefaultServerInPreferences(ctx, srv);
305 
306     create();
307 
308     ShadowActivity sActivity = shadowOf(activity);
309     Intent loginIntent = sActivity.getNextStartedActivity();
310     ShadowIntent sIntent = shadowOf(loginIntent);
311 
312     assertThat(sIntent.getComponent().getClassName(), equalTo(LoginActivity.class.getName()));
313 
314   }
315 
316 }