blob: f127abaac1670d97fb6b4b911ada0b0b9b086389 [file] [log] [blame]
Rahul Ravikumarc90b9002019-04-08 17:42:53 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.work.impl.background.gcm
18
19import android.content.Context
20import android.util.Log
21import androidx.arch.core.executor.ArchTaskExecutor
22import androidx.arch.core.executor.TaskExecutor
23import androidx.test.core.app.ApplicationProvider
24import androidx.test.ext.junit.runners.AndroidJUnit4
Rahul Ravikumare9ba8c02019-05-07 09:41:47 -070025import androidx.test.filters.MediumTest
Rahul Ravikumarc90b9002019-04-08 17:42:53 -070026import androidx.work.Configuration
27import androidx.work.impl.WorkManagerImpl
Rahul Ravikumar5975b62c2019-07-24 12:24:17 -070028import androidx.work.impl.utils.SerialExecutor
Rahul Ravikumarc90b9002019-04-08 17:42:53 -070029import androidx.work.impl.utils.SynchronousExecutor
30import com.google.android.gms.gcm.GcmNetworkManager
31import com.google.android.gms.gcm.TaskParams
32import org.junit.Before
33import org.junit.Test
34import org.junit.runner.RunWith
35import org.mockito.Mockito.`when`
36import org.mockito.Mockito.mock
37import java.util.concurrent.Executor
38
39@RunWith(AndroidJUnit4::class)
Rahul Ravikumare9ba8c02019-05-07 09:41:47 -070040@MediumTest
Rahul Ravikumarc90b9002019-04-08 17:42:53 -070041class WorkManagerGcmDispatcherTest {
42 lateinit var mContext: Context
43 lateinit var mExecutor: Executor
44 lateinit var mWorkManager: WorkManagerImpl
45 lateinit var mDispatcher: WorkManagerGcmDispatcher
46
47 @Before
48 fun setUp() {
49 mContext = ApplicationProvider.getApplicationContext()
50 mExecutor = SynchronousExecutor()
51 ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() {
52 override fun executeOnDiskIO(runnable: Runnable) {
53 runnable.run()
54 }
55
56 override fun isMainThread(): Boolean {
57 return true
58 }
59
60 override fun postToMainThread(runnable: Runnable) {
61 runnable.run()
62 }
63 })
64
65 val workTaskExecutor: androidx.work.impl.utils.taskexecutor.TaskExecutor =
66 object : androidx.work.impl.utils.taskexecutor.TaskExecutor {
Rahul Ravikumar5975b62c2019-07-24 12:24:17 -070067 private val mSerialExecutor = SerialExecutor(mExecutor)
68 override fun postToMainThread(runnable: Runnable) {
Rahul Ravikumarc90b9002019-04-08 17:42:53 -070069 mExecutor.execute(runnable)
70 }
71
72 override fun getMainThreadExecutor(): Executor {
73 return mExecutor
74 }
75
Rahul Ravikumar5975b62c2019-07-24 12:24:17 -070076 override fun executeOnBackgroundThread(runnable: Runnable) {
77 mSerialExecutor.execute(runnable)
Rahul Ravikumarc90b9002019-04-08 17:42:53 -070078 }
79
Rahul Ravikumar5975b62c2019-07-24 12:24:17 -070080 override fun getBackgroundExecutor(): SerialExecutor {
81 return mSerialExecutor
Rahul Ravikumarc90b9002019-04-08 17:42:53 -070082 }
83 }
84
85 val configuration = Configuration.Builder()
86 .setMinimumLoggingLevel(Log.DEBUG)
87 .setExecutor(mExecutor)
88 .build()
89
90 mWorkManager = WorkManagerImpl(mContext, configuration, workTaskExecutor, true)
91 WorkManagerImpl.setDelegate(mWorkManager)
92 mDispatcher = WorkManagerGcmDispatcher(mContext)
93 }
94
95 @Test
96 fun testNullWorkSpecId() {
97 val taskParams = mock(TaskParams::class.java)
98 `when`(taskParams.tag).thenReturn(null)
99 val result = mDispatcher.onRunTask(taskParams)
100 assert(result == GcmNetworkManager.RESULT_FAILURE)
101 }
102
103 @Test
104 fun testWorkSpecIdThatDoesNotExit() {
105 val taskParams = mock(TaskParams::class.java)
106 `when`(taskParams.tag).thenReturn("InvalidWorkSpecId")
107 val result = mDispatcher.onRunTask(taskParams)
108 assert(result == GcmNetworkManager.RESULT_FAILURE)
109 }
110}