1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package com.mapbox.mapboxsdk.testapp.storage;
import android.os.Looper;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import com.mapbox.mapboxsdk.storage.FileSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.action.WaitAction;
import com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity;
import org.hamcrest.Matcher;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.mapbox.mapboxsdk.testapp.action.OrientationChangeAction.orientationLandscape;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
@RunWith(AndroidJUnit4.class)
public class FileSourceTest {
@Rule
public ActivityTestRule<FeatureOverviewActivity> rule = new ActivityTestRule<>(FeatureOverviewActivity.class);
private FileSource fileSource;
@Before
public void setUp() throws Exception {
onView(withId(R.id.recyclerView)).perform(new FileSourceCreator());
}
@Test
public void testDefault() throws Exception {
assertFalse("FileSource should not be active", fileSource.isActivated());
}
@Test
public void testActivateDeactivate() throws Exception {
assertFalse("1) FileSource should not be active", fileSource.isActivated());
onView(withId(R.id.recyclerView)).perform(new FileSourceActivator(true));
assertTrue("2) FileSource should be active", fileSource.isActivated());
onView(withId(R.id.recyclerView)).perform(new FileSourceActivator(false));
assertFalse("3) FileSource should not be active", fileSource.isActivated());
}
@Test
public void testOpenCloseMapView() throws Exception {
assertFalse("1) FileSource should not be active", fileSource.isActivated());
onView(withText("Simple Map")).perform(click());
onView(withId(R.id.mapView)).perform(new WaitAction());
assertTrue("2) FileSource should be active", fileSource.isActivated());
onView(withId(R.id.mapView)).perform(new WaitAction());
pressBack();
assertFalse("3) FileSource should not be active", fileSource.isActivated());
}
@Test
@Ignore
public void testRotateMapView() throws Exception {
assertFalse("1) FileSource should not be active", fileSource.isActivated());
onView(withText("Simple Map")).perform(click());
onView(withId(R.id.mapView)).perform(new WaitAction());
onView(isRoot()).perform(orientationLandscape());
onView(withId(R.id.mapView)).perform(new WaitAction());
assertTrue("2) FileSource should be active", fileSource.isActivated());
onView(withId(R.id.mapView)).perform(new WaitAction());
pressBack();
assertFalse("3) FileSource should not be active", fileSource.isActivated());
}
private class FileSourceCreator implements ViewAction {
@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}
@Override
public String getDescription() {
return "Creates the filesource instance on the UI thread";
}
@Override
public void perform(UiController uiController, View view) {
assertTrue(Looper.myLooper() == Looper.getMainLooper());
fileSource = FileSource.getInstance(rule.getActivity());
}
}
private class FileSourceActivator implements ViewAction {
private boolean activate;
FileSourceActivator(boolean activate) {
this.activate = activate;
}
@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}
@Override
public String getDescription() {
return "Creates the filesource instance on the UI thread";
}
@Override
public void perform(UiController uiController, View view) {
assertTrue(Looper.myLooper() == Looper.getMainLooper());
if (activate) {
fileSource.activate();
} else {
fileSource.deactivate();
}
}
}
}
|