2 * This is an example test project created in Eclipse to test NotePad which is a sample
3 * project located in AndroidSDK/samples/android-11/NotePad
4 * Just click on File --> New --> Project --> Android Project --> Create Project from existing source and
7 * Then you can run these test cases either on the emulator or on device. You right click
8 * the test project and select Run As --> Run As Android JUnit Test
10 * @author Renas Reda, renas.reda@jayway.com
14 package org.openintents.filemanager.test;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.util.Random;
21 import android.app.Activity;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.net.Uri;
25 import android.os.Environment;
26 import android.preference.PreferenceManager;
27 import android.test.InstrumentationTestCase;
28 import android.test.suitebuilder.annotation.Smoke;
30 import com.jayway.android.robotium.solo.Solo;
32 public class TestFileManagerActivity extends InstrumentationTestCase {
34 private static final String TAG = "TestFileManagerActivity";
37 private Activity activity;
38 private Random random = new Random();
39 private Intent intent;
40 private String sdcardPath;
42 public TestFileManagerActivity() {
46 protected void setUp() throws Exception {
49 sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath()+'/';
51 // need to do this before creating activity
52 cleanDirectory(new File(sdcardPath + "oi-filemanager-tests"));
53 createDirectory(sdcardPath + "oi-filemanager-tests");
55 intent = new Intent();
56 intent.setAction("android.intent.action.MAIN");
57 intent.setClassName("org.openintents.filemanager",
58 "org.openintents.filemanager.FileManagerActivity");
59 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
61 activity = getInstrumentation().startActivitySync(intent);
63 this.solo = new Solo(getInstrumentation(), activity);
66 protected void tearDown() throws Exception {
68 this.solo.finishOpenedActivities();
69 } catch (Throwable e) {
73 cleanDirectory(new File(sdcardPath + "oi-filemanager-tests"));
76 private String getAppString(int resId) {
77 return activity.getString(resId);
81 public void test000Eula() {
82 String accept = getAppString(org.openintents.distribution.R.string.oi_distribution_eula_accept);
83 String cancel = getAppString(org.openintents.distribution.R.string.oi_distribution_eula_refuse);
84 boolean existsAccept = solo.searchButton(accept);
85 boolean existsCancel = solo.searchButton(cancel);
87 if (existsAccept && existsCancel) {
88 solo.clickOnButton(accept);
93 public void test001RecentChanges() {
94 String recentChanges = getAppString(org.openintents.distribution.R.string.oi_distribution_newversion_recent_changes);
95 String cont = getAppString(org.openintents.distribution.R.string.oi_distribution_newversion_continue);
96 while(solo.scrollUp());
97 boolean existsRecentChanges = solo.searchText(recentChanges);
98 boolean existsCont = solo.searchButton(cont);
100 if (existsRecentChanges && existsCont) {
101 solo.clickOnButton(cont);
105 private void cleanDirectory(File file) {
106 if(!file.exists()) return;
107 for(String name:file.list()) {
108 if(!name.startsWith("oi-") && !name.startsWith(".oi-")) {
109 throw new RuntimeException(file + " contains unexpected file");
111 File child = new File(file, name);
112 if(child.isDirectory())
113 cleanDirectory(child);
119 throw new RuntimeException("Deletion of " + file + " failed");
123 private void createFile(String path, String content) throws IOException {
124 File file = new File(path);
125 FileWriter wr = new FileWriter(file);
130 private void createDirectory(String path) throws IOException {
131 File file = new File(path);
134 throw new IOException("Creation of " + path + " failed");
137 private void deleteDirectory(String path) {
138 File file = new File(path);
140 if(file.isDirectory())
141 cleanDirectory(file);
145 public void testNavigation() throws IOException {
146 // if(solo.searchText("Accept")) {
147 // solo.clickOnButton("Accept");
148 // if(solo.searchButton("Continue"))
149 // solo.clickOnButton("Continue");
151 createDirectory(sdcardPath + "oi-filemanager-tests");
152 createFile(sdcardPath + "oi-filemanager-tests/oi-test.txt", "");
153 createDirectory(sdcardPath + "oi-filemanager-tests/oi-test-dir");
154 createFile(sdcardPath + "oi-filemanager-tests/oi-test-dir/oi-fff.txt", "");
156 solo.clickOnText("oi-filemanager-tests");
157 assertTrue(solo.searchText("oi-test.txt"));
158 solo.clickOnText("oi-test-dir");
159 assertTrue(solo.searchText("oi-fff.txt"));
163 solo.clickOnText("oi-filemanager-tests");
164 assertTrue(solo.searchText("oi-test.txt"));
166 solo.clickOnText("oi-test-dir");
168 assertTrue(solo.searchText("oi-test.txt"));
173 public void testModification() throws IOException {
174 createDirectory(sdcardPath + "oi-filemanager-tests");
175 createFile(sdcardPath + "oi-filemanager-tests/oi-rem-test.txt", "");
176 solo.clickOnText("oi-filemanager-tests");
177 solo.clickLongOnText("oi-rem-test.txt");
178 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.menu_delete)); // Delete
179 solo.clickOnText(getAppString(android.R.string.ok));
181 //when actionbar is present, this test case should find the first ImageButton
182 if(android.os.Build.VERSION.SDK_INT < 11)
183 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.menu_new_folder)); // New Folder
185 solo.clickOnImageButton(0);
186 solo.enterText(0, "oi-created-folder");
187 solo.clickOnText(getAppString(android.R.string.ok));
190 assertTrue(solo.searchText("oi-created-folder"));
193 File createdFolder = new File(sdcardPath + "oi-filemanager-tests/oi-created-folder");
194 assertTrue(createdFolder.exists());
195 assertTrue(createdFolder.isDirectory());
196 assertFalse(new File(sdcardPath + "oi-filemanager-tests/oi-rem-test.txt").exists());
199 public void testBookmarks() throws IOException {
200 String fn = "oi-bookmark-" + random.nextInt(1000);
201 createDirectory(sdcardPath + "oi-filemanager-tests");
202 createDirectory(sdcardPath + "oi-filemanager-tests/" + fn);
203 createFile(sdcardPath + "oi-filemanager-tests/" + fn + "/oi-inside-book.txt", "");
207 solo.clickOnText("oi-filemanager-tests");
208 solo.clickLongOnText(fn);
209 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.menu_bookmark)); // Add to bookmarks
213 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.bookmarks)); // Bookmarks
214 solo.clickOnText(fn);
215 assertTrue(solo.searchText("oi-inside-book.txt"));
221 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.settings));
222 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.bookmarks_manage));
223 solo.clickOnText(fn);
224 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.bookmarks_delete));
227 // make sure that it is deleted
229 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.bookmarks));
230 assertFalse(solo.searchText(fn));
235 public void testActions() throws IOException {
236 createDirectory(sdcardPath + "oi-filemanager-tests");
237 createDirectory(sdcardPath + "oi-filemanager-tests/oi-move-target");
238 createFile(sdcardPath + "oi-filemanager-tests/oi-file-1.txt", "");
239 createFile(sdcardPath + "oi-filemanager-tests/oi-file-2.txt", "");
240 createFile(sdcardPath + "oi-filemanager-tests/oi-file-3.txt", "");
241 createFile(sdcardPath + "oi-filemanager-tests/oi-file-4.txt", "");
242 createFile(sdcardPath + "oi-filemanager-tests/oi-file-5.txt", "");
243 solo.clickOnText("oi-filemanager-tests");
247 solo.clickLongOnText("oi-file-1.txt");
248 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.menu_copy));
249 selectTargetAndCheck("oi-move-target", "oi-file-1.txt", null);
250 assertTrue(solo.searchText("oi-file-1.txt"));
254 solo.clickLongOnText("oi-file-2.txt");
255 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.menu_move));
256 selectTargetAndCheck("oi-move-target", "oi-file-2.txt", null);
257 assertFalse(solo.searchText("oi-file-2.txt"));
261 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.menu_multi_select));
262 solo.clickOnText("oi-file-3.txt");
263 solo.clickOnText("oi-file-4.txt");
264 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.move_button_multiselect));
265 selectTargetAndCheck("oi-move-target", "oi-file-3.txt", "oi-file-4.txt");
269 solo.clickLongOnText("oi-file-5.txt");
270 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.menu_rename));
271 solo.enterText(0, "oi-renamed-file.txt");
272 solo.clickOnText(getAppString(android.R.string.ok)); // not sure what to do
273 assertTrue(solo.searchText("oi-renamed-file.txt"));
279 private void selectTargetAndCheck(String dirname, String name1, String name2) throws IOException {
280 createDirectory(sdcardPath + "oi-filemanager-tests");
281 solo.clickOnText(dirname);
282 solo.clickOnButton(getAppString(org.openintents.filemanager.R.string.copy_button) + "|" +
283 getAppString(org.openintents.filemanager.R.string.move_button));
284 solo.clickOnText(dirname);
285 assertTrue(solo.searchText(name1));
287 assertTrue(solo.searchText(name2));
291 public void testDetails() throws IOException {
292 createDirectory(sdcardPath + "oi-filemanager-tests");
293 createFile(sdcardPath + "oi-filemanager-tests/oi-detail.txt", "abcdefg");
295 solo.clickOnText("oi-filemanager-tests");
297 solo.clickLongOnText("oi-detail.txt");
298 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.menu_details));
299 assertTrue(solo.searchText(getAppString(org.openintents.filemanager.R.string.details_type_file)));
300 // depending on locale:
301 assertTrue(solo.searchText("7.00B") || solo.searchText("7.00 B") || solo.searchText("7,00B") || solo.searchText("7,00 B"));
304 //Calendar today = new GregorianCalendar();
305 //String todayString = today.get(Calendar.DAY_OF_MONTH) + "/" + today.get(Calendar.MONTH) + "/" + today.get(Calendar.YEAR);
306 //assertTrue(solo.searchText(todayString));
313 public void testFilters() throws IOException {
314 createDirectory(sdcardPath + "oi-filemanager-tests");
315 createFile(sdcardPath + "oi-filemanager-tests/oi-not-filter.txt", "");
316 createFile(sdcardPath + "oi-filemanager-tests/oi-filtered.py", "");
317 createDirectory(sdcardPath + "oi-filemanager-tests/oi-f-dir");
318 solo.clickOnText("oi-filemanager-tests");
320 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.menu_filter));
321 solo.enterText(0, ".py");
322 solo.clickOnButton(getAppString(android.R.string.ok));
324 assertTrue(solo.searchText("oi-filtered.py"));
325 assertTrue(solo.searchText("oi-f-dir"));
326 assertFalse(solo.searchText("oi-not-filter.txt"));
332 public void testHiddenFiles() throws IOException {
333 createDirectory(sdcardPath + "oi-filemanager-tests");
334 createFile(sdcardPath + "oi-filemanager-tests/.oi-hidden.txt", "");
335 solo.clickOnText("oi-filemanager-tests");
337 boolean origState = solo.searchText(".oi-hidden.txt");
339 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.settings));
341 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.preference_displayhiddenfiles_title));
343 assertTrue(origState != solo.searchText(".oi-hidden.txt"));
349 public void testOrder() throws IOException, InterruptedException {
350 createDirectory(sdcardPath + "oi-filemanager-tests");
351 createFile(sdcardPath + "oi-filemanager-tests/oi-b.txt", "bbb");
352 Thread.sleep(10); // make sure that next file is younger
353 createFile(sdcardPath + "oi-filemanager-tests/oi-a.txt", "aaaaaa");
355 createFile(sdcardPath + "oi-filemanager-tests/oi-c.txt", "");
356 solo.clickOnText("oi-filemanager-tests");
358 String[] sortOrders = activity.getResources().getStringArray(org.openintents.filemanager.R.array.preference_sortby_names);
361 setSortOrder(sortOrders[0]);
362 assertItemsInOrder("oi-a.txt", "oi-b.txt", "oi-c.txt");
364 setSortOrder(sortOrders[1]);
365 assertItemsInOrder("oi-c.txt", "oi-b.txt", "oi-a.txt");
367 setSortOrder(sortOrders[2]);
368 assertItemsInOrder("oi-b.txt", "oi-a.txt", "oi-c.txt");
371 setSortOrder(sortOrders[0]);
372 assertItemsInOrder("oi-c.txt", "oi-b.txt", "oi-a.txt");
375 private void setSortOrder(String name) {
376 solo.clickOnMenuItem(getAppString(org.openintents.filemanager.R.string.settings));
377 solo.clickOnText(getAppString(org.openintents.filemanager.R.string.preference_sortby));
378 solo.clickOnText(name);
382 private void setAscending(boolean enabled) {
383 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
384 SharedPreferences.Editor editor = settings.edit();
385 editor.putBoolean("ascending", enabled);
389 private void assertItemsInOrder(String a, String b, String c) {
390 int aPos = solo.getText(a).getTop();
391 int bPos = solo.getText(b).getTop();
392 int cPos = solo.getText(c).getTop();
399 public void testIntentSaveAs() throws IOException {
400 createDirectory(sdcardPath + "oi-filemanager-tests");
401 createFile(sdcardPath + "oi-filemanager-tests/oi-to-open.txt", "bbb");
403 Uri uri = Uri.parse("file:///mnt/sdcard/oi-filemanager-tests/oi-to-open.txt");
404 intent = new Intent("android.intent.action.VIEW", uri);
405 intent.setClassName("org.openintents.filemanager",
406 "org.openintents.filemanager.SaveAsActivity");
407 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
409 activity = getInstrumentation().startActivitySync(intent);
411 solo.enterText(0, "oi-target.txt");
412 solo.clickOnButton(getAppString(android.R.string.ok));
413 assertTrue(new File(sdcardPath + "oi-filemanager-tests/oi-to-open.txtoi-target.txt").exists());
418 public void testIntentUrl() throws IOException {
419 createDirectory(sdcardPath + "oi-filemanager-tests");
420 createDirectory(sdcardPath + "oi-filemanager-tests/oi-dir-to-open");
421 createDirectory(sdcardPath + "oi-filemanager-tests/oi-dir-to-open/oi-intent");
423 Uri uri = Uri.parse("file:///mnt/sdcard/oi-filemanager-tests/oi-dir-to-open");
424 intent = new Intent("android.intent.action.VIEW", uri);
425 intent.setClassName("org.openintents.filemanager",
426 "org.openintents.filemanager.FileManagerActivity");
427 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
429 activity = getInstrumentation().startActivitySync(intent);
431 assertTrue(solo.searchText("oi-intent"));
436 public void testIntentUri() throws IOException {
437 createDirectory(sdcardPath + "oi-filemanager-tests");
438 createFile(sdcardPath + "oi-filemanager-tests/oi-to-open.txt", "bbb");
440 Intent intent = new Intent(Intent.ACTION_VIEW);
441 intent.setData(Uri.parse("file://" + sdcardPath + "oi-filemanager-tests/oi-to-open.txt"));
442 intent.setPackage("org.openintents.filemanager");
443 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
445 activity = getInstrumentation().startActivitySync(intent);
447 assertTrue(solo.searchText("oi-to-open.txt"));
452 public void testIntentPickFile() throws IOException {
453 // startActivityForResult is, I think, impossible to test on Robotinium
454 createDirectory(sdcardPath + "oi-filemanager-tests");
455 createFile(sdcardPath + "oi-filemanager-tests/oi-pick-file", "");
457 Uri uri = Uri.parse("file:///mnt/sdcard/oi-filemanager-tests/oi-dir-to-open");
458 intent = new Intent("org.openintents.action.PICK_FILE", uri);
459 intent.setClassName("org.openintents.filemanager",
460 "org.openintents.filemanager.FileManagerActivity");
461 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
463 activity = getInstrumentation().startActivitySync(intent);
465 solo.clickOnText("oi-pick-file");
466 solo.clickOnButton(getAppString(android.R.string.ok));
471 public void testIntentRememberPickFilePath() throws IOException {
472 String[] actions = new String[]{
473 "org.openintents.action.PICK_FILE",
474 "org.openintents.action.PICK_DIRECTORY",
475 Intent.ACTION_GET_CONTENT
478 for(int i=0;i<3;i++){
479 createDirectory(sdcardPath + "oi-filemanager-tests");
480 if(i==1){ //Pick directory
481 createDirectory(sdcardPath + "oi-filemanager-tests/oi-dir-to-pick");
484 createFile(sdcardPath + "oi-filemanager-tests/oi-file-to-pick.txt", "bbb");
486 //Directory because PICK_DIRECTORY doesn't show files
487 createDirectory(sdcardPath + "oi-to-pick-test-folder-deleted");
491 Uri uri = Uri.parse("file:///mnt/sdcard"); //If there was already a remembered pick file path
492 intent = new Intent(actions[i], uri);
493 intent.setClassName("org.openintents.filemanager",
494 "org.openintents.filemanager.FileManagerActivity");
495 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
497 activity = getInstrumentation().startActivitySync(intent);
499 solo.clickOnText("oi-filemanager-tests");
500 if(i==1) //Pick directory
501 solo.clickOnText("oi-dir-to-pick");
503 solo.clickOnText("oi-file-to-pick.txt");
505 if(i != 2) // When ACTION_GET_CONTENT, the file is picked automatically, when clicked
506 solo.clickOnButton(getAppString(android.R.string.ok));
508 // Check, if we are in the oi-filemanager-tests directory
509 intent = new Intent(actions[i]);
510 intent.setClassName("org.openintents.filemanager",
511 "org.openintents.filemanager.FileManagerActivity");
512 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
513 activity = getInstrumentation().startActivitySync(intent);
518 //Delete the oi-filemanager-tests directory
519 deleteDirectory(sdcardPath + "oi-filemanager-tests");
521 //Check, if the current directory is the default (sdcardPath)
522 intent = new Intent(actions[i]);
523 intent.setClassName("org.openintents.filemanager",
524 "org.openintents.filemanager.FileManagerActivity");
525 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
526 activity = getInstrumentation().startActivitySync(intent);
528 assertTrue(solo.searchText("oi-to-pick-test-folder-deleted"));
531 (new File(sdcardPath + "oi-to-pick-test-folder-deleted")).delete();
538 public void testBrowseToOnPressEnter() throws IOException {
539 String dirPath = "oi-filemanager-tests";
540 String filename = "oi-test-is-in-right-directory";
541 createDirectory(sdcardPath + dirPath);
542 createFile(sdcardPath + dirPath + "/" + filename, "");
545 * We start at the SD card. Home ImageButton has index 0. Then there's a mnt classic button.
546 * And finally SD card ImageButton with index 1. (Android 1.x and 2.x)
548 * Remark: On Android 3.x(?) and 4.x, the index may have to be set to 2?
550 int imageButtonIndex = 0;
551 if(android.os.Build.VERSION.SDK_INT < 11)
552 imageButtonIndex = 1;
554 imageButtonIndex = 2;
555 solo.clickOnImageButton(imageButtonIndex);
557 solo.clickOnEditText(0); // Let the editText has focus to be able to send the enter key.
558 solo.enterText(0, "/"+dirPath);
559 solo.sendKey(Solo.ENTER);
561 assertTrue(solo.searchText(filename));
567 // Other possible tests:
570 // testKeyboardFilter