Skip to content

Commit 193da58

Browse files
committed
Merge into main
2 parents e512cb6 + a2247fa commit 193da58

File tree

10 files changed

+119
-231
lines changed

10 files changed

+119
-231
lines changed

.github/workflows/blueprints.yaml

Lines changed: 6 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ name: blueprints
33
on:
44
push:
55
branches:
6-
- dev-compose
6+
- main
7+
- views
78
pull_request:
89
branches:
9-
- dev-compose
10+
- main
11+
- views
1012

1113
jobs:
1214
build:
13-
runs-on: macos-latest
15+
runs-on: macos-latest # Needed for emulators
1416
timeout-minutes: 45
1517

1618
steps:
@@ -48,132 +50,21 @@ jobs:
4850
- name: Robolectric Prod
4951
run: ./gradlew spotlessCheck assembleMockDebug assembleProdDebug testMockDebugUnitTest testProdDebugUnitTest --stacktrace
5052

51-
# test:
52-
# needs: build
53-
# runs-on: macOS-latest # enables hardware acceleration in the virtual machine
54-
# timeout-minutes: 30
55-
# strategy:
56-
# matrix:
57-
# api-level: [23, 29]
58-
#
59-
# steps:
60-
# - name: Checkout
61-
# uses: actions/checkout@v2
62-
#
63-
# - name: Copy CI gradle.properties
64-
# run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties
65-
#
66-
# - name: Set up JDK 11
67-
# uses: actions/setup-java@v1
68-
# with:
69-
# java-version: 11
70-
#
71-
72-
#
73-
# - name: Upload test reports
74-
# if: always()
75-
# uses: actions/upload-artifact@v2
76-
# with:
77-
# name: test-reports
78-
# path: app/build/reports/
79-
80-
#
81-
# gradleManagedVirtualDevicesTest:
82-
# needs: build
83-
# runs-on: macos-latest
84-
# timeout-minutes: 30
85-
#
86-
# steps:
87-
# - name: Checkout
88-
# uses: actions/checkout@v2
89-
#
90-
# - name: Copy CI gradle.properties
91-
# run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties
92-
#
93-
# - name: Set up JDK 11
94-
# uses: actions/setup-java@v1
95-
# with:
96-
# java-version: 11
97-
9853
# Needed to accept licenses
9954
- name: Setup Android SDK
10055
uses: android-actions/setup-android@v2
10156

102-
# - name: Generate cache key for Gradle cache
103-
# run: ./scripts/checksum.sh $SAMPLE_PATH checksum.txt
104-
#
105-
# - uses: actions/cache@v2
106-
# with:
107-
# path: |
108-
# ~/.gradle/caches/modules-*
109-
# ~/.gradle/caches/jars-*
110-
# ~/.gradle/caches/build-cache-*
111-
# key: gradle-${{ hashFiles('checksum.txt') }}
112-
113-
# - name: Cache pixel2api30 system image
114-
# uses: actions/cache@v2
115-
# with:
116-
# path: |
117-
# ~/.android/gradle/avd/dev30_aosp-atd_x86_Pixel_2.avd.*
118-
# key: pixel2api30
119-
#
120-
# - name: Cache pixel2api27 system image
121-
# uses: actions/cache@v2
122-
# with:
123-
# path: ~/.android/gradle/avd/dev27_aosp_x86_Pixel_2.*
124-
# key: pixel2api27
125-
#
126-
# - name: Cache nexus9api29 system image
127-
# uses: actions/cache@v2
128-
# with:
129-
# path: ~/.android/gradle/avd/dev29_aosp_x86_Nexus_9.*
130-
# key: nexus9api29
131-
132-
# - name: Run all tests pixel 2 api 27 ATD
133-
# working-directory: ${{ env.SAMPLE_PATH }}
134-
# run: ./gradlew pixel2api27atdMockDebugAndroidTest
135-
#
136-
# - name: Run all tests nexus 9 api 29 ATD
137-
# working-directory: ${{ env.SAMPLE_PATH }}
138-
# run: ./gradlew nexus9api29atdMockDebugAndroidTest
139-
14057
- name: Compile AndroidTests Mock
14158
run: ./gradlew compileMockDebugAndroidTestKotlin
14259

