17
17
package com .example .android .persistence .ui ;
18
18
19
19
20
+ import static android .support .test .espresso .Espresso .onView ;
21
+ import static android .support .test .espresso .action .ViewActions .click ;
22
+ import static android .support .test .espresso .assertion .ViewAssertions .matches ;
23
+ import static android .support .test .espresso .matcher .ViewMatchers .isDisplayed ;
24
+ import static android .support .test .espresso .matcher .ViewMatchers .withContentDescription ;
25
+ import static android .support .test .espresso .matcher .ViewMatchers .withText ;
26
+
27
+ import static org .hamcrest .core .IsNot .not ;
28
+
20
29
import android .arch .core .executor .testing .CountingTaskExecutorRule ;
30
+ import android .arch .lifecycle .LiveData ;
31
+ import android .arch .lifecycle .Observer ;
32
+ import android .support .annotation .Nullable ;
33
+ import android .support .test .InstrumentationRegistry ;
21
34
import android .support .test .espresso .contrib .RecyclerViewActions ;
22
35
import android .support .test .espresso .matcher .ViewMatchers ;
23
36
import android .support .test .rule .ActivityTestRule ;
24
37
38
+ import com .example .android .persistence .AppExecutors ;
39
+ import com .example .android .persistence .EspressoTestUtil ;
40
+ import com .example .android .persistence .R ;
41
+ import com .example .android .persistence .db .AppDatabase ;
42
+
43
+ import org .hamcrest .CoreMatchers ;
44
+ import org .hamcrest .MatcherAssert ;
25
45
import org .junit .Before ;
26
46
import org .junit .Rule ;
27
47
import org .junit .Test ;
28
48
49
+ import java .util .concurrent .CountDownLatch ;
29
50
import java .util .concurrent .TimeUnit ;
30
51
import java .util .concurrent .TimeoutException ;
31
52
32
- import static android .support .test .espresso .Espresso .onView ;
33
- import static android .support .test .espresso .action .ViewActions .click ;
34
- import static android .support .test .espresso .assertion .ViewAssertions .matches ;
35
- import static android .support .test .espresso .matcher .ViewMatchers .isDisplayed ;
36
- import static android .support .test .espresso .matcher .ViewMatchers .withContentDescription ;
37
- import static android .support .test .espresso .matcher .ViewMatchers .withText ;
38
- import static org .hamcrest .core .IsNot .not ;
39
-
40
- import com .example .android .persistence .EspressoTestUtil ;
41
- import com .example .android .persistence .R ;
42
- import com .example .android .persistence .ui .MainActivity ;
43
-
44
53
public class MainActivityTest {
45
54
46
55
@ Rule
@@ -50,13 +59,43 @@ public class MainActivityTest {
50
59
@ Rule
51
60
public CountingTaskExecutorRule mCountingTaskExecutorRule = new CountingTaskExecutorRule ();
52
61
62
+ public MainActivityTest () {
63
+ // delete the database
64
+ InstrumentationRegistry .getTargetContext ().deleteDatabase (AppDatabase .DATABASE_NAME );
65
+ }
66
+
53
67
@ Before
54
68
public void disableRecyclerViewAnimations () {
69
+ // Disable RecyclerView animations
55
70
EspressoTestUtil .disableAnimations (mActivityRule );
56
71
}
57
72
73
+ @ Before
74
+ public void waitForDbCreation () throws Throwable {
75
+ final CountDownLatch latch = new CountDownLatch (1 );
76
+ final LiveData <Boolean > databaseCreated = AppDatabase .getInstance (
77
+ InstrumentationRegistry .getTargetContext (), new AppExecutors ())
78
+ .getDatabaseCreated ();
79
+ mActivityRule .runOnUiThread (new Runnable () {
80
+ @ Override
81
+ public void run () {
82
+ databaseCreated .observeForever (new Observer <Boolean >() {
83
+ @ Override
84
+ public void onChanged (@ Nullable Boolean aBoolean ) {
85
+ if (Boolean .TRUE .equals (aBoolean )) {
86
+ databaseCreated .removeObserver (this );
87
+ latch .countDown ();
88
+ }
89
+ }
90
+ });
91
+ }
92
+ });
93
+ MatcherAssert .assertThat ("database should've initialized" ,
94
+ latch .await (1 , TimeUnit .MINUTES ), CoreMatchers .is (true ));
95
+ }
96
+
58
97
@ Test
59
- public void clickOnFirstItem_opensComments () throws TimeoutException , InterruptedException {
98
+ public void clickOnFirstItem_opensComments () throws Throwable {
60
99
drain ();
61
100
// When clicking on the first product
62
101
onView (ViewMatchers .withContentDescription (R .string .cd_products_list ))
0 commit comments