• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2023 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 package com.google.jetpackcamera
17 
18 import android.app.Activity
19 import androidx.compose.ui.semantics.SemanticsProperties
20 import androidx.compose.ui.test.isDisplayed
21 import androidx.compose.ui.test.junit4.ComposeTestRule
22 import androidx.compose.ui.test.onNodeWithTag
23 import androidx.compose.ui.test.performClick
24 import androidx.test.core.app.ActivityScenario
25 import com.google.jetpackcamera.feature.preview.R
26 import com.google.jetpackcamera.feature.preview.quicksettings.ui.QUICK_SETTINGS_FLIP_CAMERA_BUTTON
27 import com.google.jetpackcamera.settings.model.LensFacing
28 
29 const val APP_START_TIMEOUT_MILLIS = 10_000L
30 const val IMAGE_CAPTURE_TIMEOUT_MILLIS = 5_000L
31 
32 inline fun <reified T : Activity> runScenarioTest(
33     crossinline block: ActivityScenario<T>.() -> Unit
34 ) {
35     ActivityScenario.launch(T::class.java).use { scenario ->
36         scenario.apply(block)
37     }
38 }
39 
40 context(ActivityScenario<MainActivity>)
ComposeTestRulenull41 fun ComposeTestRule.getCurrentLensFacing(): LensFacing {
42     var needReturnFromQuickSettings = false
43     onNodeWithContentDescription(R.string.quick_settings_dropdown_closed_description).apply {
44         if (isDisplayed()) {
45             performClick()
46             needReturnFromQuickSettings = true
47         }
48     }
49 
50     onNodeWithContentDescription(R.string.quick_settings_dropdown_open_description).assertExists(
51         "LensFacing can only be retrieved from PreviewScreen or QuickSettings screen"
52     )
53 
54     try {
55         return onNodeWithTag(QUICK_SETTINGS_FLIP_CAMERA_BUTTON).fetchSemanticsNode(
56             "Flip camera button is not visible when expected."
57         ).let { node ->
58             node.config[SemanticsProperties.ContentDescription].any { description ->
59                 when (description) {
60                     getResString(R.string.quick_settings_front_camera_description) ->
61                         return@let LensFacing.FRONT
62 
63                     getResString(R.string.quick_settings_back_camera_description) ->
64                         return@let LensFacing.BACK
65 
66                     else -> false
67                 }
68             }
69             throw AssertionError("Unable to determine lens facing from quick settings")
70         }
71     } finally {
72         if (needReturnFromQuickSettings) {
73             onNodeWithContentDescription(R.string.quick_settings_dropdown_open_description)
74                 .assertExists()
75                 .performClick()
76         }
77     }
78 }
79