Skip to content

Commit

Permalink
Android 单元测试 & 增强用于存储空间的文本阅读器
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Nov 26, 2021
1 parent fd6591f commit 7921441
Show file tree
Hide file tree
Showing 27 changed files with 16,316 additions and 7,327 deletions.
37 changes: 34 additions & 3 deletions SteamToolsV2+.sln
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.ClientLib.Droid", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.ClientLib.iOS", "src\Common.ClientLib.iOS\Common.ClientLib.iOS.csproj", "{4658B02A-CB1A-4561-AD9C-3B3FF969D7F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.UnitTest.Droid", "tests\Common.UnitTest.Droid\Common.UnitTest.Droid.csproj", "{80E6762E-AB43-41E2-BDB4-7A3E24713977}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ST.Client.UnitTest.Droid", "tests\Common.UnitTest.Droid\ST.Client.UnitTest.Droid.csproj", "{80E6762E-AB43-41E2-BDB4-7A3E24713977}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.UnitTest.Droid.App", "tests\Common.UnitTest.Droid.App\Common.UnitTest.Droid.App.csproj", "{AD7B78B0-AE51-4FB7-9B11-4E22A2B538CA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ST.Client.UnitTest.Droid.App", "tests\Common.UnitTest.Droid.App\ST.Client.UnitTest.Droid.App.csproj", "{AD7B78B0-AE51-4FB7-9B11-4E22A2B538CA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ST.Client.Desktop.UnitTest", "tests\ST.Client.Desktop.UnitTest\ST.Client.Desktop.UnitTest.csproj", "{5D9E0EAA-2419-426B-BAB4-EA496C1FF7E9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ST.Client.UnitTest.Resources", "tests\ST.Client.UnitTest.Resources\ST.Client.UnitTest.Resources.csproj", "{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1731,6 +1733,34 @@ Global
{5D9E0EAA-2419-426B-BAB4-EA496C1FF7E9}.Release|x64.Build.0 = Release|Any CPU
{5D9E0EAA-2419-426B-BAB4-EA496C1FF7E9}.Release|x86.ActiveCfg = Release|Any CPU
{5D9E0EAA-2419-426B-BAB4-EA496C1FF7E9}.Release|x86.Build.0 = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|ARM.ActiveCfg = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|ARM.Build.0 = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|ARM64.Build.0 = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|iPhone.Build.0 = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|x64.ActiveCfg = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|x64.Build.0 = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|x86.ActiveCfg = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Debug|x86.Build.0 = Debug|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|Any CPU.Build.0 = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|ARM.ActiveCfg = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|ARM.Build.0 = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|ARM64.ActiveCfg = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|ARM64.Build.0 = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|iPhone.ActiveCfg = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|iPhone.Build.0 = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|x64.ActiveCfg = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|x64.Build.0 = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|x86.ActiveCfg = Release|Any CPU
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1792,9 +1822,10 @@ Global
{80E6762E-AB43-41E2-BDB4-7A3E24713977} = {CC46B486-00D7-49E6-ABA3-FDF8F4868D2B}
{AD7B78B0-AE51-4FB7-9B11-4E22A2B538CA} = {CC46B486-00D7-49E6-ABA3-FDF8F4868D2B}
{5D9E0EAA-2419-426B-BAB4-EA496C1FF7E9} = {CC46B486-00D7-49E6-ABA3-FDF8F4868D2B}
{FB2CA9B4-71F1-48E8-B9AE-FF4375F4FE43} = {CC46B486-00D7-49E6-ABA3-FDF8F4868D2B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9313F2B4-10DA-43AA-A481-CAA7571C740C}
RESX_NeutralResourcesLanguage = zh-Hans
SolutionGuid = {9313F2B4-10DA-43AA-A481-CAA7571C740C}
EndGlobalSection
EndGlobal
3 changes: 2 additions & 1 deletion src/Common.CoreLib/Common.CoreLib.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<!--<TargetFrameworks>netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>-->
<TargetFrameworks>netstandard2.1;net6.0</TargetFrameworks>
<RootNamespace>System</RootNamespace>
<AssemblyName>System.Common.CoreLib</AssemblyName>
</PropertyGroup>
Expand Down
24 changes: 24 additions & 0 deletions src/Common.CoreLib/FileEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ public static class FileEx

public const string HTML = ".html";

public const string HTM = ".htm";

public const string CSS = ".css";

public const string SCSS = ".scss";

public const string LESS = ".less";

public const string JS = ".js";

public const string SVG = ".svg";
Expand Down Expand Up @@ -99,6 +105,10 @@ public static class FileEx

public const string CMD = ".cmd";

public const string BAT = ".bat";

public const string XML = ".xml";

