doc/articles/uno-development/working-with-the-samples-apps.md
The SamplesApp in Uno.UI is an Uno application containing a large number of UI and non-UI samples. It serves a few purposes:
This article details how to run SamplesApp and how to add a new sample.
For instructions on working with automated UI tests, go here.
The SamplesApp from latest master branch for WebAssembly is available online: https://aka.platform.uno/wasm-samples-app
To run the SamplesApp locally:
SamplesApp.[Platform] as the startup app. (Eg, SamplesApp.iOS if you're running on iOS.)If everything builds successfully, the app will run. The app is a collection of samples, grouped into categories. You can navigate to a sample using the menu on the left.
By default, SamplesApp starts in debugging mode on localhost, making it inaccessible from external devices, even if they are on the same network. To remedy this, you can utilize Visual Studio Dev Tunnels to establish an externally accessible URL for your application. For more details, refer to the Microsoft Learn documentation. If the option doesn't appear initially, consider running a rebuild of the SamplesApp.Wasm project.
Samples are located in the UITests.Shared project. UI-related samples are generally grouped by control, or by functional area for samples that aren't specific to a particular control (eg VisualStateTests). Non-UI samples are generally grouped by namespace of the tested feature.
Note that there's no 'master list' of samples. Instead, individual samples are tagged with SampleAttribute, and the SamplesApp automatically picks up all samples using the attribute.
SampleAttribute accepts one or more optional categories, as well as an optional Name and Description, and a ViewModelType property which can be used to set a Type which will be instantiated and used as the DataContext of the sample control. If a category and/or a name aren't explicitly set, then the sample name and category will be automatically determined from the class name and the last part of the namespace.
Examples:
In the first example, no parameters are supplied to the Sample attribute. The sample will be included under the name ToolTip_Long_Text, in the ToolTip category.
namespace UITests.Windows_UI_Xaml_Controls.ToolTip
{
[Sample]
public sealed partial class ToolTip_Long_Text : UserControl
In the second example, the category and name are manually specified, and a view-model type is specified to use as the DataContext of the sample.
namespace UITests.Windows_Devices.Haptics
{
[Sample("Windows.Devices", Name = "Haptics.VibrationDevice", ViewModelType = typeof(VibrationDeviceTestsViewModel))]
public sealed partial class VibrationDeviceTests : Page
To add a new sample to the SamplesApp:
UITests.Shared project. The folder structure is typically Namespace_In_Snake_Case/ControlNameTests.UserControl from the Visual Studio templates, with a meaningful name.UserControl.[Uno.UI.Samples.Controls.Sample] attribute to the class in the code-behind partial file.UITests.Shared.projitems
Open src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Add an entry for your XAML page and code-behind file:
<Page Include="$(MSBuildThisFileDirectory)YourFolder\YourSample.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Compile Include="$(MSBuildThisFileDirectory)YourFolder\YourSample.xaml.cs">
<DependentUpon>YourSample.xaml</DependentUpon>
</Compile>
Without this step, your sample will NOT be visible in SamplesApp
dotnet xstyler -f src/SamplesApp/UITests.Shared/YourFolder/YourSample.xaml (requires dotnet tool restore first, see Code style).SamplesApp to check that your sample appears in the browser and works as expected.Some tests cannot be validated automatically, and need to be flagged with the IsManualTest property on SampleAttribute. These tests will be filtered in the Samples App to be validated by a human.
The content of those tests must describe a scenario to follow, what to expect, and which exceptional conditions may need to be validated. If the result is visual, an image or video resource file may be needed as well.
Each CI build of Uno.UI records screenshots of each sample in the SamplesApp. A diff tool details screenshots that have changed from the previous master build, allowing unexpected changes in the visual output to be caught.
The WebAssembly head has the ability to be run through puppeteer, and displays all tests in sequence. Puppeteer runs a headless version of Chromium, suited for running tests in a CI environment.
To run the tests:
SamplesApp.Wasm.UITests folder and run npm i. This will download Puppeteer and the Chrome driver.SamplesApp.Wasm.UITests.njsproj projectF5, node will start and run the tests sequentiallyoutNote that the same operation is run during the CI, in a specific job running under Linux. The screen shots are located in the Unit Tests section under Screenshots Compare Test Run as well as in the build artifact.
In the CI build, an artifact named wasm-uitests is generated and contains an HTML file that shows all the differences
for screenshots taken for the past builds. Download this artifact and open the html file to determine if any screenshots
have changed.
It is possible to enable the chromium head using the configuration parameters in the app.ts file.
Performance is measured using BenchmarkDotNet, in the suite located in the SamplesApp.Benchmarks shared project.
A few points to consider when adding benchmarks:
_