Skip to content

Commit 8fb45dc

Browse files
duracellkoglennawatson
authored andcommitted
feature: add Android unit tests (#2201)
* Add Android tests * Deploy Android unit tests in solution * Fix memory leak in PocoObservableForPropertyTests amd make tests more robust
1 parent a82f7a6 commit 8fb45dc

File tree

16 files changed

+378
-21
lines changed

16 files changed

+378
-21
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,4 +359,7 @@ src/*.Tests/**/ApiApprovalTests*.received.txt
359359
.idea/
360360

361361
# Fody Weavers (for tests)
362-
src/Tools/
362+
src/Tools/
363+
364+
# Android auto-generated Resource file
365+
src/ReactiveUI.Android.Tests/Resources/Resource.[Dd]esigner.cs
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Any raw assets you want to be deployed with your application can be placed in
2+
this directory (and child directories) and given a Build Action of "AndroidAsset".
3+
4+
These files will be deployed with your package and will be accessible using Android's
5+
AssetManager, like this:
6+
7+
public class ReadAsset : Activity
8+
{
9+
protected override void OnCreate (Bundle bundle)
10+
{
11+
base.OnCreate (bundle);
12+
13+
InputStream input = Assets.Open ("my_asset.txt");
14+
}
15+
}
16+
17+
Additionally, some Android functions will automatically load asset files:
18+
19+
Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"diagnosticMessages": true,
3+
"longRunningTestSeconds": 30,
4+
"methodDisplay": "classAndMethod"
5+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using System;
7+
using System.Reflection;
8+
using Android.App;
9+
using Android.Content;
10+
using Android.Runtime;
11+
using Android.Views;
12+
using Android.Widget;
13+
using Android.OS;
14+
15+
using Xunit.Sdk;
16+
using Xunit.Runners.UI;
17+
18+
namespace Splat.Android.Tests
19+
{
20+
/// <summary>
21+
/// Unit Test Runner Activity.
22+
/// </summary>
23+
// ReSharper disable UnusedMember.Global
24+
[Activity(Label = "xUnit Android Runner", MainLauncher = true, Theme = "@android:style/Theme.Material.Light")]
25+
public class MainActivity : RunnerActivity
26+
27+
// ReSharper restore UnusedMember.Global
28+
{
29+
/// <inheritdoc/>
30+
protected override void OnCreate(Bundle bundle)
31+
{
32+
Locator.CurrentMutable.RegisterPlatformBitmapLoader();
33+
AddExecutionAssembly(typeof(ExtensibilityPointFactory).Assembly);
34+
35+
AddTestAssembly(typeof(ReactiveUI.Tests.RxAppTest).Assembly);
36+
37+
// you cannot add more assemblies once calling base
38+
base.OnCreate(bundle);
39+
}
40+
}
41+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ReactiveUI.Android.Tests" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal">
3+
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
4+
<application android:label="Splat.Android.Tests" android:icon="@drawable/Icon"></application>
5+
</manifest>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using System.Reflection;
7+
using System.Runtime.CompilerServices;
8+
using System.Runtime.InteropServices;
9+
10+
// General Information about an assembly is controlled through the following
11+
// set of attributes. Change these attribute values to modify the information
12+
// associated with an assembly.
13+
[assembly: AssemblyTitle("ReactiveUI.Android.Tests")]
14+
[assembly: AssemblyDescription("")]
15+
[assembly: AssemblyConfiguration("")]
16+
[assembly: AssemblyCompany("")]
17+
[assembly: AssemblyProduct("ReactiveUI.Android.Tests")]
18+
[assembly: AssemblyCopyright("Copyright © 2019")]
19+
[assembly: AssemblyTrademark("")]
20+
[assembly: AssemblyCulture("")]
21+
[assembly: ComVisible(false)]
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{CBF8E93D-A2EA-4692-8087-B3655A35B0C0}</ProjectGuid>
9+
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
10+
<TemplateGuid>{122416d6-6b49-4ee2-a1e8-b825f31c79fe}</TemplateGuid>
11+
<OutputType>Library</OutputType>
12+
<AppDesignerFolder>Properties</AppDesignerFolder>
13+
<RootNamespace>ReactiveUI.Android.Tests</RootNamespace>
14+
<AssemblyName>ReactiveUI.Android.Tests</AssemblyName>
15+
<FileAlignment>512</FileAlignment>
16+
<AndroidApplication>True</AndroidApplication>
17+
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
18+
<AndroidResgenClass>Resource</AndroidResgenClass>
19+
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
20+
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
21+
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
22+
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
23+
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
24+
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
25+
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
26+
<AndroidUseAapt2>true</AndroidUseAapt2>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
29+
<DebugSymbols>True</DebugSymbols>
30+
<DebugType>Full</DebugType>
31+
<Optimize>False</Optimize>
32+
<OutputPath>bin\Debug\</OutputPath>
33+
<DefineConstants>DEBUG;TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
<AndroidUseSharedRuntime>True</AndroidUseSharedRuntime>
37+
<AndroidLinkMode>None</AndroidLinkMode>
38+
<EmbedAssembliesIntoApk>False</EmbedAssembliesIntoApk>
39+
</PropertyGroup>
40+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
41+
<DebugSymbols>True</DebugSymbols>
42+
<DebugType>PdbOnly</DebugType>
43+
<Optimize>True</Optimize>
44+
<OutputPath>bin\Release\</OutputPath>
45+
<DefineConstants>TRACE</DefineConstants>
46+
<ErrorReport>prompt</ErrorReport>
47+
<WarningLevel>4</WarningLevel>
48+
<AndroidManagedSymbols>true</AndroidManagedSymbols>
49+
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
50+
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
51+
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
52+
</PropertyGroup>
53+
<ItemGroup>
54+
<Reference Include="System" />
55+
<Reference Include="System.Core" />
56+
<Reference Include="Mono.Android" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="MainActivity.cs" />
60+
<Compile Include="Resources\Resource.designer.cs" />
61+
<Compile Include="Properties\AssemblyInfo.cs" />
62+
<AdditionalFiles Include="..\stylecop.json">
63+
<Link>stylecop.json</Link>
64+
</AdditionalFiles>
65+
<None Include="Resources\AboutResources.txt" />
66+
<None Include="Properties\AndroidManifest.xml" />
67+
<None Include="Assets\AboutAssets.txt" />
68+
<AndroidAsset Include="Assets\xunit.runner.json" />
69+
<AndroidResource Include="Resources\layout\Main.axml" />
70+
<AndroidResource Include="Resources\values\Strings.xml" />
71+
<AndroidResource Include="Resources\drawable\Icon.png" />
72+
</ItemGroup>
73+
<ItemGroup>
74+
<PackageReference Include="xunit.runner.devices">
75+
<Version>2.*</Version>
76+
</PackageReference>
77+
</ItemGroup>
78+
<ItemGroup>
79+
<ProjectReference Include="..\ReactiveUI.Tests\ReactiveUI.Tests.csproj">
80+
<Project>{2ade0a50-5012-4341-8f4f-97597c2d6920}</Project>
81+
<Name>ReactiveUI.Tests</Name>
82+
</ProjectReference>
83+
<ProjectReference Include="..\ReactiveUI\ReactiveUI.csproj">
84+
<Project>{464cb812-f99f-401b-be4c-e8f0515cd19d}</Project>
85+
<Name>ReactiveUI</Name>
86+
</ProjectReference>
87+
</ItemGroup>
88+
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
89+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
90+
Other similar extension points exist, see Microsoft.Common.targets.
91+
<Target Name="BeforeBuild">
92+
</Target>
93+
<Target Name="AfterBuild">
94+
</Target>
95+
-->
96+
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Images, layout descriptions, binary blobs and string dictionaries can be included
2+
in your application as resource files. Various Android APIs are designed to
3+
operate on the resource IDs instead of dealing with images, strings or binary blobs
4+
directly.
5+
6+
For example, a sample Android app that contains a user interface layout (main.xml),
7+
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
8+
would keep its resources in the "Resources" directory of the application:
9+
10+
Resources/
11+
drawable/
12+
icon.png
13+
14+
layout/
15+
main.xml
16+
17+
values/
18+
strings.xml
19+
20+
In order to get the build system to recognize Android resources, set the build action to
21+
"AndroidResource". The native Android APIs do not operate directly with filenames, but
22+
instead operate on resource IDs. When you compile an Android application that uses resources,
23+
the build system will package the resources for distribution and generate a class called "R"
24+
(this is an Android convention) that contains the tokens for each one of the resources
25+
included. For example, for the above Resources layout, this is what the R class would expose:
26+
27+
public class R {
28+
public class drawable {
29+
public const int icon = 0x123;
30+
}
31+
32+
public class layout {
33+
public const int main = 0x456;
34+
}
35+
36+
public class strings {
37+
public const int first_string = 0xabc;
38+
public const int second_string = 0xbcd;
39+
}
40+
}
41+
42+
You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main
43+
to reference the layout/main.xml file, or R.strings.first_string to reference the first
44+
string in the dictionary file values/strings.xml.
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical"
4+
android:layout_width="fill_parent"
5+
android:layout_height="fill_parent"
6+
>
7+
<Button
8+
android:id="@+id/MyButton"
9+
android:layout_width="fill_parent"
10+
android:layout_height="wrap_content"
11+
android:text="@string/Hello"
12+
/>
13+
</LinearLayout>

0 commit comments

Comments
 (0)