|
20 | 20 |
|
21 | 21 | package org.catrobat.paintroid.test.espresso |
22 | 22 |
|
| 23 | +import android.app.Activity |
| 24 | +import android.app.Instrumentation |
| 25 | +import android.content.Intent |
23 | 26 | import android.net.Uri |
24 | 27 | import android.os.Environment |
25 | 28 | import androidx.test.espresso.Espresso |
26 | 29 | import androidx.test.espresso.Espresso.onView |
27 | 30 | import androidx.test.espresso.action.ViewActions |
28 | 31 | import androidx.test.espresso.action.ViewActions.replaceText |
29 | 32 | import androidx.test.espresso.assertion.ViewAssertions.matches |
| 33 | +import androidx.test.espresso.intent.Intents.init |
| 34 | +import androidx.test.espresso.intent.Intents.intending |
| 35 | +import androidx.test.espresso.intent.Intents.release |
| 36 | +import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction |
30 | 37 | import androidx.test.espresso.matcher.RootMatchers |
31 | 38 | import androidx.test.espresso.matcher.ViewMatchers.isDisplayed |
32 | 39 | import androidx.test.espresso.matcher.ViewMatchers.withId |
@@ -65,51 +72,70 @@ class CatrobatImageIOIntegrationTest { |
65 | 72 | val screenshotOnFailRule = ScreenshotOnFailRule() |
66 | 73 |
|
67 | 74 | @get:Rule |
68 | | - val grantPermissionRule: GrantPermissionRule = EspressoUtils.grantPermissionRulesVersionCheck() |
| 75 | + val grantPermissionRule: GrantPermissionRule = |
| 76 | + EspressoUtils.grantPermissionRulesVersionCheck() |
69 | 77 |
|
70 | | - private var uriFile: Uri? = null |
71 | 78 | private lateinit var activity: MainActivity |
72 | 79 |
|
73 | 80 | companion object { |
74 | 81 | private const val IMAGE_NAME = "fileName" |
75 | 82 | } |
76 | 83 |
|
77 | 84 | @Before |
78 | | - fun setUp() { activity = launchActivityRule.activity } |
| 85 | + fun setUp() { |
| 86 | + activity = launchActivityRule.activity |
| 87 | + init() |
| 88 | + |
| 89 | + val downloadsDir = activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)!! |
| 90 | + val targetFile = File(downloadsDir, "$IMAGE_NAME.${CATROBAT_IMAGE_ENDING}") |
| 91 | + |
| 92 | + downloadsDir.mkdirs() |
| 93 | + |
| 94 | + if (!targetFile.exists()) { |
| 95 | + targetFile.createNewFile() |
| 96 | + } |
| 97 | + |
| 98 | + val fakeUri = Uri.fromFile(targetFile) |
| 99 | + |
| 100 | + val resultData = Intent().setData(fakeUri) |
| 101 | + intending(hasAction(Intent.ACTION_CREATE_DOCUMENT)) |
| 102 | + .respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, resultData)) |
| 103 | + } |
79 | 104 |
|
80 | 105 | @After |
81 | 106 | fun tearDown() { |
82 | | - val imagesDirectory = |
83 | | - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() |
84 | | - val pathToFile = imagesDirectory + File.separator + IMAGE_NAME + "." + CATROBAT_IMAGE_ENDING |
85 | | - val imageFile = File(pathToFile) |
86 | | - if (imageFile.exists()) { |
87 | | - imageFile.delete() |
88 | | - } |
| 107 | + // clean up |
| 108 | + release() |
| 109 | + activity.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) |
| 110 | + ?.let { dir -> |
| 111 | + File(dir, "$IMAGE_NAME.$CATROBAT_IMAGE_ENDING").delete() |
| 112 | + } |
89 | 113 | } |
90 | 114 |
|
91 | 115 | @Test |
92 | 116 | fun testWriteAndReadCatrobatImage() { |
93 | | - onDrawingSurfaceView() |
94 | | - .perform(UiInteractions.touchAt(DrawingSurfaceLocationProvider.MIDDLE)) |
95 | | - TopBarViewInteraction.onTopBarView() |
96 | | - .performOpenMoreOptions() |
97 | | - onView(withText(R.string.menu_save_image)) |
98 | | - .perform(ViewActions.click()) |
99 | | - onView(withId(R.id.pocketpaint_save_dialog_spinner)) |
100 | | - .perform(ViewActions.click()) |
| 117 | + onDrawingSurfaceView().perform(UiInteractions.touchAt(DrawingSurfaceLocationProvider.MIDDLE)) |
| 118 | + |
| 119 | + TopBarViewInteraction.onTopBarView().performOpenMoreOptions() |
| 120 | + onView(withText(R.string.menu_save_image)).perform(ViewActions.click()) |
| 121 | + onView(withId(R.id.pocketpaint_save_dialog_spinner)).perform(ViewActions.click()) |
101 | 122 | Espresso.onData( |
102 | 123 | AllOf.allOf( |
103 | | - Matchers.`is`(Matchers.instanceOf<Any>(String::class.java)), |
| 124 | + Matchers.`is`(Matchers.instanceOf(String::class.java)), |
104 | 125 | Matchers.`is`(FileIO.FileType.CATROBAT.value) |
105 | 126 | ) |
106 | 127 | ).inRoot(RootMatchers.isPlatformPopup()).perform(ViewActions.click()) |
107 | | - onView(withId(R.id.pocketpaint_image_name_save_text)) |
108 | | - .perform(replaceText(IMAGE_NAME)) |
| 128 | + onView(withId(R.id.pocketpaint_image_name_save_text)).perform(replaceText(IMAGE_NAME)) |
109 | 129 | onView(withText(R.string.save_button_text)).check(matches(isDisplayed())) |
110 | 130 | .perform(ViewActions.click()) |
111 | | - uriFile = activity.model.savedPictureUri!! |
112 | | - Assert.assertNotNull(uriFile) |
113 | | - Assert.assertNotNull(CommandSerializer(activity, activity.commandManager, activity.model).readFromFile(uriFile!!)) |
| 131 | + |
| 132 | + Espresso.onIdle() |
| 133 | + |
| 134 | + val uriFile = activity.model.savedPictureUri |
| 135 | + Assert.assertNotNull("savedPictureUri was never set", uriFile) |
| 136 | + |
| 137 | + val commands = CommandSerializer(activity, activity.commandManager, activity.model) |
| 138 | + .readFromFile(uriFile!!) |
| 139 | + Assert.assertNotNull("Failed to read back Catrobat commands", commands) |
114 | 140 | } |
115 | 141 | } |
0 commit comments