23
23
24
24
package org .catrobat .catroid .uiespresso .ui .fragment ;
25
25
26
+ import android .app .Activity ;
27
+ import android .app .Instrumentation ;
28
+ import android .content .Intent ;
29
+ import android .net .Uri ;
30
+ import android .os .Environment ;
26
31
import android .widget .EditText ;
27
32
28
33
import org .catrobat .catroid .ProjectManager ;
29
34
import org .catrobat .catroid .R ;
35
+ import org .catrobat .catroid .common .Constants ;
30
36
import org .catrobat .catroid .common .ScreenModes ;
31
37
import org .catrobat .catroid .content .Project ;
38
+ import org .catrobat .catroid .io .ResourceImporter ;
32
39
import org .catrobat .catroid .io .StorageOperations ;
33
40
import org .catrobat .catroid .io .asynctask .ProjectSaveTask ;
34
41
import org .catrobat .catroid .ui .ProjectActivity ;
35
42
import org .catrobat .catroid .uiespresso .util .UiTestUtils ;
36
43
import org .catrobat .catroid .uiespresso .util .rules .FragmentActivityTestRule ;
44
+ import org .hamcrest .Matcher ;
45
+ import org .hamcrest .core .AllOf ;
37
46
import org .junit .After ;
38
47
import org .junit .Assert ;
39
48
import org .junit .Before ;
49
58
50
59
import androidx .test .core .app .ApplicationProvider ;
51
60
import androidx .test .espresso .action .ViewActions ;
61
+ import androidx .test .espresso .intent .Intents ;
52
62
import androidx .test .ext .junit .runners .AndroidJUnit4 ;
63
+ import androidx .test .platform .app .InstrumentationRegistry ;
53
64
65
+ import static org .catrobat .catroid .R .id .tab_layout ;
54
66
import static org .catrobat .catroid .common .Constants .CATROBAT_EXTENSION ;
55
- import static org .catrobat .catroid .common .FlavoredConstants .DEFAULT_ROOT_DIRECTORY ;
56
67
import static org .catrobat .catroid .common .Constants .EXTERNAL_STORAGE_ROOT_EXPORT_DIRECTORY ;
68
+ import static org .catrobat .catroid .common .FlavoredConstants .DEFAULT_ROOT_DIRECTORY ;
69
+ import static org .catrobat .catroid .uiespresso .util .actions .TabActionsKt .selectTabAtPosition ;
70
+ import static org .catrobat .catroid .uiespresso .util .matchers .BundleMatchers .bundleHasExtraIntent ;
71
+ import static org .catrobat .catroid .uiespresso .util .matchers .BundleMatchers .bundleHasMatchingString ;
57
72
import static org .hamcrest .Matchers .allOf ;
58
73
import static org .hamcrest .Matchers .anyOf ;
74
+ import static org .hamcrest .Matchers .equalTo ;
75
+ import static org .hamcrest .Matchers .hasItem ;
59
76
import static org .hamcrest .Matchers .instanceOf ;
60
77
61
78
import static androidx .test .espresso .Espresso .closeSoftKeyboard ;
66
83
import static androidx .test .espresso .action .ViewActions .replaceText ;
67
84
import static androidx .test .espresso .assertion .ViewAssertions .doesNotExist ;
68
85
import static androidx .test .espresso .assertion .ViewAssertions .matches ;
86
+ import static androidx .test .espresso .intent .Intents .intended ;
87
+ import static androidx .test .espresso .intent .Intents .intending ;
88
+ import static androidx .test .espresso .intent .matcher .IntentMatchers .hasAction ;
89
+ import static androidx .test .espresso .intent .matcher .IntentMatchers .hasCategories ;
90
+ import static androidx .test .espresso .intent .matcher .IntentMatchers .hasComponent ;
91
+ import static androidx .test .espresso .intent .matcher .IntentMatchers .hasExtras ;
92
+ import static androidx .test .espresso .intent .matcher .IntentMatchers .hasType ;
69
93
import static androidx .test .espresso .matcher .RootMatchers .isDialog ;
70
94
import static androidx .test .espresso .matcher .ViewMatchers .isDisplayed ;
71
95
import static androidx .test .espresso .matcher .ViewMatchers .isNotChecked ;
@@ -85,7 +109,7 @@ public class ProjectOptionsTest {
85
109
@ Rule
86
110
public FragmentActivityTestRule <ProjectActivity > baseActivityTestRule =
87
111
new FragmentActivityTestRule <>(ProjectActivity .class , ProjectActivity .EXTRA_FRAGMENT_POSITION ,
88
- ProjectActivity .FRAGMENT_SPRITES );
112
+ ProjectActivity .FRAGMENT_SPRITES );
89
113
90
114
@ Before
91
115
public void setUp () throws Exception {
@@ -108,7 +132,7 @@ public void tearDown() {
108
132
}
109
133
110
134
@ Test
111
- public void changeProjectName () {
135
+ public void changeProjectName () throws IOException {
112
136
onView (allOf (withText (PROJECT_NAME ), isDisplayed (), instanceOf (EditText .class )))
113
137
.perform (replaceText (EXISTING_PROJECT_NAME ));
114
138
@@ -131,6 +155,72 @@ public void changeProjectName() {
131
155
onView (withText (NEW_PROJECT_NAME ))
132
156
.check (matches (isDisplayed ()));
133
157
Assert .assertEquals (NEW_PROJECT_NAME , project .getName ());
158
+
159
+ onView (withText (R .string .default_project_background_name ))
160
+ .perform (click ());
161
+
162
+ onView (withId (tab_layout )).perform (selectTabAtPosition (1 ));
163
+
164
+ onView (withId (R .id .button_add ))
165
+ .perform (click ());
166
+
167
+ Matcher <Intent > expectedPaintNewLookIntent = createLookFromPaintroid ();
168
+
169
+ onView (withId (R .id .dialog_new_look_paintroid ))
170
+ .perform (click ());
171
+
172
+ intended (expectedPaintNewLookIntent );
173
+
174
+ onView (withText ("Background (1)" ))
175
+ .check (matches (isDisplayed ()));
176
+ }
177
+
178
+ private Matcher <Intent > createLookFromPaintroid () throws IOException {
179
+ File tmpDir = new File (
180
+ Environment .getExternalStorageDirectory ().getAbsolutePath (), "Pocket Code Test Temp" );
181
+ String lookFileName = "catroid_sunglasses.png" ;
182
+
183
+ Intents .init ();
184
+
185
+ Matcher <Intent > expectedGetContentIntent = AllOf .allOf (
186
+ hasAction ("android.intent.action.GET_CONTENT" ),
187
+ hasType ("image/*" ));
188
+
189
+ String chooserTitle = UiTestUtils .getResourcesString (R .string .select_look_from_gallery );
190
+ Matcher <Intent > expectedChooserIntent = AllOf .allOf (
191
+ hasAction ("android.intent.action.CHOOSER" ),
192
+ hasExtras (bundleHasMatchingString ("android.intent.extra.TITLE" , chooserTitle )),
193
+ hasExtras (bundleHasExtraIntent (expectedGetContentIntent )));
194
+
195
+ if (!tmpDir .exists ()) {
196
+ tmpDir .mkdirs ();
197
+ }
198
+
199
+ File imageFile = ResourceImporter .createImageFileFromResourcesInDirectory (
200
+ InstrumentationRegistry .getInstrumentation ().getContext ().getResources (),
201
+ org .catrobat .catroid .test .R .drawable .catroid_banzai ,
202
+ tmpDir ,
203
+ lookFileName ,
204
+ 1 );
205
+
206
+ Intent resultData = new Intent ();
207
+ resultData .setData (Uri .fromFile (imageFile ));
208
+
209
+ Instrumentation .ActivityResult result =
210
+ new Instrumentation .ActivityResult (Activity .RESULT_OK , resultData );
211
+
212
+ intending (expectedChooserIntent ).respondWith (result );
213
+
214
+ Matcher <Intent > expectedPaintNewLookIntent = AllOf .allOf (
215
+ hasComponent (Constants .POCKET_PAINT_INTENT_ACTIVITY_NAME ),
216
+ hasAction ("android.intent.action.MAIN" ),
217
+ hasCategories (hasItem (equalTo ("android.intent.category.LAUNCHER" ))));
218
+
219
+ Instrumentation .ActivityResult resultPaintroid = new Instrumentation .ActivityResult (Activity .RESULT_OK , null );
220
+
221
+ intending (expectedPaintNewLookIntent ).respondWith (resultPaintroid );
222
+
223
+ return expectedPaintNewLookIntent ;
134
224
}
135
225
136
226
@ Test
0 commit comments