Skip to content

Commit 9cfb2e8

Browse files
committed
Fix automated tests
1 parent 79a1844 commit 9cfb2e8

File tree

3 files changed

+107
-114
lines changed

3 files changed

+107
-114
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package com.jorge.boats.xkcd.view.activity;
22

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+
38
import android.app.Activity;
4-
import android.app.Instrumentation;
59
import android.content.Intent;
6-
import android.content.IntentFilter;
710
import android.os.Handler;
811
import android.os.Looper;
912
import android.support.annotation.NonNull;
1013
import android.support.test.espresso.ViewInteraction;
1114
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;
1615

1716
import static android.support.test.espresso.Espresso.onView;
1817
import static android.support.test.espresso.assertion.ViewAssertions.matches;
@@ -27,107 +26,96 @@
2726
*/
2827
public class StripeActivityTest extends ActivityInstrumentationTestCase2<StripeActivity> {
2928

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+
}
131120
}
132-
}
133121
}

presentation/src/main/java/com/jorge/boats/xkcd/view/stripe/StripeActivity.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import javax.inject.Inject;
5353

5454
import butterknife.Bind;
55+
import io.fabric.sdk.android.Fabric;
5556
import uk.co.senab.photoview.PhotoView;
5657
import uk.co.senab.photoview.PhotoViewAttacher;
5758

@@ -460,9 +461,10 @@ public void share() {
460461
intent.putExtra(Intent.EXTRA_SUBJECT, mShareableRenderedData[0]);
461462
intent.putExtra(Intent.EXTRA_TEXT, mShareableRenderedData[1]);
462463

463-
Answers.getInstance().logShare(new ShareEvent()
464-
.putContentId(mShareableRenderedData[2].toString())
465-
.putContentName(mShareableRenderedData[0].toString()));
464+
if (Fabric.isInitialized())
465+
Answers.getInstance().logShare(new ShareEvent()
466+
.putContentId(mShareableRenderedData[2].toString())
467+
.putContentName(mShareableRenderedData[0].toString()));
466468

467469
startActivity(Intent.createChooser(intent, getString(R.string.action_share_title)));
468470
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<!-- Apparently having test resources within instrumentation-scope only is not supported yet. -->
22
<resources>
3-
<string name="stub_stripe_title" translatable="false">Centrifugal Force</string>
4-
<string name="stub_previous_stripe_title" translatable="false">Quirky Girls</string>
5-
<string name="stub_next_stripe_title" translatable="false">Blogofractal</string>
3+
<string name="stub_stripe_title" translatable="false">Centrifugal Force</string>
4+
<string name="stub_stripe_description" translatable="false">You spin me right round, baby, right round, in a manner depriving me of an inertial reference frame. Baby.</string>
5+
<string name="stub_previous_stripe_title" translatable="false">Quirky Girls</string>
6+
<string name="stub_previous_stripe_description" translatable="false">Romantic comedy heroines, I\'m talking to you.</string>
7+
<string name="stub_next_stripe_title" translatable="false">Blogofractal</string>
8+
<string name="stub_next_stripe_description" translatable="false">Edward Tufte\'s \'The Visual Display of Quantitative Information\' is a fantastic book, and should be required reading for anyone in either the sciences or graphic design.</string>
69
</resources>

0 commit comments

Comments
 (0)