11package org .wordpress .android .support ;
22
3- import android .app .Instrumentation ;
4- import android .util .Log ;
53
6- import androidx .annotation .Nullable ;
4+ import androidx .annotation .NonNull ;
75import androidx .compose .ui .test .junit4 .ComposeTestRule ;
86import androidx .test .espresso .accessibility .AccessibilityChecks ;
97import androidx .test .ext .junit .rules .ActivityScenarioRule ;
10- import androidx .test .platform .app .InstrumentationRegistry ;
11-
12- import com .fasterxml .jackson .databind .util .ISO8601Utils ;
13- import com .github .jknack .handlebars .Helper ;
14- import com .github .jknack .handlebars .Options ;
15- import com .github .tomakehurst .wiremock .client .WireMock ;
16- import com .github .tomakehurst .wiremock .extension .responsetemplating .ResponseTemplateTransformer ;
17- import com .github .tomakehurst .wiremock .extension .responsetemplating .helpers .DateOffset ;
18- import com .github .tomakehurst .wiremock .extension .responsetemplating .helpers .HandlebarsHelper ;
19- import com .github .tomakehurst .wiremock .junit .WireMockRule ;
8+
209import com .google .android .apps .common .testing .accessibility .framework .AccessibilityCheckResult ;
2110import com .google .android .apps .common .testing .accessibility .framework .AccessibilityCheckResult .AccessibilityCheckResultType ;
11+ import com .google .gson .internal .bind .util .ISO8601Utils ;
2212
23- import org .apache .commons .lang3 .LocaleUtils ;
2413import org .hamcrest .Matcher ;
2514import org .hamcrest .Matchers ;
2615import org .junit .Before ;
3120import org .wordpress .android .e2e .flows .LoginFlow ;
3221import org .wordpress .android .e2e .pages .MePage ;
3322import org .wordpress .android .e2e .pages .MySitesPage ;
34- import org .wordpress .android .editor .Utils ;
35- import org .wordpress .android .mocks .AndroidNotifier ;
36- import org .wordpress .android .mocks .AssetFileSource ;
3723import org .wordpress .android .rules .RetryTestRule ;
3824import org .wordpress .android .ui .WPLaunchActivity ;
39- import org .wordpress .android .wiremock .WireMockStub ;
4025
41- import java .io .IOException ;
4226import java .text .SimpleDateFormat ;
4327import java .util .Date ;
44- import java .util .HashMap ;
45- import java .util .List ;
4628import java .util .Locale ;
4729import java .util .TimeZone ;
4830
4931import dagger .hilt .android .testing .HiltAndroidRule ;
5032
5133import static androidx .compose .ui .test .junit4 .AndroidComposeTestRule_androidKt .createComposeRule ;
52- import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
5334import static com .google .android .apps .common .testing .accessibility .framework .AccessibilityCheckResultUtils .matchesTypes ;
5435import static org .hamcrest .Matchers .anyOf ;
5536import static org .hamcrest .Matchers .is ;
6041
6142public class BaseTest {
6243 static final String TAG = BaseTest .class .getSimpleName ();
63- public static final int WIREMOCK_PORT = 8080 ;
6444
6545 @ Rule (order = 0 )
6646 public HiltAndroidRule mHiltRule = new HiltAndroidRule (this );
@@ -76,52 +56,8 @@ public class BaseTest {
7656 = new ActivityScenarioRule <>(WPLaunchActivity .class );
7757
7858 @ Rule (order = 4 )
79- public WireMockRule wireMockRule ;
80-
81- @ Rule (order = 5 )
8259 public RetryTestRule retryTestRule = new RetryTestRule ();
8360
84- public BaseTest () {
85- this (null );
86- }
87-
88- /**
89- * Constructor
90- *
91- * @param wireMockStubs the wiremock stubs to use for this specific test.
92- */
93- public BaseTest (@ Nullable final List <WireMockStub > wireMockStubs ) {
94- Instrumentation instrumentation = InstrumentationRegistry .getInstrumentation ();
95- wireMockRule = new WireMockRule (
96- options ().port (WIREMOCK_PORT )
97- .fileSource (
98- new AssetFileSource (instrumentation .getContext ().getAssets ())
99- )
100-
101- .extensions (new ResponseTemplateTransformer (true , new HashMap <String , Helper >() {
102- {
103- put ("fnow" , new UnlocalizedDateHelper ());
104- }
105- }))
106- .notifier (new AndroidNotifier ()));
107- if (wireMockStubs != null && !wireMockStubs .isEmpty ()) {
108- for (WireMockStub wireMockStub : wireMockStubs ) {
109- try {
110- final String result = Utils .getStringFromInputStream (
111- instrumentation .getContext ().getClassLoader ().getResourceAsStream (
112- wireMockStub .getFileName ()
113- )
114- );
115- // This is where we can stub out
116- wireMockRule .stubFor (WireMock .get (WireMock .urlPathMatching (wireMockStub .getUrlPath ().getPath ()))
117- .willReturn (WireMock .aResponse ().withBody (result )));
118- } catch (final Exception exception ) {
119- Log .e (TAG , "Problem stubbing endpoint" , exception );
120- }
121- }
122- }
123- }
124-
12561 @ Before
12662 public void setup () {
12763 Matcher <? super AccessibilityCheckResult > nonErrorLevelMatcher =
@@ -172,27 +108,6 @@ private void wpLogout() {
172108 }
173109}
174110
175- class UnlocalizedDateHelper extends HandlebarsHelper <Object > {
176- @ Override public Object apply (Object context , Options options ) throws IOException {
177- String format = options .hash ("format" , null );
178- String offset = options .hash ("offset" , null );
179- String timezone = options .hash ("timezone" , null );
180- String localeCode = options .hash ("locale" , "en_US_POSIX" );
181-
182- Date date = new Date ();
183- if (offset != null ) {
184- date = new DateOffset (offset ).shift (date );
185- }
186-
187- Locale locale = Locale .getDefault ();
188- if (localeCode != null ) {
189- locale = LocaleUtils .toLocale (localeCode );
190- }
191-
192- return new LocaleAwareRenderableDate (date , format , timezone , locale );
193- }
194- }
195-
196111class LocaleAwareRenderableDate {
197112 private static final long DIVIDE_MILLISECONDS_TO_SECONDS = 1000L ;
198113
@@ -208,7 +123,7 @@ class LocaleAwareRenderableDate {
208123 this .mLocale = locale ;
209124 }
210125
211- @ Override
126+ @ NonNull @ Override
212127 public String toString () {
213128 if (mFormat != null ) {
214129 if (mFormat .equals ("epoch" )) {
0 commit comments