-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use LensFacing instead of flipCamera (#133)
* Add LensFacing to model * Remove unused CameraUseCase data classes Use the classes in the model instead * Change CameraUseCase.flipCamera() to setLensFacing() * Use of setLensFacing rather than flipCamera Switches all usage to setting LensFacing directly rather than relying on booleans. * Add switch camera tests This will switch cameras using three methods: 1. The flip camera button on the preview screen 2. Double tapping the screen to flip cameras 3. Flipping cameras with the quick settings button * Fix unit test failures * Remove unused import and unnecessary changes * Fix BackgroundDeviceTest Fixes tests in BackgroundDeviceTest that use the flip camera button * Fix comments in LocalSettingsRepositoryInstrumentedTest
- Loading branch information
Showing
33 changed files
with
485 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
app/src/androidTest/java/com/google/jetpackcamera/SwitchCameraTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* Copyright (C) 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 | ||
* | ||
* http://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.jetpackcamera | ||
|
||
import androidx.compose.ui.semantics.SemanticsProperties | ||
import androidx.compose.ui.test.doubleClick | ||
import androidx.compose.ui.test.isDisplayed | ||
import androidx.compose.ui.test.isEnabled | ||
import androidx.compose.ui.test.junit4.ComposeTestRule | ||
import androidx.compose.ui.test.junit4.createEmptyComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.compose.ui.test.performTouchInput | ||
import androidx.test.core.app.ActivityScenario | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.rule.GrantPermissionRule | ||
import com.google.common.truth.Truth.assertThat | ||
import com.google.jetpackcamera.feature.preview.ui.FLIP_CAMERA_BUTTON | ||
import com.google.jetpackcamera.feature.preview.ui.PREVIEW_DISPLAY | ||
import com.google.jetpackcamera.feature.preview.ui.QUICK_SETTINGS_BUTTON | ||
import com.google.jetpackcamera.feature.quicksettings.ui.QUICK_SETTINGS_FLIP_CAMERA_BUTTON | ||
import com.google.jetpackcamera.quicksettings.R.string.quick_settings_back_camera_description | ||
import com.google.jetpackcamera.quicksettings.R.string.quick_settings_dropdown_closed_description | ||
import com.google.jetpackcamera.quicksettings.R.string.quick_settings_dropdown_open_description | ||
import com.google.jetpackcamera.quicksettings.R.string.quick_settings_front_camera_description | ||
import com.google.jetpackcamera.settings.model.LensFacing | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class SwitchCameraTest { | ||
@get:Rule | ||
val cameraPermissionRule: GrantPermissionRule = | ||
GrantPermissionRule.grant(android.Manifest.permission.CAMERA) | ||
|
||
@get:Rule | ||
val composeTestRule = createEmptyComposeRule() | ||
|
||
@Test | ||
fun canFlipCamera_fromPreviewScreenButton() = runFlipCameraTest(composeTestRule) { | ||
val lensFacingStates = mutableListOf<LensFacing>() | ||
// Get initial lens facing | ||
val initialLensFacing = composeTestRule.getCurrentLensFacing() | ||
lensFacingStates.add(initialLensFacing) | ||
|
||
// Press the flip camera button | ||
composeTestRule.onNodeWithTag(FLIP_CAMERA_BUTTON).performClick() | ||
|
||
// Get lens facing after first flip | ||
lensFacingStates.add(composeTestRule.getCurrentLensFacing()) | ||
|
||
// Press the flip camera button again | ||
composeTestRule.onNodeWithTag(FLIP_CAMERA_BUTTON).performClick() | ||
|
||
// Get lens facing after second flip | ||
lensFacingStates.add(composeTestRule.getCurrentLensFacing()) | ||
|
||
assertThat(lensFacingStates).containsExactly( | ||
initialLensFacing, | ||
initialLensFacing.flip(), | ||
initialLensFacing.flip().flip() | ||
).inOrder() | ||
} | ||
|
||
@Test | ||
fun canFlipCamera_fromPreviewScreenDoubleTap() = runFlipCameraTest(composeTestRule) { | ||
val lensFacingStates = mutableListOf<LensFacing>() | ||
// Get initial lens facing | ||
val initialLensFacing = composeTestRule.getCurrentLensFacing() | ||
lensFacingStates.add(initialLensFacing) | ||
|
||
// Double click display to flip camera | ||
composeTestRule.onNodeWithTag(PREVIEW_DISPLAY) | ||
.performTouchInput { doubleClick() } | ||
|
||
// Get lens facing after first flip | ||
lensFacingStates.add(composeTestRule.getCurrentLensFacing()) | ||
|
||
// Double click display to flip camera again | ||
composeTestRule.onNodeWithTag(PREVIEW_DISPLAY) | ||
.performTouchInput { doubleClick() } | ||
|
||
// Get lens facing after second flip | ||
lensFacingStates.add(composeTestRule.getCurrentLensFacing()) | ||
|
||
assertThat(lensFacingStates).containsExactly( | ||
initialLensFacing, | ||
initialLensFacing.flip(), | ||
initialLensFacing.flip().flip() | ||
).inOrder() | ||
} | ||
|
||
@Test | ||
fun canFlipCamera_fromQuickSettings() = runFlipCameraTest(composeTestRule) { | ||
// Navigate to quick settings | ||
composeTestRule.onNodeWithTag(QUICK_SETTINGS_BUTTON) | ||
.assertExists() | ||
.performClick() | ||
|
||
val lensFacingStates = mutableListOf<LensFacing>() | ||
// Get initial lens facing | ||
val initialLensFacing = composeTestRule.getCurrentLensFacing() | ||
lensFacingStates.add(initialLensFacing) | ||
|
||
// Double click quick settings button to flip camera | ||
composeTestRule.onNodeWithTag(QUICK_SETTINGS_FLIP_CAMERA_BUTTON).performClick() | ||
|
||
// Get lens facing after first flip | ||
lensFacingStates.add(composeTestRule.getCurrentLensFacing()) | ||
|
||
// Double click quick settings button to flip camera again | ||
composeTestRule.onNodeWithTag(QUICK_SETTINGS_FLIP_CAMERA_BUTTON).performClick() | ||
|
||
// Get lens facing after second flip | ||
lensFacingStates.add(composeTestRule.getCurrentLensFacing()) | ||
|
||
assertThat(lensFacingStates).containsExactly( | ||
initialLensFacing, | ||
initialLensFacing.flip(), | ||
initialLensFacing.flip().flip() | ||
).inOrder() | ||
} | ||
} | ||
|
||
inline fun runFlipCameraTest( | ||
composeTestRule: ComposeTestRule, | ||
crossinline block: ActivityScenario<MainActivity>.() -> Unit | ||
) = runScenarioTest { | ||
// Wait for the preview display to be visible | ||
composeTestRule.waitUntil { | ||
composeTestRule.onNodeWithTag(PREVIEW_DISPLAY).isDisplayed() | ||
} | ||
|
||
// If flipping the camera is available, flip it. Otherwise skip test. | ||
composeTestRule.onNodeWithTag(FLIP_CAMERA_BUTTON).assume(isEnabled()) { | ||
"Device does not have multiple cameras to flip between." | ||
} | ||
|
||
block() | ||
} | ||
|
||
private fun ComposeTestRule.getCurrentLensFacing(): LensFacing { | ||
var needReturnFromQuickSettings = false | ||
onNodeWithContentDescription(quick_settings_dropdown_closed_description).apply { | ||
if (isDisplayed()) { | ||
performClick() | ||
needReturnFromQuickSettings = true | ||
} | ||
} | ||
|
||
onNodeWithContentDescription(quick_settings_dropdown_open_description).assertExists( | ||
"LensFacing can only be retrieved from PreviewScreen or QuickSettings screen" | ||
) | ||
|
||
try { | ||
return onNodeWithTag(QUICK_SETTINGS_FLIP_CAMERA_BUTTON).fetchSemanticsNode( | ||
"Flip camera button is not visible when expected." | ||
).let { node -> | ||
node.config[SemanticsProperties.ContentDescription].any { description -> | ||
when (description) { | ||
getResString(quick_settings_front_camera_description) -> | ||
return@let LensFacing.FRONT | ||
|
||
getResString(quick_settings_back_camera_description) -> | ||
return@let LensFacing.BACK | ||
|
||
else -> false | ||
} | ||
} | ||
throw AssertionError("Unable to determine lens facing from quick settings") | ||
} | ||
} finally { | ||
if (needReturnFromQuickSettings) { | ||
onNodeWithContentDescription(quick_settings_dropdown_open_description) | ||
.assertExists() | ||
.performClick() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.