14360
- name: Run all tests pixel 2 api 30 ATD
14461
working-directory: .
14562
run: ./gradlew -Pandroid.sdk.channel=3 -Pandroid.experimental.androidTest.numManagedDeviceShards=1 -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect" pixel2api30atdMockDebugAndroidTest
14663

147-
- name: Run all tests pixel 2 api 30 ATD again
148-
working-directory: .
149-
run: ./gradlew -Pandroid.sdk.channel=3 -Pandroid.experimental.androidTest.numManagedDeviceShards=1 -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect" pixel2api30atdMockDebugAndroidTest
150-
151-
- name: Run all tests pixel 2 api 30 non-atd
64+
- name: Run all tests pixel 2 api 30 non-ATD
15265
working-directory: .
15366
run: ./gradlew -Pandroid.sdk.channel=3 -Pandroid.experimental.androidTest.numManagedDeviceShards=1 -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect" pixel2api30MockDebugAndroidTest
15467

155-
- name: Run all tests pixel 2 api 30 non-atd again
156-
working-directory: .
157-
run: ./gradlew -Pandroid.sdk.channel=3 -Pandroid.experimental.androidTest.numManagedDeviceShards=1 -Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect" pixel2api30MockDebugAndroidTest
158-
159-
- name: Run instrumentation tests API 30
160-
uses: reactivecircus/android-emulator-runner@v2
161-
with:
162-
api-level: 30
163-
arch: x86_64
164-
disable-animations: true
165-
script: ./gradlew app:cMDAT --stacktrace
166-
167-
- name: Run instrumentation tests API 30 ATD
168-
uses: reactivecircus/android-emulator-runner@v2
169-
with:
170-
api-level: 30
171-
arch: x86
172-
channel: canary
173-
target: aosp_atd
174-
disable-animations: true
175-
script: ./gradlew app:cMDAT --stacktrace
176-
17768
- name: Upload build reports
17869
if: always()
17970
uses: actions/upload-artifact@v2
@@ -187,10 +78,3 @@ jobs:
18778
with:
18879
name: outputs
18980
path: app/build/outputs/
190-
191-
# - name: Upload test reports
192-
# if: always()
193-
# uses: actions/upload-artifact@v2
194-
# with:
195-
# name: test-reports
196-
# path: ./app/build/reports/androidTests/managedDevice/

app/src/sharedTest/java/com/example/android/architecture/blueprints/todoapp/MainCoroutineRule.kt

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,35 @@
1616

1717
package com.example.android.architecture.blueprints.todoapp
1818

19-
import kotlinx.coroutines.CoroutineDispatcher
2019
import kotlinx.coroutines.Dispatchers
2120
import kotlinx.coroutines.ExperimentalCoroutinesApi
22-
import kotlinx.coroutines.test.TestCoroutineScope
21+
import kotlinx.coroutines.test.TestDispatcher
22+
import kotlinx.coroutines.test.UnconfinedTestDispatcher
2323
import kotlinx.coroutines.test.resetMain
2424
import kotlinx.coroutines.test.setMain
2525
import org.junit.rules.TestWatcher
2626
import org.junit.runner.Description
27-
import kotlin.coroutines.ContinuationInterceptor
2827

