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
18 
19 import android.content.ContextWrapper
20 import androidx.test.core.app.ApplicationProvider
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import androidx.test.filters.SdkSuppress
23 import androidx.test.filters.SmallTest
24 import androidx.work.Configuration
25 import androidx.work.impl.utils.SynchronousExecutor
26 import androidx.work.impl.utils.taskexecutor.InstantWorkTaskExecutor
27 import org.junit.Assert.assertNotNull
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 
31 @RunWith(AndroidJUnit4::class)
32 class WorkManagerInitializationTest {
33     private val executor = SynchronousExecutor()
34     private val configuration =
35         Configuration.Builder().setExecutor(executor).setTaskExecutor(executor).build()
36     private val taskExecutor = InstantWorkTaskExecutor()
37 
38     @Test(expected = IllegalStateException::class)
39     @SmallTest
40     @SdkSuppress(minSdkVersion = 24)
directBootTestnull41     fun directBootTest() {
42         val context = DeviceProtectedStoreContext(true)
43         TestWorkManagerImpl(context, configuration, taskExecutor)
44     }
45 
46     @Test
47     @SmallTest
48     @SdkSuppress(minSdkVersion = 24)
credentialBackedStorageTestnull49     fun credentialBackedStorageTest() {
50         val context = DeviceProtectedStoreContext(false)
51         val workManager = TestWorkManagerImpl(context, configuration, taskExecutor)
52         assertNotNull(workManager)
53     }
54 }
55 
56 private class DeviceProtectedStoreContext(val deviceProtectedStorage: Boolean) :
57     ContextWrapper(ApplicationProvider.getApplicationContext()) {
isDeviceProtectedStoragenull58     override fun isDeviceProtectedStorage() = deviceProtectedStorage
59 
60     override fun getApplicationContext() = this
61 }
62