-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathMainActivityTests.java
More file actions
187 lines (155 loc) · 7.3 KB
/
MainActivityTests.java
File metadata and controls
187 lines (155 loc) · 7.3 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package at.tomtasche.reader.test;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isEnabled;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.util.ArrayMap;
import androidx.core.content.FileProvider;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.IdlingResource;
import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import at.tomtasche.reader.R;
import at.tomtasche.reader.ui.activity.MainActivity;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTests {
private IdlingResource m_idlingResource;
private static final Map<String, File> s_testFiles = new ArrayMap<>();
// Yes, this is ActivityTestRule instead of ActivityScenario, because ActivityScenario does not actually work.
// Issue ID may or may not be added later.
@Rule
public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void setUp() {
MainActivity mainActivity = mainActivityActivityTestRule.getActivity();
m_idlingResource = mainActivity.getOpenFileIdlingResource();
IdlingRegistry.getInstance().register(m_idlingResource);
// Close system dialogs which may cover our Activity.
// Happens frequently on slow emulators.
mainActivity.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
Intents.init();
}
@After
public void tearDown() {
Intents.release();
if (null != m_idlingResource) {
IdlingRegistry.getInstance().unregister(m_idlingResource);
}
}
private static void copy(InputStream src, File dst) throws IOException {
try (OutputStream out = new FileOutputStream(dst)) {
byte[] buf = new byte[1024];
int len;
while ((len = src.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
@BeforeClass
public static void extractTestFiles() throws IOException {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
Context appContext = instrumentation.getTargetContext();
File appCacheDir = appContext.getCacheDir();
File testDocumentsDir = new File(appCacheDir, "test-documents");
testDocumentsDir.mkdirs();
Assert.assertTrue(testDocumentsDir.exists());
AssetManager testAssetManager = instrumentation.getContext().getAssets();
for (String filename: new String[] {"test.odt", "dummy.pdf"}) {
File targetFile = new File(testDocumentsDir, filename);
try (InputStream inputStream = testAssetManager.open(filename)) {
copy(inputStream, targetFile);
}
s_testFiles.put(filename, targetFile);
}
}
@AfterClass
public static void cleanupTestFiles() {
for (File file: s_testFiles.values()) {
file.delete();
}
}
@Test
public void testODT() {
File testFile = s_testFiles.get("test.odt");
Assert.assertNotNull(testFile);
Context appCtx = InstrumentationRegistry.getInstrumentation().getTargetContext();
Uri testFileUri = FileProvider.getUriForFile(appCtx, appCtx.getPackageName() + ".provider", testFile);
Intents.intending(hasAction(Intent.ACTION_OPEN_DOCUMENT)).respondWith(
new Instrumentation.ActivityResult(Activity.RESULT_OK,
new Intent()
.setData(testFileUri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
)
);
onView(allOf(withId(R.id.menu_open), withContentDescription("Open document"), isDisplayed()))
.perform(click());
// The menu item could be either Documents or Files.
onView(allOf(withId(android.R.id.text1), anyOf(withText("Documents"), withText("Files")), isDisplayed()))
.perform(click());
// next onView will be blocked until m_idlingResource is idle.
onView(allOf(withId(R.id.menu_edit), withContentDescription("Edit document"), isEnabled()))
.withFailureHandler((error, viewMatcher) -> {
// fails on small screens, try again with overflow menu
onView(allOf(withContentDescription("More options"), isDisplayed())).perform(click());
onView(allOf(withId(R.id.menu_edit), withContentDescription("Edit document"), isDisplayed()))
.perform(click());
});
}
@Test
public void testPDF() {
File testFile = s_testFiles.get("dummy.pdf");
Assert.assertNotNull(testFile);
Context appCtx = InstrumentationRegistry.getInstrumentation().getTargetContext();
Uri testFileUri = FileProvider.getUriForFile(appCtx, appCtx.getPackageName() + ".provider", testFile);
Intents.intending(hasAction(Intent.ACTION_OPEN_DOCUMENT)).respondWith(
new Instrumentation.ActivityResult(Activity.RESULT_OK,
new Intent()
.setData(testFileUri)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
)
);
onView(allOf(withId(R.id.menu_open), withContentDescription("Open document"), isDisplayed()))
.perform(click());
// The menu item could be either Documents or Files.
onView(allOf(withId(android.R.id.text1), anyOf(withText("Documents"), withText("Files")), isDisplayed()))
.perform(click());
// next onView will be blocked until m_idlingResource is idle.
onView(allOf(withId(R.id.menu_edit), withContentDescription("Edit document"), isEnabled()))
.withFailureHandler((error, viewMatcher) -> {
// fails on small screens, try again with overflow menu
onView(allOf(withContentDescription("More options"), isDisplayed())).perform(click());
onView(allOf(withId(R.id.menu_edit), withContentDescription("Edit document"), isDisplayed()))
.perform(click());
});
}
}