1+ package net .osmtracker .layouts ;
2+
3+ import android .content .SharedPreferences ;
4+ import android .preference .PreferenceManager ;
5+
6+ import androidx .test .espresso .Espresso ;
7+ import androidx .test .espresso .assertion .ViewAssertions ;
8+ import androidx .test .rule .ActivityTestRule ;
9+
10+ import net .osmtracker .OSMTracker ;
11+ import net .osmtracker .R ;
12+ import net .osmtracker .activity .TrackManager ;
13+ import net .osmtracker .util .TestUtils ;
14+
15+ import org .apache .commons .io .FileUtils ;
16+ import org .junit .Rule ;
17+ import org .junit .Test ;
18+
19+ import java .util .Locale ;
20+
21+ import static androidx .test .platform .app .InstrumentationRegistry .getInstrumentation ;
22+ import static androidx .test .espresso .Espresso .onData ;
23+ import static androidx .test .espresso .Espresso .onView ;
24+ import static androidx .test .espresso .Espresso .openActionBarOverflowOrOptionsMenu ;
25+ import static androidx .test .espresso .action .ViewActions .click ;
26+ import static androidx .test .espresso .action .ViewActions .scrollTo ;
27+ import static androidx .test .espresso .matcher .PreferenceMatchers .withTitleText ;
28+ import static androidx .test .espresso .matcher .ViewMatchers .isDisplayed ;
29+ import static androidx .test .espresso .matcher .ViewMatchers .withId ;
30+ import static androidx .test .espresso .matcher .ViewMatchers .withText ;
31+ import static junit .framework .TestCase .fail ;
32+
33+ public class DownloadLayoutTest {
34+ @ Rule
35+ public ActivityTestRule <TrackManager > mRule = new ActivityTestRule (TrackManager .class ) {
36+ @ Override
37+ protected void beforeActivityLaunched () {
38+ // Skip cool intro
39+ SharedPreferences dtPrefs = PreferenceManager
40+ .getDefaultSharedPreferences (getInstrumentation ().getTargetContext ());
41+ dtPrefs .edit ().putBoolean (OSMTracker .Preferences .KEY_DISPLAY_APP_INTRO , false ).apply ();
42+ }
43+ };
44+
45+ @ Test
46+ public void downloadLayoutTest () {
47+ deleteLayoutsDirectory ();
48+
49+ TestUtils .setLayoutsTestingRepository ();
50+
51+ String layoutName = "abc" ;
52+
53+ navigateToAvailableLayouts ();
54+
55+ clickButtonsToDownloadLayout (layoutName );
56+
57+ makePostDownloadAssertions (layoutName );
58+ }
59+
60+
61+ public void deleteLayoutsDirectory (){
62+ try {
63+ FileUtils .deleteDirectory (TestUtils .getLayoutsDirectory ());
64+ }catch (Exception e ){
65+ e .printStackTrace ();
66+ fail ();
67+ }
68+ }
69+
70+
71+ /**
72+ * Assuming being in TrackManager
73+ */
74+ public void navigateToAvailableLayouts (){
75+ openActionBarOverflowOrOptionsMenu (getInstrumentation ().getTargetContext ());
76+
77+ onView (withText (TestUtils .getStringResource (R .string .menu_settings ))).perform (click ());
78+
79+ onData (withTitleText (TestUtils .getStringResource (R .string .prefs_ui_buttons_layout ))).perform (scrollTo (), click ());
80+
81+ onView (withId (R .id .launch_available )).perform (click ());
82+ }
83+
84+
85+ /**
86+ * Check the new layouts appears as a new option
87+ * Select the layout and check its buttons are shown when tracking
88+ * @param layoutName
89+ */
90+ private void makePostDownloadAssertions (String layoutName ) {
91+ Espresso .pressBack ();
92+
93+ // Check the layout appears as a new option in AvailableLayouts
94+ onView (withText (layoutName .toLowerCase ())).check (ViewAssertions .matches (isDisplayed ()));
95+
96+ // Select the layout
97+ onView (withText (layoutName .toLowerCase ())).perform (click ());
98+
99+ // Go to TrackLogger
100+ Espresso .pressBack ();
101+ Espresso .pressBack ();
102+ onView (withId (R .id .trackmgr_fab )).perform (click ());
103+
104+ // Check the buttons are loaded correctly
105+ String expectedButtonsLabels [] = new String []{"A" , "B" , "C" };
106+ for (String label : expectedButtonsLabels )
107+ onView (withText (label )).check (ViewAssertions .matches (isDisplayed ()));
108+
109+ }
110+
111+
112+ private void clickButtonsToDownloadLayout (String layoutName ) {
113+ onView (withText (layoutName )).perform (click ());
114+
115+ // Catch languages available dialog that shows up when the cell phone is not in English
116+ if (! Locale .getDefault ().getLanguage ().equalsIgnoreCase ("en" )) {
117+ onView (withText ("English" )).perform (click ());
118+ }
119+
120+ onView (withText (TestUtils .getStringResource (R .string .available_layouts_description_dialog_positive_confirmation ))).
121+ perform (click ());
122+
123+ TestUtils .checkToastIsShownWith (TestUtils .getStringResource (R .string .available_layouts_successful_download ));
124+ }
125+ }
0 commit comments