Rahul Ravikumar | c90b900 | 2019-04-08 17:42:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.work.impl.background.gcm |
| 18 | |
| 19 | import android.content.Context |
| 20 | import android.util.Log |
| 21 | import androidx.arch.core.executor.ArchTaskExecutor |
| 22 | import androidx.arch.core.executor.TaskExecutor |
| 23 | import androidx.test.core.app.ApplicationProvider |
| 24 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
Rahul Ravikumar | e9ba8c0 | 2019-05-07 09:41:47 -0700 | [diff] [blame] | 25 | import androidx.test.filters.MediumTest |
Rahul Ravikumar | c90b900 | 2019-04-08 17:42:53 -0700 | [diff] [blame] | 26 | import androidx.work.Configuration |
| 27 | import androidx.work.impl.WorkManagerImpl |
Rahul Ravikumar | 5975b62c | 2019-07-24 12:24:17 -0700 | [diff] [blame^] | 28 | import androidx.work.impl.utils.SerialExecutor |
Rahul Ravikumar | c90b900 | 2019-04-08 17:42:53 -0700 | [diff] [blame] | 29 | import androidx.work.impl.utils.SynchronousExecutor |
| 30 | import com.google.android.gms.gcm.GcmNetworkManager |
| 31 | import com.google.android.gms.gcm.TaskParams |
| 32 | import org.junit.Before |
| 33 | import org.junit.Test |
| 34 | import org.junit.runner.RunWith |
| 35 | import org.mockito.Mockito.`when` |
| 36 | import org.mockito.Mockito.mock |
| 37 | import java.util.concurrent.Executor |
| 38 | |
| 39 | @RunWith(AndroidJUnit4::class) |
Rahul Ravikumar | e9ba8c0 | 2019-05-07 09:41:47 -0700 | [diff] [blame] | 40 | @MediumTest |
Rahul Ravikumar | c90b900 | 2019-04-08 17:42:53 -0700 | [diff] [blame] | 41 | class 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 Ravikumar | 5975b62c | 2019-07-24 12:24:17 -0700 | [diff] [blame^] | 67 | private val mSerialExecutor = SerialExecutor(mExecutor) |
| 68 | override fun postToMainThread(runnable: Runnable) { |
Rahul Ravikumar | c90b900 | 2019-04-08 17:42:53 -0700 | [diff] [blame] | 69 | mExecutor.execute(runnable) |
| 70 | } |
| 71 | |
| 72 | override fun getMainThreadExecutor(): Executor { |
| 73 | return mExecutor |
| 74 | } |
| 75 | |
Rahul Ravikumar | 5975b62c | 2019-07-24 12:24:17 -0700 | [diff] [blame^] | 76 | override fun executeOnBackgroundThread(runnable: Runnable) { |
| 77 | mSerialExecutor.execute(runnable) |
Rahul Ravikumar | c90b900 | 2019-04-08 17:42:53 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Rahul Ravikumar | 5975b62c | 2019-07-24 12:24:17 -0700 | [diff] [blame^] | 80 | override fun getBackgroundExecutor(): SerialExecutor { |
| 81 | return mSerialExecutor |
Rahul Ravikumar | c90b900 | 2019-04-08 17:42:53 -0700 | [diff] [blame] | 82 | } |
| 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 | } |