Skip to content

Commit

Permalink
Add DeviceConfigurationOverride for insets to allow screenshot testin…
Browse files Browse the repository at this point in the history
…g insets

Change-Id: Idc781bf23021c1dba0d992bda0a3233e38e7c436
  • Loading branch information
alexvanyo committed Jun 10, 2024
1 parent 85129e4 commit 171faf9
Show file tree
Hide file tree
Showing 19 changed files with 422 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.samples.apps.nowinandroid.ui

import android.view.WindowInsets
import android.widget.FrameLayout
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.test.DeviceConfigurationOverride
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.children

/**
* A [DeviceConfigurationOverride] that allows overriding the [windowInsets] available
* to the content under test.
*/
fun DeviceConfigurationOverride.Companion.WindowInsets(
windowInsets: WindowInsetsCompat
): DeviceConfigurationOverride = DeviceConfigurationOverride { contentUnderTest ->
val currentContentUnderTest by rememberUpdatedState(contentUnderTest)
val currentWindowInsets by rememberUpdatedState(windowInsets)
AndroidView(
factory = { context ->
object : FrameLayout(context) {
override fun dispatchApplyWindowInsets(insets: WindowInsets): WindowInsets {
children.forEach {
it.dispatchApplyWindowInsets(currentWindowInsets.toWindowInsets())
}
return WindowInsetsCompat.CONSUMED.toWindowInsets()!!
}

/**
* Deprecated, but intercept the `requestApplyInsets` call via the deprecated
* method.
*/
@Deprecated("Deprecated in Java")
override fun requestFitSystemWindows() {
dispatchApplyWindowInsets(currentWindowInsets.toWindowInsets()!!)
}
}.apply {
addView(
ComposeView(context).apply {
setContent {
currentContentUnderTest()
}
}
)
}
},
)
}
Loading

0 comments on commit 171faf9

Please sign in to comment.