1 /* 2 * Copyright (C) 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 com.android.launcher3.dagger 18 19 import android.content.Context 20 import android.content.ContextWrapper 21 import android.view.ContextThemeWrapper 22 import androidx.test.core.app.ApplicationProvider 23 import androidx.test.ext.junit.runners.AndroidJUnit4 24 import com.android.launcher3.R 25 import com.android.launcher3.util.LauncherModelHelper.SandboxModelContext 26 import com.android.launcher3.util.LauncherModelHelper.TEST_PACKAGE 27 import org.junit.Assert.assertNotNull 28 import org.junit.Assert.assertNotSame 29 import org.junit.Assert.assertSame 30 import org.junit.Test 31 import org.junit.runner.RunWith 32 33 @RunWith(AndroidJUnit4::class) 34 class LauncherComponentProviderTest { 35 36 val app: Context = ApplicationProvider.getApplicationContext() 37 38 @Test returns same component as Launcher applicationnull39 fun `returns same component as Launcher application`() { 40 val c = SandboxModelContext() 41 assertSame(c.appComponent, LauncherComponentProvider.get(c)) 42 assertNotSame(LauncherComponentProvider.get(c), LauncherComponentProvider.get(app)) 43 } 44 45 @Test returns same component for isolated contextnull46 fun `returns same component for isolated context`() { 47 val c = IsolatedContext() 48 49 // Same component is returned for multiple calls, irrespective of the wrappers 50 assertNotNull(LauncherComponentProvider.get(c)) 51 assertSame( 52 LauncherComponentProvider.get(c), 53 LauncherComponentProvider.get(ContextThemeWrapper(c, R.style.LauncherTheme)), 54 ) 55 56 // Different than main application 57 assertNotSame(LauncherComponentProvider.get(c), LauncherComponentProvider.get(app)) 58 } 59 60 @Test different components for different isolated contextnull61 fun `different components for different isolated context`() { 62 val c1 = IsolatedContext() 63 val c2 = IsolatedContext() 64 65 assertNotNull(LauncherComponentProvider.get(c1)) 66 assertNotNull(LauncherComponentProvider.get(c2)) 67 assertNotSame(LauncherComponentProvider.get(c1), LauncherComponentProvider.get(c2)) 68 } 69 70 inner class IsolatedContext : ContextWrapper(app.createPackageContext(TEST_PACKAGE, 0)) { 71 getApplicationContextnull72 override fun getApplicationContext(): Context = this 73 } 74 } 75