public static string Clean(string extension, bool trimLeadingPeriod = false)
{
if (string.IsNullOrWhiteSpace(extension))
Expand All @@ -112,5 +122,19 @@ public static string Clean(string extension, bool trimLeadingPeriod = false)

return extension;
}

public static bool IsSupportedTextReader(string extension) =>
string.Equals(extension, JSON, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, TXT, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, LOG, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, HTML, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, HTM, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, JS, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, CSS, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, SCSS, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, LESS, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, CMD, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, BAT, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, XML, StringComparison.OrdinalIgnoreCase);
}
}
7 changes: 5 additions & 2 deletions src/Common.CoreLib/Properties/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using static System.Properties.ThisAssembly;

[assembly: InternalsVisibleTo("System.Common.UnitTest" + PublicKey)]
[assembly: InternalsVisibleTo("System.Common.UnitTest.iOS" + PublicKey)]
[assembly: InternalsVisibleTo("System.Common.UnitTest.Droid" + PublicKey)]
[assembly: InternalsVisibleTo("System.Common.UnitTest.Droid" + PublicKey)]
[assembly: InternalsVisibleTo("System.Application.SteamTools.Client.UnitTest" + PublicKey)]
[assembly: InternalsVisibleTo("System.Application.SteamTools.Client.Desktop.UnitTest" + PublicKey)]
[assembly: InternalsVisibleTo("System.Application.SteamTools.Client.Droid.UnitTest" + PublicKey)]
16 changes: 12 additions & 4 deletions src/ST.Client.Android/GoToPlatformPages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ static void StartActivity(this Activity activity, Type activityType, byte[] view
var value = Serializable.DMP<TViewModel>(byteArray);
return value;
}
catch
catch (Exception e)
{
Log.Warn(nameof(GoToPlatformPages), e, "GetViewModel fail, type: {0}, activity: {1}", typeof(TViewModel), activity.GetType());
}
}
}
Expand All @@ -85,6 +86,9 @@ public static void StartActivity(Activity activity, Type activityType, object vi
{
viewModelType ??= viewModel.GetType();
var viewModel_ = Serializable.SMP(viewModelType, viewModel);
#if DEBUG
var t = Serializable.DMP(viewModelType, viewModel_);
#endif
StartActivity(activity, activityType, viewModel_);
}

Expand All @@ -110,6 +114,9 @@ public static void StartActivity<TActivity>(Fragment fragment, object viewModel,
public static void StartActivity<TViewModel>(Activity activity, Type activityType, TViewModel viewModel)
{
var viewModel_ = Serializable.SMP(viewModel);
#if DEBUG
var t = Serializable.DMP(typeof(TViewModel), viewModel_);
#endif
StartActivity(activity, activityType, viewModel_);
}

Expand All @@ -132,7 +139,7 @@ public static void StartActivity<TActivity, TViewModel>(Fragment fragment, TView
StartActivity<TActivity, TViewModel>(activity, viewModel);
}

public static void OpenFile(Context context, JFile apkFile, string mime)
public static bool OpenFile(Context context, JFile apkFile, string mime)
{
try
{
Expand All @@ -151,14 +158,15 @@ public static void OpenFile(Context context, JFile apkFile, string mime)
intent.SetDataAndType(apkUri, mime);
intent.AddFlags(ActivityFlags.NewTask);
context.StartActivity(intent);
return true;
}
catch
{

return false;
}
}

public static void InstallApk(Context context, JFile apkFile) => OpenFile(context, apkFile, MediaTypeNames.APK);
public static bool InstallApk(Context context, JFile apkFile) => OpenFile(context, apkFile, MediaTypeNames.APK);

public static void InstallApk(Context context, string apkFilePath)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using XEPlatform = Xamarin.Essentials.Platform;
using AndroidApplication = Android.App.Application;
using AndroidX.AppCompat.App;
using System.Application.UI.Activities;

namespace System.Application.Services.Implementation
{
Expand All @@ -26,9 +27,16 @@ public void SetLightOrDarkThemeFollowingSystem(bool enable)
}

void IPlatformService.OpenFileByTextReader(string filePath)
=> GoToPlatformPages.OpenFile(
XEPlatform.CurrentActivity,
new(filePath),
MediaTypeNames.TXT);
{
var activity = XEPlatform.CurrentActivity;
var result = GoToPlatformPages.OpenFile(
activity,
new(filePath),
MediaTypeNames.TXT);
if (!result)
{
TextBlockActivity.StartActivity(activity, new() { FilePath = filePath });
}
}
}
}
47 changes: 40 additions & 7 deletions src/ST.Client.Android/UI/Activities/TextBlockActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,23 @@ namespace System.Application.UI.Activities
[IntentFilter(new[] { ActionView }, Categories = new[] { CategoryDefault }, DataMimeType = MediaTypeNames.TXT)]
internal sealed class TextBlockActivity : BaseActivity<activity_textblock>
{
/// <summary>
/// 最大支持文件大小,25MB
/// </summary>
const long MaxFileLength = 26214400;

protected override int? LayoutResource => Resource.Layout.activity_textblock;

static string GetContentByFileInfo(FileInfo fileInfo)
{
if (!fileInfo.Exists) return $"File {fileInfo.FullName} not found.";
if (fileInfo.Length > MaxFileLength) return "File with file size greater than 25MB are not supported.";
var fileContent = File.ReadAllText(fileInfo.FullName);
return fileContent;
}

static string GetContentByFilePath(string filePath) => GetContentByFileInfo(new(filePath));

protected override void OnCreate2(Bundle? savedInstanceState)
{
base.OnCreate2(savedInstanceState);
Expand All @@ -33,16 +48,18 @@ protected override void OnCreate2(Bundle? savedInstanceState)
try
{
var filePath = Intent.Data.EnsurePhysicalPath();
var fileContent = File.ReadAllText(filePath);
vm = new TextBlockViewModel
if (!string.IsNullOrEmpty(filePath))
{
Title = Path.GetFileName(filePath),
Content = fileContent,
};
vm = new TextBlockViewModel
{
Title = Path.GetFileName(filePath),
Content = GetContentByFilePath(filePath!),
};
}
}
catch (Exception e)
{
Log.Error(nameof(TextBlockActivity), nameof(OnCreate), e);
Log.Error(nameof(TextBlockActivity), nameof(OnCreate) + "_Intent.Data.EnsurePhysicalPath", e);
}
}
}
Expand All @@ -55,9 +72,25 @@ protected override void OnCreate2(Bundle? savedInstanceState)

