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.robolectric.Shadows.shadowOf;
22  
23  import org.exoplatform.R;
24  import org.exoplatform.ui.CirclePageIndicator;
25  import org.exoplatform.ui.SignInActivity;
26  import org.exoplatform.ui.SignUpActivity;
27  import org.exoplatform.ui.WelcomeActivity;
28  import org.exoplatform.ui.login.LoginActivity;
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.robolectric.Robolectric;
33  import org.robolectric.shadows.ShadowActivity;
34  import org.robolectric.shadows.ShadowIntent;
35  import org.robolectric.shadows.ShadowView;
36  
37  import android.content.Intent;
38  import android.content.res.Resources;
39  import android.os.Bundle;
40  import android.support.v4.view.ViewPager;
41  import android.widget.Button;
42  import android.widget.TextView;
43  
44  /**
45   * Created by The eXo Platform SAS Author : Philippe Aristote
46   * paristote@exoplatform.com Apr 8, 2014
47   */
48  @RunWith(ExoRobolectricTestRunner.class)
49  public class WelcomeActivityTest extends ExoActivityTestUtils<WelcomeActivity> {
50  
51    Resources           res;
52  
53    Button              btnLogin, btnSignUp;
54  
55    TextView            skipTxt;
56  
57    ViewPager           pager;
58  
59    CirclePageIndicator indicator;
60  
61    @Before
62    public void setup() {
63      controller = Robolectric.buildActivity(WelcomeActivity.class);
64    }
65  
66    @Override
67    public void create() {
68      super.create();
69      init();
70    }
71  
72    @Override
73    public void createWithBundle(Bundle b) {
74      super.createWithBundle(b);
75      init();
76    }
77  
78    private void init() {
79      res = activity.getResources();
80      btnLogin = (Button) activity.findViewById(R.id.welcome_btn_login);
81      btnSignUp = (Button) activity.findViewById(R.id.welcome_btn_signup);
82      skipTxt = (TextView) activity.findViewById(R.id.welcome_txt_skipStep);
83      pager = (ViewPager) activity.findViewById(org.exoplatform.R.id.pager);
84      indicator = (CirclePageIndicator) activity.findViewById(R.id.circle_page_indicator);
85    }
86  
87    @Test
88    public void verifyDefaultLayout() {
89      create();
90  
91      assertThat(pager.getAdapter()).hasCount(5); // pager shows 5 screenshots
92  
93      // check Log In button label
94      assertThat(btnLogin).containsText(R.string.LogIn);
95      org.junit.Assert.assertThat(res.getString(R.string.LogIn), equalTo("Log In"));
96  
97      // check Sign Up button label
98      assertThat(btnSignUp).containsText(R.string.SignUp);
99      org.junit.Assert.assertThat(res.getString(R.string.SignUp), equalTo("Sign Up"));
100 
101     assertThat(skipTxt).containsText(R.string.SkipStep);
102     org.junit.Assert.assertThat(res.getString(R.string.SkipStep), equalTo("or skip this for now"));
103 
104   }
105 
106   @Test
107   public void shouldRedirectToSignInScreen() {
108     create();
109 
110     ShadowView.clickOn(btnLogin);
111     ShadowActivity shadowActivity = shadowOf(activity);
112     Intent startedIntent = shadowActivity.getNextStartedActivity();
113     ShadowIntent shadowIntent = shadowOf(startedIntent);
114     org.junit.Assert.assertThat(shadowIntent.getComponent().getClassName(), equalTo(SignInActivity.class.getName()));
115   }
116 
117   @Test
118   public void shouldRedirectToSignUpScreen() {
119     create();
120 
121     ShadowView.clickOn(btnSignUp);
122     ShadowActivity shadowActivity = shadowOf(activity);
123     Intent startedIntent = shadowActivity.getNextStartedActivity();
124     ShadowIntent shadowIntent = shadowOf(startedIntent);
125     org.junit.Assert.assertThat(shadowIntent.getComponent().getClassName(), equalTo(SignUpActivity.class.getName()));
126   }
127 
128   @Test
129   public void shouldRedirectToLoginScreen() {
130     create();
131 
132     ShadowView.clickOn(skipTxt);
133     ShadowActivity shadowActivity = shadowOf(activity);
134     Intent startedIntent = shadowActivity.getNextStartedActivity();
135     ShadowIntent shadowIntent = shadowOf(startedIntent);
136     org.junit.Assert.assertThat(shadowIntent.getComponent().getClassName(), equalTo(LoginActivity.class.getName()));
137   }
138 
139   @Test
140   public void shouldSaveInstanceState() {
141     create();
142 
143     Bundle bundle = new Bundle();
144     bundle.putInt("CURRENT_SLIDER", -1); // init current page to -1 to make
145                                          // sure the value is saved by the
146                                          // activity
147 
148     controller.saveInstanceState(bundle); // save the activity state in the
149                                           // bundle
150 
151     // check that the current page number has been saved
152     org.junit.Assert.assertThat(bundle.getInt("CURRENT_SLIDER"), equalTo(0));
153 
154   }
155 
156   @Test
157   public void shoudRetrieveInstanceState() {
158     Bundle bundle = new Bundle();
159     bundle.putInt("CURRENT_SLIDER", 1); // change the value of the current
160                                         // page
161     createWithBundle(bundle);
162 
163     assertThat(pager).hasCurrentItem(1); // check that the previous state
164                                          // was retrieved
165   }
166 
167 }