surface-duo-compose-sdk/ComposeTesting
Joy Liu 84fa87338d
Introduce ComposeTesting library (#11)
* Introduce ComposeTesting

* Add sample

* Add test sample

* Add missing files

* Update dependencies

* Add more comments

* Update FoldHelper to use TestRule

* Add ktlint and update config

* Clean up the dependencies

* Rename the APIs

* Update README

* Add simple SwipeHelper example tests

* Update dependencies and README

* Update pipeline to include ComposeTesting

* Delete local.properties

* Delete local.properties

* Delete local.properties

* Add more missing files

* Move ktlint back to 0.40.0

* Address the comments

* Update the name in yml files

* Add more tests

* Rename the package name

Co-authored-by: Kristen Halper <khalp@vt.edu>
2022-02-04 11:57:19 -08:00
..
gradle/wrapper Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
library Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
sample Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
.gitignore Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
README.md Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
build.gradle Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
dependencies.gradle Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
gradle.properties Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
gradlew Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
gradlew.bat Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
ktlint.gradle Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
publishing.gradle Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00
settings.gradle Introduce ComposeTesting library (#11) 2022-02-04 11:57:19 -08:00

README.md

ComposeTesting - Surface Duo Compose SDK

ComposeTesting provides some helper functions that help you easily test your application on the dual-screen, foldable and large screen devices, by simulating the foldingFeatures(fold/hinge), device gestures, screenshot comparing and zooming.

Add to your project

  1. Make sure you have mavenCentral() repository in your top level build.gradle or settings.gradle file:

    repositories {
        google()
        mavenCentral()
    }
    
  2. Add dependencies to the module-level build.gradle file (current version may be different from what's shown here).

    implementation "com.microsoft.device.dualscreen.testing:testing-compose:1.0.0-alpha01"
    
  3. Also ensure the compileSdkVersion and targetSdkVersion are set to API 31 or newer in the module-level build.gradle file.

    android { 
        compileSdkVersion 31
    
        defaultConfig { 
            targetSdkVersion 31
        } 
        ... 
    }
    
  4. Access the testing functions from ComposeTesting to test your application. Please refer to the sample for more details.

API reference

FoldingFeature Helper

These functions can be used in foldable UI tests to simulate the present of vertical and horizontal foldingFeatures(folds/hinges). The foldingFeatures are simulated using TestWindowLayoutInfo, using the Google Jetpack WindowManager Testing library.

fun createWindowLayoutInfoPublisherRule(): TestRule

Return WindowLayoutInfoPublisherRule which allows you to simulate the different foldingFeature by pushing through different WindowLayoutInfo values.

fun <A : ComponentActivity> TestRule.simulateVerticalFoldingFeature(
    composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<A>, A>,
    center: Int = -1,
    size: Int = 0,
    state: FoldingFeature.State = FoldingFeature.State.HALF_OPENED
)

Simulate a vertical foldingFeature in a Compose test

fun <A : ComponentActivity> TestRule.simulateHorizontalFoldingFeature(
    composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<A>, A>,
    center: Int = -1,
    size: Int = 0,
    state: FoldingFeature.State = FoldingFeature.State.HALF_OPENED
)

Simulate a horizontal foldingFeature in a Compose test

fun <A : ComponentActivity> TestRule.simulateFoldingFeature(
    composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<A>, A>,
    center: Int,
    size: Int,
    state: FoldingFeature.State,
    orientation: FoldingFeature.Orientation,
) 

Simulate a foldingFeature with the given properties in a Compose test

Swipe Helper

These functions can be used in dual-screen UI tests to simulate swipe gestures that affect app display. The swipes are simulated using UiDevice, and the coordinates are calculated based on the display width/height of the testing device. They can be used only on dual-screen devices, not foldable devices or large screen devices.

fun UiDevice.spanFromStart()

Span app from the top/left pane

fun UiDevice.spanFromEnd()

Span app from the bottom/right pane

fun UiDevice.unspanToStart()

Unspan app to the top/left pane

fun UiDevice.unspanToEnd()

Unspan app to bottom/right pane

fun UiDevice.switchToStart()

Switch app from bottom/right pane to top/left pane

fun UiDevice.switchToEnd() 

Switch app from top/left pane to bottom/right pane

fun UiDevice.closeStart() 

Close app from top/left pane

fun UiDevice.closeEnd()

Close app from bottom/right pane

String Helper

These functions can be used for string operations in UI tests to simplify testing code.

fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.getString(@StringRes id: Int): String

Get resource string inside Compose test with resource id

fun <A : ComponentActivity> AndroidComposeTestRule<ActivityScenarioRule<A>, A>.getString(@StringRes id: Int, vararg formatArgs: Any): String

Get resource string inside Compose test with resource id and arguments

Zoom Helper

These functions can be used to perform zooming gestures during Compose UI tests.

fun TouchInjectionScope.zoomIn(pinchMillis: Long = PINCH_MILLIS)

Performs a zoom in gesture (swipes start towards center then move outwards)

fun TouchInjectionScope.zoomOut(pinchMillis: Long = PINCH_MILLIS)

Performs a zoom out gesture (swipes start towards center then move outwards)

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

Copyright (c) Microsoft Corporation.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.