this.SetSupportActionBarWithNavigationClick(binding!.toolbar, true);

if (!string.IsNullOrWhiteSpace(vm.Title)) Title = vm.Title;
var hasVMTitle = !string.IsNullOrWhiteSpace(vm.Title);
if (hasVMTitle) Title = vm.Title;

if (!string.IsNullOrWhiteSpace(vm.Content)) binding!.tvContent.Text = vm.Content;
else if (!string.IsNullOrEmpty(vm.FilePath))
{
try
{
if (!hasVMTitle)
{
Title = vm.Title = Path.GetFileName(vm.FilePath);
}
binding!.tvContent.Text = vm.Content = GetContentByFilePath(vm.FilePath!);
}
catch (Exception e)
{
Log.Error(nameof(TextBlockActivity), nameof(OnCreate) + "_vm.FilePath", e);
}
}
else if (vm.ContentSource != default) binding!.tvContent.Text = vm.ContentSource switch
{
TextBlockViewModel.ContentSourceEnum.OpenSourceLibrary => OpenSourceLibrary.StringValues,
Expand Down
3 changes: 3 additions & 0 deletions src/ST.Client.Android/UI/ViewModels/TextBlockViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class TextBlockViewModel
[MPKey(3)]
public int? FontSizeResId { get; set; }

[MPKey(4)]
public string? FilePath { get; set; }

public enum ContentSourceEnum
{
None,
Expand Down
4 changes: 1 addition & 3 deletions src/ST.Client/UI/ViewModels/Pages/ExplorerPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ public void OnItemClick(PathInfoViewModel pathInfo)
else
{
var extension = Path.GetExtension(pathInfo.FullName);
if (string.Equals(extension, FileEx.JSON, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, FileEx.TXT, StringComparison.OrdinalIgnoreCase) ||
string.Equals(extension, FileEx.LOG, StringComparison.OrdinalIgnoreCase))
if (FileEx.IsSupportedTextReader(extension))
{
IPlatformService.Instance.OpenFileByTextReader(pathInfo.FullName);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ST/Properties/InternalsVisibleTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
using static System.Properties.ThisAssembly;

[assembly: InternalsVisibleTo("System.Application.SteamTools.Client.UnitTest" + PublicKey)]
[assembly: InternalsVisibleTo("System.Application.SteamTools.Client.Desktop.UnitTest" + PublicKey)]
[assembly: InternalsVisibleTo("System.Application.SteamTools.Client.Desktop.UnitTest" + PublicKey)]
[assembly: InternalsVisibleTo("System.Application.SteamTools.Client.Droid.UnitTest" + PublicKey)]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.github.aigiol.steamtools.unittest" android:versionCode="1" android:versionName="1.0.0">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="Steam++(Test)" android:roundIcon="@mipmap/ic_launcher_round2" android:theme="@style/Theme.AppCompat.Light.NoActionBar"></application>
<application android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round2" android:theme="@style/Theme.AppCompat.Light.NoActionBar"></application>
</manifest>
Loading

0 comments on commit 7921441

Please sign in to comment.