2928
/**
30-
* Sets the main coroutines dispatcher to a [TestCoroutineScope] for unit testing. A
31-
* [TestCoroutineScope] provides control over the execution of coroutines.
29+
* Sets the main coroutines dispatcher to a [TestDispatcher] for unit testing.
3230
*
3331
* Declare it as a JUnit Rule:
3432
*
3533
* ```
3634
* @get:Rule
37-
* var mainCoroutineRule = MainCoroutineRule()
35+
* val mainCoroutineRule = MainCoroutineRule()
3836
* ```
3937
*
40-
* Use it directly as a [TestCoroutineScope]:
41-
*
42-
* ```
43-
* mainCoroutineRule.pauseDispatcher()
44-
* ...
45-
* mainCoroutineRule.resumeDispatcher()
46-
* ...
47-
* mainCoroutineRule.runBlockingTest { }
48-
* ...
49-
*
50-
* ```
38+
* Then, use `runTest` to execute your tests.
5139
*/
5240
@ExperimentalCoroutinesApi
53-
class MainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
41+
class MainCoroutineRule(
42+
val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
43+
) : TestWatcher() {
5444

5545
override fun starting(description: Description?) {
5646
super.starting(description)
57-
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
47+
Dispatchers.setMain(testDispatcher)
5848
}
5949

6050
override fun finished(description: Description?) {

app/src/sharedTest/java/com/example/android/architecture/blueprints/todoapp/data/source/local/TasksDaoTest.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import androidx.test.filters.SmallTest
2424
import com.example.android.architecture.blueprints.todoapp.MainCoroutineRule
2525
import com.example.android.architecture.blueprints.todoapp.data.Task
2626
import kotlinx.coroutines.ExperimentalCoroutinesApi
27-
import kotlinx.coroutines.test.runBlockingTest
27+
import kotlinx.coroutines.test.runTest
2828
import org.hamcrest.CoreMatchers.`is`
2929
import org.hamcrest.CoreMatchers.notNullValue
3030
import org.hamcrest.MatcherAssert.assertThat
@@ -44,11 +44,11 @@ class TasksDaoTest {
4444
// Set the main coroutines dispatcher for unit testing.
4545
@ExperimentalCoroutinesApi
4646
@get:Rule
47-
var mainCoroutineRule = MainCoroutineRule()
47+
val mainCoroutineRule = MainCoroutineRule()
4848

4949
// Executes each task synchronously using Architecture Components.
5050
@get:Rule
51-
var instantExecutorRule = InstantTaskExecutorRule()
51+
val instantExecutorRule = InstantTaskExecutorRule()
5252

5353
@Before
5454
fun initDb() {
@@ -64,7 +64,7 @@ class TasksDaoTest {
6464
fun closeDb() = database.close()
6565

6666
@Test
67-
fun insertTaskAndGetById() = runBlockingTest {
67+
fun insertTaskAndGetById() = runTest {
6868
// GIVEN - insert a task
6969
val task = Task("title", "description")
7070
database.taskDao().insertTask(task)
@@ -81,7 +81,7 @@ class TasksDaoTest {
8181
}
8282

8383
@Test
84-
fun insertTaskReplacesOnConflict() = runBlockingTest {
84+
fun insertTaskReplacesOnConflict() = runTest {
8585
// Given that a task is inserted
8686
val task = Task("title", "description")
8787
database.taskDao().insertTask(task)
@@ -99,7 +99,7 @@ class TasksDaoTest {
9999
}
100100

101101
@Test
102-
fun insertTaskAndGetTasks() = runBlockingTest {
102+
fun insertTaskAndGetTasks() = runTest {
103103
// GIVEN - insert a task
104104
val task = Task("title", "description")
105105
database.taskDao().insertTask(task)
@@ -116,7 +116,7 @@ class TasksDaoTest {
116116
}
117117

118118
@Test
119-
fun updateTaskAndGetById() = runBlockingTest {
119+
fun updateTaskAndGetById() = runTest {
120120
// When inserting a task
121121
val originalTask = Task("title", "description")
122122
database.taskDao().insertTask(originalTask)
@@ -134,7 +134,7 @@ class TasksDaoTest {
134134
}
135135

136136
@Test
137-
fun updateCompletedAndGetById() = runBlockingTest {
137+
fun updateCompletedAndGetById() = runTest {
138138
// When inserting a task
139139
val task = Task("title", "description", true)
140140
database.taskDao().insertTask(task)
@@ -151,7 +151,7 @@ class TasksDaoTest {
151151
}
152152

153153
@Test
154-
fun deleteTaskByIdAndGettingTasks() = runBlockingTest {
154+
fun deleteTaskByIdAndGettingTasks() = runTest {
155155
// Given a task inserted
156156
val task = Task("title", "description")
157157
database.taskDao().insertTask(task)
@@ -165,7 +165,7 @@ class TasksDaoTest {
165165
}
166166

167167
@Test
168-
fun deleteTasksAndGettingTasks() = runBlockingTest {
168+
fun deleteTasksAndGettingTasks() = runTest {
169169
// Given a task inserted
170170
database.taskDao().insertTask(Task("title", "description"))
171171

@@ -178,7 +178,7 @@ class TasksDaoTest {
178178
}
179179

180180
@Test
181-
fun deleteCompletedTasksAndGettingTasks() = runBlockingTest {
181+
fun deleteCompletedTasksAndGettingTasks() = runTest {
182182
// Given a completed task inserted
183183
database.taskDao().insertTask(Task("completed", "task", true))
184184

app/src/sharedTest/java/com/example/android/architecture/blueprints/todoapp/data/source/local/TasksLocalDataSourceTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import com.example.android.architecture.blueprints.todoapp.data.source.TasksData
2727
import com.example.android.architecture.blueprints.todoapp.data.succeeded
2828
import kotlinx.coroutines.Dispatchers
2929
import kotlinx.coroutines.ExperimentalCoroutinesApi
30-
import kotlinx.coroutines.test.runBlockingTest
30+
import kotlinx.coroutines.test.runTest
3131
import org.hamcrest.CoreMatchers.`is`
3232
import org.hamcrest.MatcherAssert.assertThat
3333
import org.junit.After
@@ -50,11 +50,11 @@ class TasksLocalDataSourceTest {
5050
// Set the main coroutines dispatcher for unit testing.
5151
@ExperimentalCoroutinesApi
5252
@get:Rule
53-
var mainCoroutineRule = MainCoroutineRule()
53+
val mainCoroutineRule = MainCoroutineRule()
5454

5555
// Executes each task synchronously using Architecture Components.
5656
@get:Rule
57-
var instantExecutorRule = InstantTaskExecutorRule()
57+
val instantExecutorRule = InstantTaskExecutorRule()
5858

5959
@Before
6060
fun setup() {
@@ -75,7 +75,7 @@ class TasksLocalDataSourceTest {
7575
}
7676

7777
@Test
78-
fun saveTask_retrievesTask() = runBlockingTest {
78+
fun saveTask_retrievesTask() = runTest {
7979
// GIVEN - a new task saved in the database
8080
val newTask = Task("title", "description", true)
8181
localDataSource.saveTask(newTask)
@@ -92,7 +92,7 @@ class TasksLocalDataSourceTest {
9292
}
9393

9494
@Test
95-
fun completeTask_retrievedTaskIsComplete() = runBlockingTest {
95+
fun completeTask_retrievedTaskIsComplete() = runTest {
9696
// Given a new task in the persistent repository
9797
val newTask = Task("title")
9898
localDataSource.saveTask(newTask)
@@ -109,7 +109,7 @@ class TasksLocalDataSourceTest {
109109
}
110110

111111
@Test
112-
fun activateTask_retrievedTaskIsActive() = runBlockingTest {
112+
fun activateTask_retrievedTaskIsActive() = runTest {
113113
// Given a new completed task in the persistent repository
114114
val newTask = Task("Some title", "Some description", true)
115115
localDataSource.saveTask(newTask)
@@ -127,7 +127,7 @@ class TasksLocalDataSourceTest {
127127
}
128128

129129
@Test
130-
fun clearCompletedTask_taskNotRetrievable() = runBlockingTest {
130+
fun clearCompletedTask_taskNotRetrievable() = runTest {
131131
// Given 2 new completed tasks and 1 active task in the persistent repository
132132
val newTask1 = Task("title")
133133
val newTask2 = Task("title2")
@@ -153,7 +153,7 @@ class TasksLocalDataSourceTest {
153153
}
154154

155155
@Test
156-
fun deleteAllTasks_emptyListOfRetrievedTask() = runBlockingTest {
156+
fun deleteAllTasks_emptyListOfRetrievedTask() = runTest {
157157
// Given a new task in the persistent repository and a mocked callback
158158
val newTask = Task("title")
159159

@@ -168,7 +168,7 @@ class TasksLocalDataSourceTest {
168168
}
169169

170170
@Test
171-
fun getTasks_retrieveSavedTasks() = runBlockingTest {
171+
fun getTasks_retrieveSavedTasks() = runTest {
172172
// Given 2 new tasks in the persistent repository
173173
val newTask1 = Task("title")
174174
val newTask2 = Task("title")

0 commit comments

Comments
 (0)