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.assertThat;
22  import static org.junit.Assert.assertTrue;
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.login.LoginProxy;
31  import org.exoplatform.ui.setting.ServerEditionActivity;
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.junit.runner.RunWith;
36  import org.robolectric.Robolectric;
37  import org.robolectric.RuntimeEnvironment;
38  import org.robolectric.shadows.ShadowToast;
39  import org.robolectric.shadows.ShadowView;
40  
41  import android.content.Context;
42  import android.content.Intent;
43  import android.content.SharedPreferences;
44  import android.util.Log;
45  import android.widget.Button;
46  import android.widget.EditText;
47  import android.widget.TextView;
48  
49  @RunWith(ExoRobolectricTestRunner.class)
50  public class ServerEditionActivityTest extends ExoActivityTestUtils<ServerEditionActivity> {
51  
52    TextView /* mTitleTxt, */ mServerName, mServerURL, mUsername, mPassword;
53  
54    EditText                  mServerNameEditTxt, mServerUrlEditTxt, mUserEditTxt, mPassEditTxt;
55  
56    Button                    mOkBtn, mDeleteBtn;
57  
58    ExoAccount                thisServer = null;
59  
60    @Override
61    @Before
62    public void setup() {
63      controller = Robolectric.buildActivity(ServerEditionActivity.class);
64    }
65  
66    @Override
67    @After
68    public void teardown() {
69      Context ctx = RuntimeEnvironment.application.getApplicationContext();
70      deleteAllAccounts(ctx);
71      LoginProxy.userIsLoggedIn = false;
72      super.teardown();
73    }
74  
75    /**
76     * Create the activity with a server passed in extra and configured as the
77     * only and default server
78     */
79    public void createWithDefaultServer() {
80      thisServer = getServerWithDefaultValues();
81      Context ctx = RuntimeEnvironment.application.getApplicationContext();
82      setDefaultServerInPreferences(ctx, thisServer);
83      Intent i = new Intent(ctx, ServerEditionActivity.class);
84      i.putExtra("EXO_SERVER_OBJ", thisServer);
85      super.createWithIntent(i);
86      init();
87    }
88  
89    private void init() {
90      // mTitleTxt = (TextView)
91      // activity.findViewById(R.id.server_setting_title_txt);
92      mServerName = (TextView) activity.findViewById(R.id.server_setting_server_name_txt);
93      mServerURL = (TextView) activity.findViewById(R.id.server_setting_server_url_txt);
94      mUsername = (TextView) activity.findViewById(R.id.server_setting_user_txt);
95      mPassword = (TextView) activity.findViewById(R.id.server_setting_pass_txt);
96      mServerNameEditTxt = (EditText) activity.findViewById(R.id.server_setting_server_name_edit_txt);
97      mServerUrlEditTxt = (EditText) activity.findViewById(R.id.server_setting_server_url_edit_txt);
98      mUserEditTxt = (EditText) activity.findViewById(R.id.server_setting_user_edit_txt);
99      mPassEditTxt = (EditText) activity.findViewById(R.id.server_setting_pass_edit_txt);
100     mOkBtn = (Button) activity.findViewById(R.id.server_setting_ok_btn);
101     mDeleteBtn = (Button) activity.findViewById(R.id.server_setting_delete_btn);
102   }
103 
104   @Test
105   public void verifyDefaultLayout() {
106     createWithDefaultServer();
107 
108     // TODO check
109     assertTrue("Incorrect activity title", thisServer.accountName.equalsIgnoreCase(activity.getTitle().toString()));
110     assertThat(mServerName).containsText(R.string.ServerName);
111     assertThat(mServerNameEditTxt).containsText(thisServer.accountName);
112     assertThat(mServerURL).containsText(R.string.ServerUrl);
113     assertThat(mServerUrlEditTxt).containsText(thisServer.serverUrl);
114     assertThat(mUsername).containsText(R.string.UserNameCellTitle);
115     assertThat(mUsername).hasHint(null); // hint is null because username is
116                                          // set
117     assertThat(mUserEditTxt).containsText(thisServer.username);
118     assertThat(mPassword).containsText(R.string.PasswordCellTitle);
119     assertThat(mPassword).hasHint(null); // hint is null because password is
120                                          // set
121     assertThat(mPassEditTxt).containsText(thisServer.password);
122     assertThat(mOkBtn).isVisible().isClickable().containsText(R.string.OK);
123     assertThat(mDeleteBtn).isVisible().isClickable().containsText(R.string.Delete);
124 
125   }
126 
127   @Test
128   public void verifyDefaultLayoutWithLimitedEdit() {
129     LoginProxy.userIsLoggedIn = true; // simulate signed-in user
130     createWithDefaultServer();
131     // Server name is editable and contains the current account name
132     assertThat(mServerNameEditTxt).containsText(thisServer.accountName);
133     assertThat(mServerNameEditTxt).isEnabled();
134 
135     // Server URL is *not* editable and contains the current server URL
136     assertThat(mServerUrlEditTxt).containsText(thisServer.serverUrl);
137     assertThat(mServerUrlEditTxt).isDisabled().overridingErrorMessage("Server URL field should be disabled");
138 
139     // Account username is *not* editable and contains the current account
140     // username
141     assertThat(mUserEditTxt).containsText(thisServer.username);
142     assertThat(mUserEditTxt).isDisabled().overridingErrorMessage("Username field should be disabled");
143 
144     // Account password is *not* editable and contains the current account
145     // password
146     assertThat(mPassEditTxt).containsText(thisServer.password);
147     assertThat(mPassEditTxt).isDisabled().overridingErrorMessage("Password field should be disabled");
148 
149     // Delete button is not displayed
150     assertThat(mDeleteBtn).isGone().overridingErrorMessage("Delete button should be hidden");
151   }
152 
153   @Test
154   public void verifyEditOneAccount() {
155 
156     createWithDefaultServer();
157     Context ctx = RuntimeEnvironment.application.getApplicationContext();
158     final String newName = TEST_SERVER_NAME + " new";
159     final String newURL = TEST_SERVER_URL + ".fr";
160     final String newUser = TEST_USER_NAME + "_new";
161     final String newPass = TEST_USER_PWD + "_new";
162 
163     assertThat("1 server should already exist", ServerSettingHelper.getInstance().getServerInfoList(ctx).size(), equalTo(1));
164 
165     mServerNameEditTxt.setText(newName);
166     mServerUrlEditTxt.setText(newURL);
167     mUserEditTxt.setText(newUser);
168     mPassEditTxt.setText(newPass);
169 
170     ShadowView.clickOn(mOkBtn);
171 
172     assertThat("Only 1 server should exist", ServerSettingHelper.getInstance().getServerInfoList(ctx).size(), equalTo(1));
173     assertTrue("Should have displayed toast ServerUpdated but displayed '" + ShadowToast.getTextOfLatestToast() + "' instead.",
174                ShadowToast.showedToast(ctx.getResources().getString(R.string.ServerUpdated)));
175 
176     ExoAccount srv = ServerSettingHelper.getInstance().getServerInfoList(ctx).get(0);
177 
178     assertThat("Account name should have been modified", srv.accountName, equalTo(newName));
179     assertThat("Account server should have been modified", srv.serverUrl, equalTo(newURL));
180     assertThat("Account username should have been modified", srv.username, equalTo(newUser));
181     assertThat("Account password should have been modified", srv.password, equalTo(newPass));
182   }
183 
184   @Test
185   public void verifyDeleteOneAccount() {
186     createWithDefaultServer();
187     Context ctx = RuntimeEnvironment.application.getApplicationContext();
188 
189     assertThat("1 server should already exist", ServerSettingHelper.getInstance().getServerInfoList(ctx).size(), equalTo(1));
190 
191     ShadowView.clickOn(mDeleteBtn);
192 
193     assertThat("Server should have been deleted", ServerSettingHelper.getInstance().getServerInfoList(ctx).size(), equalTo(0));
194     assertTrue("Should have displayed toast ServerDeleted but displayed '" + ShadowToast.getTextOfLatestToast() + "' instead.",
195                ShadowToast.showedToast(ctx.getResources().getString(R.string.ServerDeleted)));
196   }
197 
198   @Test
199   public void verifyEditAccountFailsWithIncorrectAccountName() {
200     createWithDefaultServer();
201     Context ctx = RuntimeEnvironment.application.getApplicationContext();
202 
203     final String newInvalidAccountName = TEST_SERVER_NAME + " ** new";
204 
205     mServerNameEditTxt.setText(newInvalidAccountName);
206 
207     ShadowView.clickOn(mOkBtn);
208 
209     ExoAccount srv = ServerSettingHelper.getInstance().getServerInfoList(ctx).get(0);
210 
211     assertThat("Account name should NOT have been modified", srv.accountName, equalTo(TEST_SERVER_NAME));
212     assertTrue("Should have displayed toast AccountNameInvalid but displayed '" + ShadowToast.getTextOfLatestToast()
213         + "' instead.", ShadowToast.showedToast(ctx.getResources().getString(R.string.AccountNameInvalid)));
214   }
215 
216   @Test
217   public void verifyEditAccountFailsWithIncorrectAccountServer() {
218     createWithDefaultServer();
219     Context ctx = RuntimeEnvironment.application.getApplicationContext();
220 
221     final String newInvalidAccountServer = TEST_WRONG_SERVER_URL;
222 
223     mServerUrlEditTxt.setText(newInvalidAccountServer);
224 
225     ShadowView.clickOn(mOkBtn);
226 
227     ExoAccount srv = ServerSettingHelper.getInstance().getServerInfoList(ctx).get(0);
228 
229     assertThat("Account Server URL should NOT have been modified", srv.serverUrl, equalTo(TEST_SERVER_URL));
230     assertTrue("Should have displayed toast AccountServerInvalid but displayed '" + ShadowToast.getTextOfLatestToast()
231         + "' instead.", ShadowToast.showedToast(ctx.getResources().getString(R.string.AccountServerInvalid)));
232   }
233 
234   @Test
235   public void verifyEditAccountFailsWithForbiddenAccountServer() {
236     createWithDefaultServer();
237     Context ctx = RuntimeEnvironment.application.getApplicationContext();
238 
239     final String newForbiddenAccountServer = "http://exoplatform.net";
240 
241     mServerUrlEditTxt.setText(newForbiddenAccountServer);
242 
243     ShadowView.clickOn(mOkBtn);
244 
245     ExoAccount srv = ServerSettingHelper.getInstance().getServerInfoList(ctx).get(0);
246 
247     assertThat("Account Server URL should NOT have been modified", srv.serverUrl, equalTo(TEST_SERVER_URL));
248     assertTrue("Should have displayed toast AccountServerForbidden but displayed '" + ShadowToast.getTextOfLatestToast()
249         + "' instead.", ShadowToast.showedToast(ctx.getResources().getString(R.string.AccountServerForbidden)));
250   }
251 
252   @Test
253   public void verifyEditAccountFailsWithIncorrectAccountUserName() {
254     createWithDefaultServer();
255     Context ctx = RuntimeEnvironment.application.getApplicationContext();
256 
257     final String newInvalidUsername = TEST_USER_NAME + " ** new";
258 
259     mUserEditTxt.setText(newInvalidUsername);
260 
261     ShadowView.clickOn(mOkBtn);
262 
263     ExoAccount srv = ServerSettingHelper.getInstance().getServerInfoList(ctx).get(0);
264 
265     assertThat("Account username should NOT have been modified", srv.username, equalTo(TEST_USER_NAME));
266     assertTrue("Should have displayed toast AccountUsernameInvalid but displayed '" + ShadowToast.getTextOfLatestToast()
267         + "' instead.", ShadowToast.showedToast(ctx.getResources().getString(R.string.AccountUsernameInvalid)));
268   }
269 
270   @Test
271   public void verifyAccountIsSelectedWhenOnlyOneRemainsAfterDelete() {
272     Context ctx = RuntimeEnvironment.application.getApplicationContext();
273 
274     // Create 2 accounts
275     thisServer = getServerWithDefaultValues();
276     thisServer.accountName = TEST_SERVER_NAME + " one";
277     thisServer.serverUrl = TEST_SERVER_URL + ".net";
278 
279     ExoAccount acc2 = getServerWithDefaultValues();
280     acc2.accountName = TEST_SERVER_NAME + " two";
281     acc2.serverUrl = TEST_SERVER_URL + ".org";
282 
283     ArrayList<ExoAccount> servers = new ArrayList<ExoAccount>(2);
284     servers.add(thisServer);
285     servers.add(acc2);
286     addServersInPreferences(ctx, servers);
287 
288     // Select the 1st account
289     SharedPreferences.Editor prefs = ctx.getSharedPreferences("exo_preference", 0).edit();
290     prefs.putString("exo_prf_domain_index", "0");
291     prefs.commit();
292 
293     // Start activity with thisServer (the 1st account)
294     Intent i = new Intent(ctx, ServerEditionActivity.class);
295     i.putExtra("EXO_SERVER_OBJ", thisServer);
296     super.createWithIntent(i);
297     init();
298 
299     // Delete the current account (the 1st)
300     ShadowView.clickOn(mDeleteBtn);
301 
302     ExoAccount remainingAcc = AccountSetting.getInstance().getCurrentAccount();
303 
304     Log.d(TAG_TEST, remainingAcc.accountName);
305     Log.d(TAG_TEST, acc2.accountName);
306 
307     assertTrue("The 2nd account should have been selected automatically when the 1st was deleted", acc2.equals(remainingAcc));
308 
309   }
310 
311 }