fun resultsReported_forScreenshotPreviewTestsWithNestedTestSuites()

in project-system-gradle/testSrc/com/android/tools/idea/testartifacts/testsuite/temp/AndroidTestSuiteViewAdaptorTest.kt [135:560]


  fun resultsReported_forScreenshotPreviewTestsWithNestedTestSuites() {
    val runConfiguration = mock<RunConfiguration>()
    val adaptor = AndroidTestSuiteViewAdaptor(runConfiguration)
    val testSuiteView = mock<AndroidTestSuiteView>()
    val expectedDevice =
      AndroidDevice(
        id = "Preview",
        deviceName = "Preview",
        avdName = "",
        deviceType = AndroidDeviceType.LOCAL_EMULATOR,
        version = AndroidVersion.DEFAULT,
      )
    val expectedTestSuite =
      AndroidTestSuite(
        id = "1.1",
        name = "Gradle Test Executor 1",
        testCaseCount = 0,
        result = null,
        runConfiguration = runConfiguration,
      )

    // Start internal Gradle test suites
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = ":app:validateDebugScreenshotTest",
        parentId = "",
        name = "Gradle Test Run :app:validateDebugScreenshotTest",
        displayName = "Gradle Test Run :app:validateDebugScreenshotTest",
        className = "",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = "1.1",
        parentId = ":app:validateDebugScreenshotTest",
        name = "Gradle Test Executor 1",
        displayName = "Gradle Test Executor 1",
        className = "",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createOnOutputXml(
        id = "1.1",
        parentId = ":app:validateDebugScreenshotTest",
        name = "Gradle Test Executor 1",
        displayName = "Gradle Test Executor 1",
        className = "",
        content =
          "W2FkZGl0aW9uYWxUZXN0QXJ0aWZhY3RzXWRldmljZUlkPVByZXZpZXcK", // [additionalTestArtifacts]deviceId=Preview
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createOnOutputXml(
        id = "1.1",
        parentId = ":app:validateDebugScreenshotTest",
        name = "Gradle Test Executor 1",
        displayName = "Gradle Test Executor 1",
        className = "",
        content =
          "W2FkZGl0aW9uYWxUZXN0QXJ0aWZhY3RzXWRldmljZURpc3BsYXlOYW1lPVByZXZpZXcK", // [additionalTestArtifacts]deviceDisplayName=Preview
      ),
      testSuiteView,
    )

    verify(testSuiteView, times(1)).onTestSuiteScheduled(expectedDevice)

    // Start nested test suites
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = "1.2",
        parentId = "1.1",
        name = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        displayName = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = "1.3",
        parentId = "1.2",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = "1.4",
        parentId = "1.3",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
      ),
      testSuiteView,
    )

    verify(testSuiteView, times(1)).onTestSuiteStarted(expectedDevice, expectedTestSuite)

    // Start first test
    adaptor.processEvent(
      createBeforeTestXml(
        id = "1.5",
        parentId = "1.4",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
      ),
      testSuiteView,
    )

    val expectedFirstTestCase =
      AndroidTestCase(
        id = "1.5",
        methodName = "GreetingPreview",
        className = "ExamplePreviewsScreenshots",
        packageName = "com.example.screenshottesting",
        result = AndroidTestCaseResult.IN_PROGRESS,
      )
    verify(testSuiteView, times(1))
      .onTestCaseStarted(
        expectedDevice,
        expectedTestSuite.copy(testCaseCount = 1),
        expectedFirstTestCase,
      )

    // Report artifacts
    adaptor.processEvent(
      createOnOutputXml(
        id = "1.5",
        parentId = "1.4",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        content =
          Base64.getEncoder()
            .encodeToString(
              "[additionalTestArtifacts]PreviewScreenshot.newImagePath=ExamplePreviewsScreenshots/GreetingPreview_748aa731_0.png"
                .toByteArray()
            ),
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createOnOutputXml(
        id = "1.5",
        parentId = "1.4",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        content =
          Base64.getEncoder()
            .encodeToString(
              "[additionalTestArtifacts]PreviewScreenshot.refImagePath=ExamplePreviewsScreenshots/GreetingPreview_238a7281_0.png"
                .toByteArray()
            ),
      ),
      testSuiteView,
    )

    // End first test
    adaptor.processEvent(
      createAfterTestXml(
        id = "1.5",
        parentId = "1.4",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        resultType = "SUCCESS",
        startTime = "1749122532921",
        endTime = "1749122532948",
      ),
      testSuiteView,
    )

    verify(testSuiteView, times(1))
      .onTestCaseFinished(
        expectedDevice,
        expectedTestSuite.copy(testCaseCount = 1),
        expectedFirstTestCase.copy(
          result = AndroidTestCaseResult.PASSED,
          startTimestampMillis = 1749122532921,
          endTimestampMillis = 1749122532948,
          additionalTestArtifacts =
            mutableMapOf(
              "PreviewScreenshot.newImagePath" to
                "ExamplePreviewsScreenshots/GreetingPreview_748aa731_0.png",
              "PreviewScreenshot.refImagePath" to
                "ExamplePreviewsScreenshots/GreetingPreview_238a7281_0.png",
            ),
        ),
      )

    // End nested test suites
    adaptor.processEvent(
      createAfterSuiteXml(
        id = "1.4",
        parentId = "1.3",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        resultType = "SUCCESS",
        startTime = "1749122530884",
        endTime = "1749122532949",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createAfterSuiteXml(
        id = "1.3",
        parentId = "1.2",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        resultType = "SUCCESS",
        startTime = "1749122530882",
        endTime = "1749122532949",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createAfterSuiteXml(
        id = "1.2",
        parentId = "1.1",
        name = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        displayName = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots",
        resultType = "SUCCESS",
        startTime = "1749122530881",
        endTime = "1749122532949",
      ),
      testSuiteView,
    )

    // Start second set of nested test suites
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = "1.6",
        parentId = "1.1",
        name = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        displayName = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = "1.7",
        parentId = "1.6",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createBeforeSuiteXml(
        id = "1.8",
        parentId = "1.7",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
      ),
      testSuiteView,
    )

    // Start second test
    adaptor.processEvent(
      createBeforeTestXml(
        id = "1.9",
        parentId = "1.8",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
      ),
      testSuiteView,
    )

    val expectedSecondTestCase =
      AndroidTestCase(
        id = "1.9",
        methodName = "GreetingPreview",
        className = "ExamplePreviewsScreenshots2",
        packageName = "com.example.screenshottesting",
        result = AndroidTestCaseResult.IN_PROGRESS,
      )
    verify(testSuiteView, times(1))
      .onTestCaseStarted(
        expectedDevice,
        expectedTestSuite.copy(testCaseCount = 2),
        expectedSecondTestCase,
      )

    // Report artifacts
    adaptor.processEvent(
      createOnOutputXml(
        id = "1.9",
        parentId = "1.8",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        content =
          Base64.getEncoder()
            .encodeToString(
              "[additionalTestArtifacts]PreviewScreenshot.newImagePath=ExamplePreviewsScreenshots2/GreetingPreview_748aa731_0.png"
                .toByteArray()
            ),
      ),
      testSuiteView,
    )

    // End second test
    adaptor.processEvent(
      createAfterTestXml(
        id = "1.9",
        parentId = "1.8",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        resultType = "FAILURE",
        startTime = "1749122532994",
        endTime = "1749122533001",
      ),
      testSuiteView,
    )

    verify(testSuiteView, times(1))
      .onTestCaseFinished(
        expectedDevice,
        expectedTestSuite.copy(testCaseCount = 2),
        expectedSecondTestCase.copy(
          result = AndroidTestCaseResult.FAILED,
          startTimestampMillis = 1749122532994,
          endTimestampMillis = 1749122533001,
          additionalTestArtifacts =
            mutableMapOf(
              "PreviewScreenshot.newImagePath" to
                "ExamplePreviewsScreenshots2/GreetingPreview_748aa731_0.png"
            ),
        ),
      )

    // End second set of nested test suites
    adaptor.processEvent(
      createAfterSuiteXml(
        id = "1.8",
        parentId = "1.7",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        resultType = "FAILURE",
        startTime = "1749122532951",
        endTime = "1749122533001",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createAfterSuiteXml(
        id = "1.7",
        parentId = "1.6",
        name = "GreetingPreview",
        displayName = "GreetingPreview",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        resultType = "FAILURE",
        startTime = "1749122532950",
        endTime = "1749122533001",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createAfterSuiteXml(
        id = "1.6",
        parentId = "1.1",
        name = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        displayName = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        className = "com.example.screenshottesting.ExamplePreviewsScreenshots2",
        resultType = "FAILURE",
        startTime = "1749122532950",
        endTime = "1749122533001",
      ),
      testSuiteView,
    )

    // End internal Gradle test suites
    adaptor.processEvent(
      createAfterSuiteXml(
        id = "1.1",
        parentId = ":app:validateDebugScreenshotTest",
        name = "Gradle Test Executor 1",
        displayName = "Gradle Test Executor 1",
        className = "",
        resultType = "FAILURE",
        startTime = "1749122530285",
        endTime = "1749122533057",
      ),
      testSuiteView,
    )
    adaptor.processEvent(
      createAfterSuiteXml(
        id = ":app:validateDebugScreenshotTest",
        parentId = "",
        name = "Gradle Test Run :app:validateDebugScreenshotTest",
        displayName = "Gradle Test Run :app:validateDebugScreenshotTest",
        className = "",
        resultType = "FAILURE",
        startTime = "1749122529964",
        endTime = "1749122533463",
      ),
      testSuiteView,
    )

    verify(testSuiteView, times(1))
      .onTestSuiteFinished(
        expectedDevice,
        expectedTestSuite.copy(testCaseCount = 2, result = AndroidTestSuiteResult.FAILED),
      )

    // Both tests share the same root test suite, so the test suite callbacks should have only been
    // called once
    verify(testSuiteView, times(1)).onTestSuiteStarted(eq(expectedDevice), any())
    verify(testSuiteView, times(1)).onTestSuiteFinished(eq(expectedDevice), any())
  }