1 /*
2  * Copyright 2024 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.security.state.provider
18 
19 import android.content.Context
20 import android.net.Uri
21 import androidx.test.core.app.ApplicationProvider
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.MediumTest
24 import org.junit.Assert.assertEquals
25 import org.junit.Assert.assertNotNull
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 
30 @MediumTest
31 @RunWith(AndroidJUnit4::class)
32 class UpdateInfoProviderTest {
33 
34     private val authority = "com.example.updateinfoprovider"
35     private val contentUri = Uri.parse("content://$authority/updateinfo")
36     private val context = ApplicationProvider.getApplicationContext<Context>()
37     private lateinit var provider: UpdateInfoProvider
38 
39     @Before
setupnull40     fun setup() {
41         // simulate the provider being instantiated by the system from the manifest
42         provider = UpdateInfoProvider()
43         provider.attachInfo(context, null)
44     }
45 
46     @Test
query_WithCorrectUri_ReturnsExpectedCursornull47     fun query_WithCorrectUri_ReturnsExpectedCursor() {
48         val cursor = provider.query(contentUri, null, null, null, null)
49 
50         assertNotNull(cursor)
51         assertEquals(0, cursor.count)
52         cursor.close()
53     }
54 
55     @Test(expected = IllegalArgumentException::class)
query_WithIncorrectUri_ThrowsExceptionnull56     fun query_WithIncorrectUri_ThrowsException() {
57         provider.query(Uri.parse("content://$authority/invalid"), null, null, null, null)
58     }
59 
60     @Test
getType_CorrectUri_ReturnsCorrectTypenull61     fun getType_CorrectUri_ReturnsCorrectType() {
62         val type = provider.getType(contentUri)
63 
64         assertEquals("vnd.android.cursor.dir/vnd.$authority.updateinfo", type)
65     }
66 }
67