Repository: flutter
Issue: Tests using `integration_test` have incorrect duration when run natively
Labels: framework,f: integration_test,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-framework,triaged-framework

## Steps to reproduce

1. Clone the [bartekpacia/flutter_integration_test_duration_bug](https://github.com/bartekpacia/flutter_integration_test_duration_bug) repository. This is a default counter app slightly modified by me to demonstrate this problem. `cd` to the cloned repository.
3. `$ flutter pub get`
4. `$ cd android`
5. Run Flutter integration test natively:
     
    ```console
    $ ./gradlew app:connectedDebugAndroidTest -Ptarget=$(pwd)/../integration_test/app_test.dart
    ```

8. Open the test summary generated by Gradle:

    ```
    $ open ../build/app/reports/androidTests/connected/index.html
    ```

The same problem occurs when running tests on iOS. Refer to [integration_test's README](https://github.com/flutter/flutter/tree/master/packages/integration_test) to learn how to run tests on iOS.

**Expected results:** <!-- what did you want to see? -->

I expect the test duration to be correct. 

The `integration_test/app_test.dart` that was run calls `tester.pump(Duration(seconds: 3));` twice, so the duration should not be shorter than at least 6 seconds.

**Actual results:** <!-- what did you see? -->

Test duration is very short, on my machine it usually revolves around ~300ms.

![Screenshot 2022-12-20 at 12 51 33 PM](https://user-images.githubusercontent.com/40357511/208660632-e7b052b3-6b5b-44bd-9687-8452c0aa492a.png)

**More details**

This problem is caused by how Dart test results are submitted to the native side, and how native test cases are created from these Dart test results.

Currently `IntegrationTestWidgetsFlutterBinding` [submits Dart test results](https://github.com/flutter/flutter/blob/master/packages/integration_test/lib/integration_test.dart#L55-L56) to the native side after all Dart tests have been executed. When the native side receives these results, it creates native test cases ([Android implementation](https://github.com/flutter/flutter/blob/master/packages/integration_test/android/src/main/java/dev/flutter/plugins/integration_test/FlutterTestRunner.java), [iOS implementation](https://github.com/flutter/flutter/blob/master/packages/integration_test/ios/Classes/IntegrationTestIosTest.h)). But these test cases are simply stubs – their execution is finished immediately after creation. That's why the runtime of these testcases is so very short.

A solution that comes to my mind would do the following:
1. Collect all declared groups and tests declared in Dart.
2. **Submit declared groups and tests to the native side**.
3. The native side receives test results. This is where the test duration on native starts.
4. Dart tests execute, and finally finish.
5. **Submit Dart test results to the native side**


<details>
  <summary>Logs</summary>

```
$ ./gradlew app:connectedDebugAndroidTest -Ptarget=$(pwd)/../integration_test/app_test.dart

> Task :app:compressDebugAssets
Execution optimizations have been disabled for task ':app:compressDebugAssets' to ensure correctness due to the following reasons:
  - Gradle detected a problem with the following location: '/Users/bartek/dev/random/flutter_integration_test_duration_bug/build/app/intermediates/assets/debug/mergeDebugAssets'. Reason: Task ':app:compressDebugAssets' uses this output of task ':app:copyFlutterAssetsDebug' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. Please refer to https://docs.gradle.org/7.4/userguide/validation_problems.html#implicit_dependency for more details about this problem.

> Task :app:connectedDebugAndroidTest
Starting 1 tests on Pixel_4_API_31(AVD) - 12

Test results saved as file:/Users/bartek/dev/random/flutter_integration_test_duration_bug/build/app/outputs/androidTest-results/connected/Pixel_4_API_31(AVD)%20-%2012/test-result.pb. Inspect these results in Android Studio by selecting Run > Import Tests From File from the menu bar and importing test-result.pb.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4/userguide/command_line_interface.html#sec:command_line_warnings

Execution optimizations have been disabled for 1 invalid unit(s) of work during this build to ensure correctness.
Please consult deprecation warnings for more details.

BUILD SUCCESSFUL in 16s
76 actionable tasks: 5 executed, 71 up-to-date
```
```
$ flutter doctor -v
[✓] Flutter (Channel stable, 3.3.10, on macOS 13.0.1 22A400 darwin-arm, locale en-PL)
    • Flutter version 3.3.10 on channel stable at /Users/bartek/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 135454af32 (5 days ago), 2022-12-15 07:36:55 -0800
    • Engine revision 3316dd8728
    • Dart version 2.18.6
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/bartek/androidsdk
    • Platform android-33, build-tools 33.0.1
    • ANDROID_HOME = /Users/bartek/androidsdk
    • Java binary at: /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3)
    • Android Studio at /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] IntelliJ IDEA Ultimate Edition (version 2022.3)
    • IntelliJ at /Users/bartek/Applications/JetBrains Toolbox/IntelliJ IDEA Ultimate.app
    • Flutter plugin version 71.1.6
    • Dart plugin version 223.7571.203

[✓] IntelliJ IDEA Ultimate Edition (version 2022.3)
    • IntelliJ at /Users/bartek/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/223.7571.182/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.74.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.54.0

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 12 (API 31) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 13.0.1 22A400 darwin-arm
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 108.0.5359.124

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>