-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Package - Add unit test project
- Loading branch information
Showing
30 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:label="@string/app_name" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"> | ||
</application> | ||
<uses-permission android:name="android.permission.INTERNET" /> | ||
</manifest> |
21 changes: 21 additions & 0 deletions
21
AndroidX.Work.Testing.UnitTest/AndroidX.Work.Testing.UnitTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0-android</TargetFramework> | ||
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion> | ||
<OutputType>Exe</OutputType> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<ApplicationId>ai.smartway.androidx.work.testing.unittests</ApplicationId> | ||
<ApplicationVersion>1</ApplicationVersion> | ||
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="NFluent" Version="3.0.4" /> | ||
<PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.6.1.8" /> | ||
<PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.9.0.2" /> | ||
<PackageReference Include="Xamarin.Legacy.NUnitLite" Version="0.0.1-alpha" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\AndroidX.Work.Testing\AndroidX.Work.Testing.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Reflection; | ||
using Xamarin.Android.NUnitLite; | ||
|
||
namespace AndroidApp1 | ||
{ | ||
[Activity(Label = "@string/app_name", MainLauncher = true)] | ||
public class MainActivity : TestSuiteActivity | ||
{ | ||
protected override void OnCreate(Bundle bundle) | ||
{ | ||
// tests can be inside the main assembly | ||
AddTest(Assembly.GetExecutingAssembly()); | ||
// or in any reference assemblies | ||
// AddTest(typeof(Your.Library.TestClass).Assembly); | ||
|
||
// Once you called base.OnCreate(), you cannot add more assemblies. | ||
base.OnCreate(bundle); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
AndroidX.Work.Testing.UnitTest/Resources/AboutResources.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Images, layout descriptions, binary blobs and string dictionaries can be included | ||
in your application as resource files. Various Android APIs are designed to | ||
operate on the resource IDs instead of dealing with images, strings or binary blobs | ||
directly. | ||
|
||
For example, a sample Android app that contains a user interface layout (main.xml), | ||
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png) | ||
would keep its resources in the "Resources" directory of the application: | ||
|
||
Resources/ | ||
drawable/ | ||
icon.png | ||
|
||
layout/ | ||
main.xml | ||
|
||
values/ | ||
strings.xml | ||
|
||
In order to get the build system to recognize Android resources, set the build action to | ||
"AndroidResource". The native Android APIs do not operate directly with filenames, but | ||
instead operate on resource IDs. When you compile an Android application that uses resources, | ||
the build system will package the resources for distribution and generate a class called "Resource" | ||
(this is an Android convention) that contains the tokens for each one of the resources | ||
included. For example, for the above Resources layout, this is what the Resource class would expose: | ||
|
||
public class Resource { | ||
public class Drawable { | ||
public const int icon = 0x123; | ||
} | ||
|
||
public class Layout { | ||
public const int main = 0x456; | ||
} | ||
|
||
public class Strings { | ||
public const int first_string = 0xabc; | ||
public const int second_string = 0xbcd; | ||
} | ||
} | ||
|
||
You would then use Resource.Drawable.icon to reference the drawable/icon.png file, or | ||
Resource.Layout.main to reference the layout/main.xml file, or Resource.Strings.first_string | ||
to reference the first string in the dictionary file values/strings.xml. |
13 changes: 13 additions & 0 deletions
13
AndroidX.Work.Testing.UnitTest/Resources/layout/activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_centerInParent="true" | ||
android:text="@string/app_text" | ||
/> | ||
</RelativeLayout> |
4 changes: 4 additions & 0 deletions
4
AndroidX.Work.Testing.UnitTest/Resources/mipmap-anydpi-v26/appicon.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<background android:drawable="@mipmap/appicon_background" /> | ||
<foreground android:drawable="@mipmap/appicon_foreground" /> | ||
</adaptive-icon> |
4 changes: 4 additions & 0 deletions
4
AndroidX.Work.Testing.UnitTest/Resources/mipmap-anydpi-v26/appicon_round.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<background android:drawable="@mipmap/appicon_background" /> | ||
<foreground android:drawable="@mipmap/appicon_foreground" /> | ||
</adaptive-icon> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+97 Bytes
AndroidX.Work.Testing.UnitTest/Resources/mipmap-hdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.25 KB
AndroidX.Work.Testing.UnitTest/Resources/mipmap-hdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+92 Bytes
AndroidX.Work.Testing.UnitTest/Resources/mipmap-mdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.24 KB
AndroidX.Work.Testing.UnitTest/Resources/mipmap-mdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+100 Bytes
AndroidX.Work.Testing.UnitTest/Resources/mipmap-xhdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.76 KB
AndroidX.Work.Testing.UnitTest/Resources/mipmap-xhdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+108 Bytes
AndroidX.Work.Testing.UnitTest/Resources/mipmap-xxhdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.88 KB
AndroidX.Work.Testing.UnitTest/Resources/mipmap-xxhdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+117 Bytes
AndroidX.Work.Testing.UnitTest/Resources/mipmap-xxxhdpi/appicon_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+2.74 KB
AndroidX.Work.Testing.UnitTest/Resources/mipmap-xxxhdpi/appicon_foreground.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions
4
AndroidX.Work.Testing.UnitTest/Resources/values/ic_launcher_background.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<color name="ic_launcher_background">#2C3E50</color> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<resources> | ||
<string name="app_name">AndroidApp1</string> | ||
<string name="app_text">Hello, Android!</string> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Android.Content; | ||
|
||
namespace AndroidX.Work.Testing.UnitTests | ||
{ | ||
public class RetryWorker : Worker | ||
{ | ||
public RetryWorker(Context context, WorkerParameters workerParams) | ||
: base(context, workerParams) { } | ||
|
||
public static PeriodicWorkRequest Build() | ||
{ | ||
Constraints constraints = new Constraints.Builder() | ||
.SetRequiresCharging(true) | ||
.Build(); | ||
|
||
var syncWorkRequest = PeriodicWorkRequest | ||
.Builder | ||
.From<RetryWorker>(TimeSpan.FromMinutes(15)) | ||
.SetConstraints(constraints) | ||
.Build(); | ||
|
||
return syncWorkRequest; | ||
} | ||
|
||
public override Result DoWork() | ||
{ | ||
return Result.InvokeRetry(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Android.Content; | ||
using System; | ||
|
||
namespace AndroidX.Work.Testing.UnitTests | ||
{ | ||
public class SuccessWorker : Worker | ||
{ | ||
public SuccessWorker(Context context, WorkerParameters workerParams) | ||
: base(context, workerParams) { } | ||
|
||
public static PeriodicWorkRequest Build() | ||
{ | ||
Constraints constraints = new Constraints.Builder() | ||
.SetRequiresCharging(true) | ||
.Build(); | ||
|
||
var syncWorkRequest = PeriodicWorkRequest | ||
.Builder | ||
.From<SuccessWorker>(TimeSpan.FromMinutes(15)) | ||
.SetConstraints(constraints) | ||
.Build(); | ||
|
||
return syncWorkRequest; | ||
} | ||
|
||
public override Result DoWork() | ||
{ | ||
return Result.InvokeSuccess(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Android.Content; | ||
using Androidx.Work.Testing; | ||
using NFluent; | ||
using NUnit.Framework; | ||
|
||
namespace AndroidX.Work.Testing.UnitTests | ||
{ | ||
[TestFixture] | ||
public class TestRetryWorker : TestWorker | ||
{ | ||
public PeriodicWorkRequest Worker { get; private set; } | ||
|
||
public IOperation Operation { get; private set; } | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
Worker = RetryWorker.Build(); | ||
Operation = WorkManager.EnqueueUniquePeriodicWork("Work", ExistingPeriodicWorkPolicy.Update, Worker); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
WorkManager.CancelUniqueWork("Work"); | ||
} | ||
|
||
[Test] | ||
public void Retry() | ||
{ | ||
var driver = WorkManagerTestInitHelper.GetTestDriver(Context); | ||
|
||
driver.SetAllConstraintsMet(Worker.Id); | ||
|
||
var workInfo = (WorkInfo)WorkManager.GetWorkInfoById(Worker.Id).Get(); | ||
Check.That(workInfo.RunAttemptCount).IsEqualTo(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Androidx.Work.Testing; | ||
using NFluent; | ||
using NUnit.Framework; | ||
|
||
namespace AndroidX.Work.Testing.UnitTests | ||
{ | ||
|
||
[TestFixture] | ||
public class TestSuccessWorker : TestWorker | ||
{ | ||
public PeriodicWorkRequest Worker { get; private set; } | ||
|
||
public IOperation Operation { get; private set; } | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
Worker = SuccessWorker.Build(); | ||
Operation = WorkManager.EnqueueUniquePeriodicWork("Work", ExistingPeriodicWorkPolicy.Update, Worker); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
WorkManager.CancelUniqueWork("Work"); | ||
} | ||
|
||
[Test] | ||
public void Success() | ||
{ | ||
var driver = WorkManagerTestInitHelper.GetTestDriver(Context); | ||
|
||
driver.SetAllConstraintsMet(Worker.Id); | ||
|
||
var workInfo = (WorkInfo)WorkManager.GetWorkInfoById(Worker.Id).Get(); | ||
Check.That(workInfo.RunAttemptCount).IsEqualTo(0); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Android.Content; | ||
using Androidx.Work.Testing; | ||
using NUnit.Framework; | ||
|
||
namespace AndroidX.Work.Testing.UnitTests | ||
{ | ||
public class TestWorker | ||
{ | ||
public Context Context { get; private set; } | ||
public WorkManager WorkManager { get; private set; } | ||
|
||
[TestFixtureSetUp] | ||
public void OneTimeSetup() | ||
{ | ||
Context = Application.Context; | ||
WorkManagerTestInitHelper.InitializeTestWorkManager(Context); | ||
WorkManager = WorkManager.GetInstance(Context); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters