Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. package ch.epfl.sweng.groupup.activity.event.file;
  2.  
  3. import android.app.Activity;
  4. import android.app.Instrumentation;
  5. import android.content.ContentResolver;
  6. import android.content.Intent;
  7. import android.content.res.Resources;
  8. import android.net.Uri;
  9. import android.support.test.InstrumentationRegistry;
  10. import android.support.test.espresso.Espresso;
  11. import android.support.test.espresso.intent.Intents;
  12. import android.support.test.rule.ActivityTestRule;
  13. import android.support.test.runner.AndroidJUnit4;
  14.  
  15. import com.google.firebase.database.DataSnapshot;
  16. import com.google.firebase.database.DatabaseError;
  17. import com.google.firebase.database.ValueEventListener;
  18.  
  19. import org.hamcrest.Matcher;
  20. import org.junit.After;
  21. import org.junit.Before;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.runner.RunWith;
  25.  
  26. import ch.epfl.sweng.groupup.R;
  27. import ch.epfl.sweng.groupup.activity.event.creation.EventCreationActivity;
  28. import ch.epfl.sweng.groupup.lib.database.Database;
  29. import ch.epfl.sweng.groupup.object.account.Account;
  30.  
  31. import static android.support.test.espresso.Espresso.onView;
  32. import static android.support.test.espresso.action.ViewActions.click;
  33. import static android.support.test.espresso.action.ViewActions.swipeLeft;
  34. import static android.support.test.espresso.action.ViewActions.typeText;
  35. import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
  36. import static android.support.test.espresso.assertion.ViewAssertions.matches;
  37. import static android.support.test.espresso.intent.Intents.intended;
  38. import static android.support.test.espresso.intent.Intents.intending;
  39. import static android.support.test.espresso.intent.matcher.IntentMatchers.hasAction;
  40. import static android.support.test.espresso.intent.matcher.IntentMatchers.hasData;
  41. import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
  42. import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
  43. import static android.support.test.espresso.matcher.ViewMatchers.withId;
  44. import static android.support.test.espresso.matcher.ViewMatchers.withParent;
  45. import static android.support.test.espresso.matcher.ViewMatchers.withText;
  46. import static org.hamcrest.Matchers.allOf;
  47. import static org.hamcrest.Matchers.is;
  48. import static org.hamcrest.Matchers.not;
  49.  
  50. @RunWith(AndroidJUnit4.class)
  51. public class MediaSharingTests {
  52.  
  53. @Rule
  54. public final ActivityTestRule<EventCreationActivity> mActivityRule =
  55. new ActivityTestRule<>(EventCreationActivity.class);
  56.  
  57. @Before
  58. public void goToFileManagement(){
  59. Database.setUp();
  60. Account.shared.clear();
  61. Database.setUpEventListener(new ValueEventListener() {
  62. @Override
  63. public void onDataChange(DataSnapshot dataSnapshot) {
  64. //Do nothing
  65. }
  66.  
  67. @Override
  68. public void onCancelled(DatabaseError databaseError) {
  69. //Do nothing
  70. }
  71. });
  72. createEvent();
  73. onView(withParent(withId(R.id.linear_layout_event_list))).perform(click());
  74. onView(withId(R.id.swipe_bar))
  75. .perform(swipeLeft());
  76. }
  77.  
  78. @After
  79. public void clearDatabase(){
  80. Account.shared.clear();
  81. }
  82.  
  83. @Test
  84. public void addingPictureWithoutExceptionAndDisplayFullScreen(){
  85. Resources resources = InstrumentationRegistry.getTargetContext().getResources();
  86.  
  87. Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
  88. resources.getResourcePackageName(R.mipmap.ic_launcher) + '/' +
  89. resources.getResourceTypeName(R.mipmap.ic_launcher) + '/' +
  90. resources.getResourceEntryName(R.mipmap.ic_launcher));
  91.  
  92. mockMediaSelection(imageUri);
  93.  
  94. onView(withParent(withId(R.id.image_grid)))
  95. .check(matches(isDisplayed()));
  96.  
  97. onView(withParent(withId(R.id.image_grid)))
  98. .perform(click());
  99.  
  100. onView(withId(R.id.show_image))
  101. .check(matches(isDisplayed()));
  102.  
  103. onView(withId(R.id.show_image))
  104. .perform(click());
  105.  
  106. onView(withParent(withId(R.id.image_grid)))
  107. .check(matches(isDisplayed()));
  108.  
  109. mockMediaSelection(imageUri);
  110.  
  111. }
  112.  
  113.  
  114. @Test
  115. public void fileNotFoundToastOnWrongURI(){
  116.  
  117. mockMediaSelection(Uri.parse("scrogneugneu"));
  118.  
  119. onView(withText(R.string.file_management_toast_error_file_uri))
  120. .inRoot(withDecorView(not(is(mActivityRule.getActivity()
  121. .getWindow()
  122. .getDecorView()))))
  123. .check(matches(isDisplayed()));
  124.  
  125. }
  126.  
  127. @Test
  128. public void fileNotFoundToastOnNullURI(){
  129.  
  130. mockMediaSelection(null);
  131.  
  132. onView(withText(R.string.file_management_toast_error_file_uri))
  133. .inRoot(withDecorView(not(is(mActivityRule.getActivity()
  134. .getWindow()
  135. .getDecorView()))))
  136. .check(matches(isDisplayed()));
  137.  
  138. }
  139.  
  140. @Test
  141. public void fileNotAddedOnBadResult(){
  142.  
  143. Resources resources = InstrumentationRegistry.getTargetContext().getResources();
  144.  
  145. Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
  146. resources.getResourcePackageName(R.mipmap.ic_launcher) + '/' +
  147. resources.getResourceTypeName(R.mipmap.ic_launcher) + '/' +
  148. resources.getResourceEntryName(R.mipmap.ic_launcher));
  149.  
  150. mockWrongSelection(imageUri);
  151.  
  152. onView(withParent(withId(R.id.image_grid)))
  153. .check(doesNotExist());
  154. }
  155.  
  156. @Test
  157. public void openSlideShowView(){
  158. Resources resources = InstrumentationRegistry.getTargetContext().getResources();
  159.  
  160. Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
  161. resources.getResourcePackageName(R.mipmap.ic_launcher) + '/' +
  162. resources.getResourceTypeName(R.mipmap.ic_launcher) + '/' +
  163. resources.getResourceEntryName(R.mipmap.ic_launcher));
  164.  
  165. mockMediaSelection(imageUri);
  166.  
  167. onView(withParent(withId(R.id.image_grid)))
  168. .check(matches(isDisplayed()));
  169.  
  170. onView(withId(R.id.create_aftermovie)).perform(click());
  171.  
  172. onView(withId(R.id.imageSwitcher)).check(matches(isDisplayed()));
  173. }
  174.  
  175. private void mockWrongSelection(Uri imageUri){
  176. Intent resultData = new Intent();
  177. resultData.setData(imageUri);
  178. Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(
  179. Activity.RESULT_CANCELED, resultData);
  180.  
  181. Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_PICK),
  182. hasData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
  183. Intents.init();
  184. intending(expectedIntent).respondWith(result);
  185.  
  186. onView(withId(R.id.add_files)).perform(click());
  187. intended(expectedIntent);
  188. Intents.release();
  189. }
  190.  
  191. private void mockMediaSelection(Uri imageUri){
  192.  
  193. Intent resultData = new Intent();
  194. resultData.setData(imageUri);
  195. Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(
  196. Activity.RESULT_OK, resultData);
  197.  
  198. Matcher<Intent> expectedIntent = allOf(hasAction(Intent.ACTION_PICK),
  199. hasData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI));
  200. Intents.init();
  201. intending(expectedIntent).respondWith(result);
  202.  
  203. onView(withId(R.id.add_files)).perform(click());
  204. intended(expectedIntent);
  205. Intents.release();
  206. }
  207.  
  208. private void createEvent(){
  209. final String EVENT_NAME = "My event";
  210. onView(withId(R.id.ui_edit_event_name))
  211. .perform(typeText(EVENT_NAME));
  212. Espresso.closeSoftKeyboard();
  213.  
  214. onView(withId(R.id.toolbar_image_right))
  215. .perform(click());
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement