1 /*
2  * Copyright (C) 2020 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 com.android.systemui.controls.controller
18 
19 import android.app.job.JobParameters
20 import android.content.Context
21 import android.os.PersistableBundle
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.controls.controller.AuxiliaryPersistenceWrapper.DeletionJobService.Companion.USER
26 import com.android.systemui.util.mockito.whenever
27 import java.util.concurrent.TimeUnit
28 import org.junit.Assert.assertEquals
29 import org.junit.Assert.assertFalse
30 import org.junit.Assert.assertTrue
31 import org.junit.Before
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 import org.mockito.Mock
35 import org.mockito.Mockito.mock
36 import org.mockito.Mockito.verify
37 import org.mockito.MockitoAnnotations
38 
39 @SmallTest
40 @RunWith(AndroidJUnit4::class)
41 class DeletionJobServiceTest : SysuiTestCase() {
42 
43     @Mock private lateinit var context: Context
44 
45     private lateinit var service: AuxiliaryPersistenceWrapper.DeletionJobService
46 
47     @Before
setUpnull48     fun setUp() {
49         MockitoAnnotations.initMocks(this)
50 
51         service = AuxiliaryPersistenceWrapper.DeletionJobService()
52         service.attachContext(context)
53     }
54 
55     @Test
testOnStartJobnull56     fun testOnStartJob() {
57         val bundle = PersistableBundle().also { it.putInt(USER, 0) }
58         val params = mock(JobParameters::class.java)
59         whenever(params.getExtras()).thenReturn(bundle)
60 
61         // false means job is terminated
62         assertFalse(service.onStartJob(mock(JobParameters::class.java)))
63         verify(context).deleteFile(AuxiliaryPersistenceWrapper.AUXILIARY_FILE_NAME)
64     }
65 
66     @Test
testOnStopJobnull67     fun testOnStopJob() {
68         // true means run after backoff
69         assertTrue(service.onStopJob(mock(JobParameters::class.java)))
70     }
71 
72     @Test
testJobHasRightParametersnull73     fun testJobHasRightParameters() {
74         val userId = 10
75         whenever(context.userId).thenReturn(userId)
76         whenever(context.packageName).thenReturn(mContext.packageName)
77 
78         val jobInfo =
79             AuxiliaryPersistenceWrapper.DeletionJobService.getJobForContext(context, userId)
80         assertEquals(
81             AuxiliaryPersistenceWrapper.DeletionJobService.DELETE_FILE_JOB_ID + userId,
82             jobInfo.id
83         )
84         assertTrue(jobInfo.isPersisted)
85         assertEquals(userId, jobInfo.getExtras().getInt(USER))
86         assertEquals(TimeUnit.DAYS.toMillis(7), jobInfo.minLatencyMillis)
87     }
88 }
89