• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util
18 
19 import android.hardware.display.DisplayManager
20 import android.view.Display
21 import android.view.Display.DEFAULT_DISPLAY
22 import android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
23 import com.google.common.truth.Truth.assertThat
24 import org.junit.Rule
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 
28 @RunWith(LauncherMultivalentJUnit::class)
29 class SandboxApplicationTest {
30     @get:Rule val app = SandboxApplication()
31 
32     private val display: Display
33         get() {
34             return checkNotNull(app.getSystemService(DisplayManager::class.java))
35                 .getDisplay(DEFAULT_DISPLAY)
36         }
37 
38     @Test
testCreateDisplayContext_isSandboxednull39     fun testCreateDisplayContext_isSandboxed() {
40         val displayContext = app.createDisplayContext(display)
41         assertThat(displayContext.applicationContext).isEqualTo(app)
42     }
43 
44     @Test
testCreateWindowContext_fromSandboxedDisplayContext_isSandboxednull45     fun testCreateWindowContext_fromSandboxedDisplayContext_isSandboxed() {
46         val displayContext = app.createDisplayContext(display)
47         val nestedContext = displayContext.createWindowContext(TYPE_APPLICATION_OVERLAY, null)
48         assertThat(nestedContext.applicationContext).isEqualTo(app)
49     }
50 
51     @Test(expected = IllegalStateException::class)
testGetApplicationContext_beforeManualInit_throwsExceptionnull52     fun testGetApplicationContext_beforeManualInit_throwsException() {
53         val manualApp = SandboxApplication()
54         assertThat(manualApp.applicationContext).isEqualTo(manualApp)
55     }
56 
57     @Test
testGetApplicationContext_afterManualInit_isApplicationnull58     fun testGetApplicationContext_afterManualInit_isApplication() {
59         SandboxApplication().run {
60             init()
61             assertThat(applicationContext).isEqualTo(this)
62             onDestroy()
63         }
64     }
65 }
66