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  import static org.junit.Assert.assertTrue;
24  
25  import java.util.ArrayList;
26  
27  import org.exoplatform.R;
28  import org.exoplatform.controller.home.HomeController;
29  import org.exoplatform.singleton.AccountSetting;
30  import org.exoplatform.singleton.SocialServiceHelper;
31  import org.exoplatform.ui.social.ActivityStreamFragment;
32  import org.exoplatform.ui.social.AllUpdatesFragment;
33  import org.exoplatform.ui.social.SocialTabsActivity;
34  import org.exoplatform.ui.social.TabPageIndicator;
35  import org.robolectric.Robolectric;
36  import org.robolectric.Shadows;
37  import org.robolectric.shadows.ShadowActivity;
38  import org.robolectric.shadows.ShadowLooper;
39  import org.robolectric.shadows.ShadowView;
40  import org.robolectric.shadows.httpclient.FakeHttp;
41  
42  import android.app.ActionBar;
43  import android.os.Bundle;
44  import android.support.v4.app.FragmentManager;
45  import android.support.v4.view.ViewPager;
46  import android.view.View;
47  import android.widget.LinearLayout;
48  import android.widget.TextView;
49  
50  /**
51   * Created by The eXo Platform SAS Author : Philippe Aristote
52   * paristote@exoplatform.com Apr 21, 2014
53   */
54  // @RunWith(ExoRobolectricTestRunner.class)
55  public class SocialTabsActivityTest extends ExoActivityTestUtils<SocialTabsActivity> {
56  
57    private static final String FRAGMENT_TAG = "fragment";
58  
59    TabPageIndicator            tabs;
60  
61    ViewPager                   pages;
62  
63    ActionBar                   actionBar;
64  
65    ActivityStreamFragment      fragment;
66  
67    @Override
68    // @Before
69    public void setup() {
70      controller = Robolectric.buildActivity(SocialTabsActivity.class);
71  
72      // mock response for HTTP GET /rest/api/social/version/latest.json
73      FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_VERSION_LATEST),
74                                   getResponseOKForRequest(REQ_SOCIAL_VERSION_LATEST));
75      // mock response for HTTP GET
76      // /rest/private/api/social/v1-alpha3/portal/identity/organization/{testuser}.json
77      FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_IDENTITY), getResponseOKForRequest(REQ_SOCIAL_IDENTITY));
78      // mock response for HTTP GET
79      // /rest/private/api/social/v1-alpha3/portal/identity/{testidentity}.json
80      FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_IDENTITY_2), getResponseOKForRequest(REQ_SOCIAL_IDENTITY_2));
81      // mock response for HTTP GET
82      // /rest/private/api/social/v1-alpha3/portal/activity_stream/feed.json?limit=10
83      FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_NEWS), getResponseOKForRequest(REQ_SOCIAL_NEWS));
84      // mock response for HTTP GET
85      // /rest/private/api/social/v1-alpha3/portal/activity_stream/feed.json?limit=50
86      FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_ALL_UPDATES), getResponseOKForRequest(REQ_SOCIAL_ALL_UPDATES));
87      // mock response for HTTP GET
88      // /rest/private/api/social/v1-alpha3/portal/activity_stream/connections.json
89      FakeHttp.addHttpResponseRule(getMatcherForRequest(REQ_SOCIAL_MY_CONNECTIONS),
90                                   getResponseOKForRequest(REQ_SOCIAL_MY_CONNECTIONS));
91    }
92  
93    private void init() {
94  
95      tabs = (TabPageIndicator) activity.findViewById(R.id.indicator);
96      pages = (ViewPager) activity.findViewById(R.id.pager);
97      actionBar = activity.getActionBar();
98  
99    }
100 
101   private void startFragment() {
102     FragmentManager manager = activity.getSupportFragmentManager();
103     manager.beginTransaction().add(fragment, FRAGMENT_TAG).commit();
104   }
105 
106   private Bundle createBundleWithDefaultSettings() {
107     Bundle b = new Bundle();
108     AccountSetting s = AccountSetting.getInstance();
109     s.setCurrentAccount(getServerWithDefaultValues());
110     s.cookiesList = new ArrayList<String>();
111     b.putParcelable("account_setting", s);
112     return b;
113   }
114 
115   // @Test
116   public void verifyDefaultLayout() {
117     create();
118     init();
119 
120     // TODO check
121     assertTrue("Action bar should contain 2 menu items", actionBar.getNavigationItemCount() == 2);
122     // assertNotNull("Button should exist at pos 0", actionBar.getItem(0)); //
123     // Refresh
124     // assertNotNull("Button should exist at pos 1", actionBar.getItem(1)); //
125     // Compose
126     // ActionBar.getItem() returns null when there is no item at the given
127     // position
128     // assertNull("Button should not exist at pos 2", actionBar.getItem(2));
129 
130     assertThat(pages.getAdapter()).hasCount(4); // Should have 4 pages
131     assertThat("Default tab should be All Updates", pages.getCurrentItem(), equalTo(SocialTabsActivity.ALL_UPDATES));
132 
133     LinearLayout tabsLayout = (LinearLayout) tabs.getChildAt(0);
134     // check each tab's title
135     String[] tabsTitle = activity.getResources().getStringArray(R.array.SocialTabs);
136     assertThat((TextView) tabsLayout.getChildAt(SocialTabsActivity.ALL_UPDATES)).containsText(tabsTitle[SocialTabsActivity.ALL_UPDATES]);
137     assertThat((TextView) tabsLayout.getChildAt(SocialTabsActivity.MY_CONNECTIONS)).containsText(tabsTitle[SocialTabsActivity.MY_CONNECTIONS]);
138     assertThat((TextView) tabsLayout.getChildAt(SocialTabsActivity.MY_SPACES)).containsText(tabsTitle[SocialTabsActivity.MY_SPACES]);
139     assertThat((TextView) tabsLayout.getChildAt(SocialTabsActivity.MY_STATUS)).containsText(tabsTitle[SocialTabsActivity.MY_STATUS]);
140 
141   }
142 
143   // @Test
144   public void shouldMoveToOtherPage() {
145     create();
146     init();
147 
148     tabs.setCurrentItem(SocialTabsActivity.ALL_UPDATES);
149     assertThat("Should be on All Updates tab", pages.getCurrentItem(), equalTo(SocialTabsActivity.ALL_UPDATES)); // remains
150                                                                                                                  // on
151                                                                                                                  // All
152                                                                                                                  // Updates
153 
154     tabs.setCurrentItem(SocialTabsActivity.MY_CONNECTIONS);
155     assertThat("Should be on My Connections tab", pages.getCurrentItem(), equalTo(SocialTabsActivity.MY_CONNECTIONS)); // moves
156                                                                                                                        // to
157                                                                                                                        // My
158                                                                                                                        // Connections
159 
160     LinearLayout tabsLayout = (LinearLayout) tabs.getChildAt(0);
161 
162     ShadowView.clickOn((View) tabsLayout.getChildAt(SocialTabsActivity.MY_SPACES));
163     assertThat("Should be on My Spaces tab", pages.getCurrentItem(), equalTo(SocialTabsActivity.MY_SPACES)); // moves
164                                                                                                              // to
165                                                                                                              // My
166                                                                                                              // Spaces
167 
168   }
169 
170   // @Test
171   public void shouldFinishActivity() {
172     create();
173     init();
174 
175     // home button is the 1st child of the action bar layout
176     // TODO check
177     // ImageButton homeBtn = (ImageButton) actionBar.getChildAt(0);
178     View homeBtn = actionBar.getTabAt(0).getCustomView();
179     ShadowView.clickOn(homeBtn);
180 
181     ShadowActivity sActivity = Shadows.shadowOf(activity);
182 
183     assertTrue("Activity should be finishing", sActivity.isFinishing());
184   }
185 
186   /*
187    * TEST FRAGMENTS
188    */
189 
190   // @Test
191   public void shouldLoadAllUpdatesFragment() {
192     createWithBundle(createBundleWithDefaultSettings());
193     init();
194 
195     HomeController c = new HomeController(activity);
196     // TODO check
197     c.launchNewsService();
198 
199     fragment = AllUpdatesFragment.getInstance();
200 
201     // Adding a call to Robolectric.runUiThreadTasksIncludingDelayedTasks()
202     // ensures that the async tasks execute completely
203     ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
204 
205     assertThat("Fragment ID should be ALL_UPDATES",
206                ((AllUpdatesFragment) fragment).getThisTabId(),
207                equalTo(SocialTabsActivity.ALL_UPDATES));
208     assertNotNull("SocialServiceHelper should not be null", SocialServiceHelper.getInstance());
209 
210     assertNotNull("Social Info List should not be null", SocialServiceHelper.getInstance().socialInfoList);
211     assertThat("Social Info List should contain 2 items", SocialServiceHelper.getInstance().socialInfoList.size(), equalTo(2));
212 
213     FakeHttp.clearPendingHttpResponses();
214   }
215 
216 }