1 /*
<lambda>null2  * 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
18 
19 import android.content.Context
20 import androidx.room.testing.MigrationTestHelper
21 import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
22 import androidx.test.core.app.ApplicationProvider
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.LargeTest
25 import androidx.test.filters.SdkSuppress
26 import androidx.test.platform.app.InstrumentationRegistry
27 import androidx.work.impl.WorkDatabase
28 import androidx.work.impl.WorkDatabasePathHelper
29 import androidx.work.impl.WorkDatabaseVersions.VERSION_9
30 import java.io.File
31 import org.hamcrest.CoreMatchers.`is`
32 import org.hamcrest.MatcherAssert.assertThat
33 import org.junit.Before
34 import org.junit.Rule
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 
38 @RunWith(AndroidJUnit4::class)
39 @LargeTest
40 class WorkDatabasePathHelperTest {
41     @get:Rule
42     val migrationTestHelper =
43         MigrationTestHelper(
44             InstrumentationRegistry.getInstrumentation(),
45             WorkDatabase::class.java,
46             emptyList(),
47             FrameworkSQLiteOpenHelperFactory()
48         )
49 
50     private lateinit var context: Context
51 
52     @Before
53     fun setUp() {
54         context = ApplicationProvider.getApplicationContext()
55     }
56 
57     @Test
58     @SdkSuppress(minSdkVersion = 23)
59     fun testMigration_toNoBackupDirectory() {
60         // Create a database
61         migrationTestHelper.createDatabase(
62             WorkDatabasePathHelper.getDefaultDatabasePath(context).path,
63             VERSION_9
64         )
65         val paths = WorkDatabasePathHelper.migrationPaths(context)
66 
67         val asserts: Map<File, Boolean> =
68             paths.map { (source, _) -> Pair(source, source.exists()) }.toMap()
69 
70         // Migrate
71         WorkDatabasePathHelper.migrateDatabase(context)
72 
73         // Verify paths
74         paths.forEach { (source, destination) ->
75             val exists = asserts[source] ?: false
76             if (exists) {
77                 assertThat(destination.exists(), `is`(true))
78             }
79         }
80     }
81 }
82