/* * Copyright (C) 2025 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.android.privatespace import android.content.Context import android.multiuser.Flags import android.os.statsd.privatespace.FileSizeBucket import android.os.statsd.privatespace.NotificationEventType import android.os.statsd.privatespace.OperationType import android.os.statsd.privatespace.TransferResult import android.platform.test.annotations.RequiresFlagsEnabled import android.platform.test.flag.junit.CheckFlagsRule import android.platform.test.flag.junit.DeviceFlagsValueProvider import android.util.StatsEvent import android.util.StatsEventTestUtils import android.util.StatsLog import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import com.android.dx.mockito.inline.extended.ExtendedMockito import com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession import com.google.common.truth.Truth.assertThat import com.google.protobuf.ExtensionRegistryLite import java.util.stream.Collectors.toList import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.mockito.Captor import org.mockito.MockitoSession import org.mockito.kotlin.argumentCaptor import org.mockito.kotlin.atLeastOnce import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.whenever @RequiresFlagsEnabled(Flags.FLAG_ENABLE_MOVING_CONTENT_INTO_PRIVATE_SPACE) @RunWith(AndroidJUnit4::class) class PrivateSpaceActivityScreenTest { @get:Rule val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule() @get:Rule val composeTestRule = createComposeRule() private lateinit var mockViewModel: PrivateSpaceViewModel private lateinit var mockOnOpenDocumentsPicker: () -> Unit private lateinit var mockOnFinished: () -> Unit private lateinit var context: Context @Captor val statsEventCaptor = argumentCaptor() private val registry = ExtensionRegistryLite.newInstance() private lateinit var mockitoSession: MockitoSession @Before fun setUp() { context = ApplicationProvider.getApplicationContext() mockViewModel = mock() mockOnOpenDocumentsPicker = mock() mockOnFinished = mock() mockitoSession = mockitoSession().mockStatic(StatsLog::class.java).startMocking() registry.add(PrivatespaceExtensionAtoms.privateSpaceMoveContentEvent) } @After fun tearDown() { mockitoSession.finishMocking() } @Test fun uiStateStarted_showsNothing() { setScreenContent(PrivateSpaceUiState.STARTED) // Check that known text from dialogs does NOT exist. composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_title)) .assertDoesNotExist() composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .assertDoesNotExist() } @Test fun uiStateCheckingTransferStatus_showsCircularProgressIndicator() { setScreenContent(PrivateSpaceUiState.CHECKING_PROGRESS_INDICATOR) composeTestRule .onNodeWithTag(testTag = CIRCULAR_PROGRESS_INDICATOR_TEST_TAG) .assertIsDisplayed() } @Test fun uiShowDocumentPicker_callsOnOpenDocumentPicker() { setScreenContent(PrivateSpaceUiState.SHOW_DOCUMENT_PICKER) verify(mockOnOpenDocumentsPicker).invoke() } @Test fun uiStateShowMoveFilesDialog_displaysDialogAndButtons() { setScreenContent(PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_title)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_summary)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_move)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_copy)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_cancel)) .assertIsDisplayed() } @Test fun showMoveFilesDialog_clickMoveButton_callsViewModelMoveFiles() { setScreenContent(PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_move)) .performClick() verify(mockViewModel).moveFiles(context) } @Test fun showMoveFilesDialog_clickMoveButton_logsPrivateSpaceMoveContentEvent() { setScreenContent(PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_move)) .performClick() composeTestRule.waitForIdle() ExtendedMockito.verify({ StatsLog.write(statsEventCaptor.capture()) }, atLeastOnce()) val atomList = statsEventCaptor.allValues .stream() .map { statsEvent -> StatsEventTestUtils.convertToAtom(statsEvent, registry) } .filter { e -> e.hasExtension(PrivatespaceExtensionAtoms.privateSpaceMoveContentEvent) } .collect(toList()) assertThat(atomList).hasSize(1) val atom = atomList[0] val event = atom.getExtension(PrivatespaceExtensionAtoms.privateSpaceMoveContentEvent) verifyEventFields(event, OperationType.OPERATION_MOVE) } @Test fun showMoveFilesDialog_clickCopyButton_callsViewModelCopyFiles() { setScreenContent(PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_copy)) .performClick() verify(mockViewModel).copyFiles(context) } @Test fun showMoveFilesDialog_clickCopyButton_logsPrivateSpaceMoveContentEvent() { setScreenContent(PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_copy)) .performClick() composeTestRule.waitForIdle() ExtendedMockito.verify({ StatsLog.write(statsEventCaptor.capture()) }, atLeastOnce()) val atomList = statsEventCaptor.allValues .stream() .map { statsEvent -> StatsEventTestUtils.convertToAtom(statsEvent, registry) } .filter { e -> e.hasExtension(PrivatespaceExtensionAtoms.privateSpaceMoveContentEvent) } .collect(toList()) assertThat(atomList).hasSize(1) val atom = atomList[0] val event = atom.getExtension(PrivatespaceExtensionAtoms.privateSpaceMoveContentEvent) verifyEventFields(event, OperationType.OPERATION_COPY) } @Test fun showMoveFilesDialog_clickCancelButton_callsViewModelFinishFlow() { setScreenContent(PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_cancel)) .performClick() verify(mockViewModel).finishFlow() } @Test fun showMoveFilesDialog_clickCancelButton_logsPrivateSpaceMoveContentEvent() { setScreenContent(PrivateSpaceUiState.SHOW_MOVE_FILES_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.move_files_dialog_button_label_cancel)) .performClick() composeTestRule.waitForIdle() ExtendedMockito.verify({ StatsLog.write(statsEventCaptor.capture()) }, atLeastOnce()) val atomList = statsEventCaptor.allValues .stream() .map { statsEvent -> StatsEventTestUtils.convertToAtom(statsEvent, registry) } .filter { e -> e.hasExtension(PrivatespaceExtensionAtoms.privateSpaceMoveContentEvent) } .collect(toList()) assertThat(atomList).hasSize(1) val atom = atomList[0] val event = atom.getExtension(PrivatespaceExtensionAtoms.privateSpaceMoveContentEvent) verifyEventFields(event, OperationType.OPERATION_CANCEL_BY_USER) } @Test fun uiStateShowNotEnoughSpaceDialog_displaysErrorDialog() { setScreenContent(PrivateSpaceUiState.SHOW_NOT_ENOUGH_SPACE_DIALOG) composeTestRule .onNodeWithText( context.getString(R.string.filetransfer_notification_transfer_error_title) ) .assertIsDisplayed() composeTestRule .onNodeWithText( context.getString( R.string.filetransfer_notification_insufficient_storage_error_message ) ) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .assertIsDisplayed() } @Test fun uiStateShowNotEnoughSpaceDialog_clickDismissButton_callsViewModelFinishFlow() { setScreenContent(PrivateSpaceUiState.SHOW_NOT_ENOUGH_SPACE_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .performClick() verify(mockViewModel).finishFlow() } @Test fun uiStateShowAboveFileSizeLimitsDialog_displaysErrorDialog() { setScreenContent(PrivateSpaceUiState.SHOW_ABOVE_FILE_SIZE_LIMITS_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_large_files_size_error_title)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_large_files_size_error_message)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .assertIsDisplayed() } @Test fun uiStateShowAboveFileSizeLimitsDialog_clickDismissButton_callsViewModelFinishFlow() { setScreenContent(PrivateSpaceUiState.SHOW_ABOVE_FILE_SIZE_LIMITS_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .performClick() verify(mockViewModel).finishFlow() } @Test fun uiStateShowTooManyFilesSelectedDialog_displaysErrorDialog() { setScreenContent(PrivateSpaceUiState.SHOW_TOO_MANY_FILES_SELECTED_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_too_many_files_error_title)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_too_many_files_error_message)) .assertIsDisplayed() composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .assertIsDisplayed() } @Test fun uiStateShowTooManyFilesSelectedDialog_clickDismissButton_callsViewModelFinishFlow() { setScreenContent(PrivateSpaceUiState.SHOW_TOO_MANY_FILES_SELECTED_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .performClick() verify(mockViewModel).finishFlow() } @Test fun uiStateShowTransferInProgressDialog_displaysCorrectTitle_whenMoving() { whenever(mockViewModel.isCopyOperationForErrorDialog()).thenReturn(false) setScreenContent(PrivateSpaceUiState.SHOW_TRANSFER_IN_PROGRESS_DIALOG) composeTestRule .onNodeWithText( context.getString(R.string.filetransfer_dialog_still_moving_error_title) ) .assertIsDisplayed() composeTestRule .onNodeWithText( context.getString(R.string.filetransfer_dialog_still_transferring_error_message) ) .assertIsDisplayed() } @Test fun uiStateShowTransferInProgressDialog_displaysCorrectTitle_whenCopying() { whenever(mockViewModel.isCopyOperationForErrorDialog()).thenReturn(true) setScreenContent(PrivateSpaceUiState.SHOW_TRANSFER_IN_PROGRESS_DIALOG) composeTestRule .onNodeWithText( context.getString(R.string.filetransfer_dialog_still_copying_error_title) ) .assertIsDisplayed() composeTestRule .onNodeWithText( context.getString(R.string.filetransfer_dialog_still_transferring_error_message) ) .assertIsDisplayed() } @Test fun uiStateShowTransferInProgressDialog_clickDismissButton_callsViewModelFinishFlow() { setScreenContent(PrivateSpaceUiState.SHOW_TOO_MANY_FILES_SELECTED_DIALOG) composeTestRule .onNodeWithText(context.getString(R.string.filetransfer_error_dialog_button)) .performClick() verify(mockViewModel).finishFlow() } @Test fun uiStateFinished_callsOnFinishedLambda() { setScreenContent(PrivateSpaceUiState.FINISHED) verify(mockOnFinished).invoke() } private fun setScreenContent(uiState: PrivateSpaceUiState) { whenever(mockViewModel.uiState).thenReturn(uiState) composeTestRule.setContent { PrivateSpaceActivityScreen( viewModel = mockViewModel, context = context, onOpenDocumentsPicker = mockOnOpenDocumentsPicker, onFinished = mockOnFinished, ) } } private fun verifyEventFields( event: PrivateSpaceMoveContentEvent, operationType: OperationType, ) { assertThat(event.operationType).isEqualTo(operationType) // Assert other fields are not set assertThat(event.transferResult).isEqualTo(TransferResult.TRANSFER_RESULT_UNKNOWN) assertThat(event.filesSelectedCount).isEqualTo(0) assertThat(event.filesSuccessCount).isEqualTo(0) assertThat(event.totalSuccessFilesSizeBucket).isEqualTo(FileSizeBucket.FILE_SIZE_UNKNOWN) assertThat(event.successFileMimeTypesList).isEmpty() assertThat(event.filesFailureCount).isEqualTo(0) assertThat(event.totalFailureFilesSizeBucket).isEqualTo(FileSizeBucket.FILE_SIZE_UNKNOWN) assertThat(event.failedFileErrorCodesCount).isEqualTo(0) assertThat(event.failedFileMimeTypesCount).isEqualTo(0) assertThat(event.notificationEvent) .isEqualTo(NotificationEventType.NOTIFICATION_EVENT_TYPE_UNKNOWN) assertThat(event.durationMs).isEqualTo(0) } }