|
1 | 1 | package com.jorge.boats.xkcd.view.activity;
|
2 | 2 |
|
| 3 | +import com.jorge.boats.xkcd.CustomViewMatchers; |
| 4 | +import com.jorge.boats.xkcd.R; |
| 5 | +import com.jorge.boats.xkcd.domain.entity.DomainStripe; |
| 6 | +import com.jorge.boats.xkcd.view.stripe.StripeActivity; |
| 7 | + |
3 | 8 | import android.app.Activity;
|
4 |
| -import android.app.Instrumentation; |
5 | 9 | import android.content.Intent;
|
6 |
| -import android.content.IntentFilter; |
7 | 10 | import android.os.Handler;
|
8 | 11 | import android.os.Looper;
|
9 | 12 | import android.support.annotation.NonNull;
|
10 | 13 | import android.support.test.espresso.ViewInteraction;
|
11 | 14 | import android.test.ActivityInstrumentationTestCase2;
|
12 |
| -import com.jorge.boats.xkcd.CustomViewMatchers; |
13 |
| -import com.jorge.boats.xkcd.R; |
14 |
| -import com.jorge.boats.xkcd.domain.entity.DomainStripe; |
15 |
| -import com.jorge.boats.xkcd.view.stripe.StripeActivity; |
16 | 15 |
|
17 | 16 | import static android.support.test.espresso.Espresso.onView;
|
18 | 17 | import static android.support.test.espresso.assertion.ViewAssertions.matches;
|
|
27 | 26 | */
|
28 | 27 | public class StripeActivityTest extends ActivityInstrumentationTestCase2<StripeActivity> {
|
29 | 28 |
|
30 |
| - private static final long STUB_STRIPE_NUM = 123; |
31 |
| - private static final long LOAD_WAIT_TIME_MILLISECONDS = 1000; |
32 |
| - private static final long SHARE_WAIT_TIME_MILLISECONDS = 5000; |
33 |
| - |
34 |
| - private final DomainStripe mStubObject = new DomainStripe(); |
35 |
| - |
36 |
| - private Activity mActivity; |
37 |
| - |
38 |
| - public StripeActivityTest() { |
39 |
| - super(StripeActivity.class); |
40 |
| - } |
41 |
| - |
42 |
| - @Override protected void setUp() throws Exception { |
43 |
| - super.setUp(); |
44 |
| - this.setActivityIntent(createTargetIntent()); |
45 |
| - mActivity = getActivity(); |
46 |
| - |
47 |
| - this.mStubObject.setTitle( |
48 |
| - getInstrumentation().getTargetContext().getString(R.string.stub_stripe_title)); |
49 |
| - } |
50 |
| - |
51 |
| - @Override protected void tearDown() throws Exception { |
52 |
| - super.tearDown(); |
53 |
| - if (mActivity != null) mActivity.finish(); |
54 |
| - } |
55 |
| - |
56 |
| - @NonNull private Intent createTargetIntent() { |
57 |
| - return StripeActivity.getCallingIntent(getInstrumentation().getTargetContext(), |
58 |
| - STUB_STRIPE_NUM); |
59 |
| - } |
60 |
| - |
61 |
| - public void testPrevious() { |
62 |
| - final ViewInteraction titleInteraction = onView(withId(R.id.title)), descriptionInteraction = |
63 |
| - onView(withId(R.id.description)); |
64 |
| - |
65 |
| - waitForLoad(); |
66 |
| - |
67 |
| - new Handler(Looper.getMainLooper()).post(new Runnable() { |
68 |
| - @Override public void run() { |
69 |
| - mActivity.findViewById(R.id.fab_index_zero).performClick(); |
70 |
| - } |
71 |
| - }); |
72 |
| - |
73 |
| - titleInteraction.check(matches(CustomViewMatchers.withText(equalTo( |
74 |
| - getInstrumentation().getTargetContext().getString(R.string.stub_previous_stripe_title))))); |
75 |
| - //TODO Check description interaction |
76 |
| - |
77 |
| - onView(withId(R.id.retry)).check(matches(not((isDisplayed())))); |
78 |
| - } |
79 |
| - |
80 |
| - public void testShare() { |
81 |
| - final IntentFilter intentFilter = new IntentFilter(); |
82 |
| - final Instrumentation.ActivityMonitor receiverActivityMonitor = |
83 |
| - getInstrumentation().addMonitor(intentFilter, null, false); |
84 |
| - |
85 |
| - intentFilter.addAction(Intent.ACTION_CHOOSER); |
86 |
| - |
87 |
| - waitForLoad(); |
88 |
| - |
89 |
| - mActivity.runOnUiThread(new Runnable() { |
90 |
| - @Override public void run() { |
91 |
| - mActivity.findViewById(R.id.fab_index_two).performClick(); |
92 |
| - } |
93 |
| - }); |
94 |
| - |
95 |
| - getInstrumentation().waitForIdleSync(); |
96 |
| - assertNotNull(receiverActivityMonitor.waitForActivityWithTimeout(SHARE_WAIT_TIME_MILLISECONDS)); |
97 |
| - getInstrumentation().removeMonitor(receiverActivityMonitor); |
98 |
| - } |
99 |
| - |
100 |
| - public void testNext() { |
101 |
| - final ViewInteraction titleInteraction = onView(withId(R.id.title)), descriptionInteraction = |
102 |
| - onView(withId(R.id.description)); |
103 |
| - |
104 |
| - waitForLoad(); |
105 |
| - |
106 |
| - new Handler(Looper.getMainLooper()).post(new Runnable() { |
107 |
| - @Override public void run() { |
108 |
| - mActivity.findViewById(R.id.fab_index_three).performClick(); |
109 |
| - } |
110 |
| - }); |
111 |
| - |
112 |
| - titleInteraction.check(matches(CustomViewMatchers.withText(equalTo( |
113 |
| - getInstrumentation().getTargetContext().getString(R.string.stub_next_stripe_title))))); |
114 |
| - //TODO Check description interaction |
115 |
| - |
116 |
| - onView(withId(R.id.retry)).check(matches(not((isDisplayed())))); |
117 |
| - } |
118 |
| - |
119 |
| - /** |
120 |
| - * This should be avoided if possible. The reason why it is used this time is because usage of |
121 |
| - * RxJava requires that we either use something like a TestSubscriber, which we <i>can't |
122 |
| - * because we don't have access to the UC</i>, mock the repository with Mockito and inject it to |
123 |
| - * the activity (which I'm not fully sure how I would do just yet) for usage with |
124 |
| - * rxPresso (recommended) or wait to ensure completion, which is the option chosen. |
125 |
| - */ |
126 |
| - private static void waitForLoad() { |
127 |
| - try { |
128 |
| - Thread.sleep(LOAD_WAIT_TIME_MILLISECONDS); |
129 |
| - } catch (final InterruptedException e) { |
130 |
| - e.printStackTrace(System.err); |
| 29 | + private static final long STUB_STRIPE_NUM = 123; |
| 30 | + private static final long LOAD_WAIT_TIME_MILLISECONDS = 1000; |
| 31 | + |
| 32 | + private final DomainStripe mStubObject = new DomainStripe(); |
| 33 | + |
| 34 | + private Activity mActivity; |
| 35 | + |
| 36 | + public StripeActivityTest() { |
| 37 | + super(StripeActivity.class); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void setUp() throws Exception { |
| 42 | + super.setUp(); |
| 43 | + this.setActivityIntent(createTargetIntent()); |
| 44 | + mActivity = getActivity(); |
| 45 | + |
| 46 | + this.mStubObject.setTitle( |
| 47 | + getInstrumentation().getTargetContext().getString(R.string.stub_stripe_title)); |
| 48 | + this.mStubObject.setAlt( |
| 49 | + getInstrumentation().getTargetContext().getString(R.string.stub_stripe_description)); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void tearDown() throws Exception { |
| 54 | + super.tearDown(); |
| 55 | + if (mActivity != null) mActivity.finish(); |
| 56 | + } |
| 57 | + |
| 58 | + @NonNull |
| 59 | + private Intent createTargetIntent() { |
| 60 | + return StripeActivity.getCallingIntent(getInstrumentation().getTargetContext(), |
| 61 | + STUB_STRIPE_NUM); |
| 62 | + } |
| 63 | + |
| 64 | + public void testPrevious() { |
| 65 | + final ViewInteraction titleInteraction = onView(withId(R.id.title)), descriptionInteraction = |
| 66 | + onView(withId(R.id.description)); |
| 67 | + |
| 68 | + waitForLoad(); |
| 69 | + |
| 70 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 71 | + @Override |
| 72 | + public void run() { |
| 73 | + mActivity.findViewById(R.id.fab_index_zero).performClick(); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + titleInteraction.check(matches(CustomViewMatchers.withText(equalTo( |
| 78 | + getInstrumentation().getTargetContext().getString(R.string.stub_previous_stripe_title))))); |
| 79 | + descriptionInteraction.check(matches(CustomViewMatchers.withText(equalTo( |
| 80 | + getInstrumentation().getTargetContext().getString(R.string.stub_previous_stripe_description))))); |
| 81 | + |
| 82 | + onView(withId(R.id.retry)).check(matches(not((isDisplayed())))); |
| 83 | + } |
| 84 | + |
| 85 | + public void testNext() { |
| 86 | + final ViewInteraction titleInteraction = onView(withId(R.id.title)), descriptionInteraction = |
| 87 | + onView(withId(R.id.description)); |
| 88 | + |
| 89 | + waitForLoad(); |
| 90 | + |
| 91 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 92 | + @Override |
| 93 | + public void run() { |
| 94 | + mActivity.findViewById(R.id.fab_index_three).performClick(); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + titleInteraction.check(matches(CustomViewMatchers.withText(equalTo( |
| 99 | + getInstrumentation().getTargetContext().getString(R.string.stub_next_stripe_title))))); |
| 100 | + descriptionInteraction.check(matches(CustomViewMatchers.withText(equalTo( |
| 101 | + getInstrumentation().getTargetContext().getString(R.string.stub_next_stripe_description))))); |
| 102 | + |
| 103 | + onView(withId(R.id.retry)).check(matches(not((isDisplayed())))); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * This should be avoided if possible. The reason why it is used this time is because usage of |
| 108 | + * RxJava requires that we either use something like a TestSubscriber, which we <i>can't |
| 109 | + * because we don't have access to the UC</i>, mock the repository with Mockito and inject it |
| 110 | + * to |
| 111 | + * the activity (which I'm not fully sure how I would do just yet) for usage with |
| 112 | + * rxPresso (recommended) or wait to ensure completion, which is the option chosen. |
| 113 | + */ |
| 114 | + private static void waitForLoad() { |
| 115 | + try { |
| 116 | + Thread.sleep(LOAD_WAIT_TIME_MILLISECONDS); |
| 117 | + } catch (final InterruptedException e) { |
| 118 | + e.printStackTrace(System.err); |
| 119 | + } |
131 | 120 | }
|
132 |
| - } |
133 | 121 | }
|
0 commit comments