Skip to content

Commit fea09b8

Browse files
committed
Added UITests project
Added external for test purposes Added write permission for testing
1 parent 5c236f2 commit fea09b8

File tree

8 files changed

+162
-25
lines changed

8 files changed

+162
-25
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using Xamarin.UITest;
3+
using Xamarin.UITest.Queries;
4+
5+
namespace BasicPlainTextReaderApp.UITests
6+
{
7+
public class AppInitializer
8+
{
9+
public static IApp StartApp(Platform platform)
10+
{
11+
if (platform == Platform.Android)
12+
{
13+
return ConfigureApp.Android.InstalledApp("dev.jonwolf.basicplaintextreaderapp").StartApp();
14+
}
15+
16+
return ConfigureApp.iOS.StartApp();
17+
}
18+
}
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{F7E12857-6DFB-4327-9203-8BF1EEFA7B2F}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<RootNamespace>BasicPlainTextReaderApp.UITests</RootNamespace>
9+
<AssemblyName>BasicPlainTextReaderApp.UITests</AssemblyName>
10+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
11+
</PropertyGroup>
12+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
13+
<DebugSymbols>true</DebugSymbols>
14+
<DebugType>full</DebugType>
15+
<Optimize>false</Optimize>
16+
<OutputPath>bin\Debug</OutputPath>
17+
<DefineConstants>DEBUG;</DefineConstants>
18+
<ErrorReport>prompt</ErrorReport>
19+
<WarningLevel>4</WarningLevel>
20+
</PropertyGroup>
21+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
22+
<Optimize>true</Optimize>
23+
<OutputPath>bin\Release</OutputPath>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<ItemGroup>
28+
<Reference Include="System" />
29+
</ItemGroup>
30+
<ItemGroup>
31+
<PackageReference Include="NUnit" Version="3.12.0" />
32+
<PackageReference Include="Xamarin.UITest" Version="3.0.5" />
33+
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
34+
</ItemGroup>
35+
<ItemGroup>
36+
<Compile Include="Tests.cs" />
37+
<Compile Include="AppInitializer.cs" />
38+
</ItemGroup>
39+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
40+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using NUnit.Framework;
5+
using Xamarin.UITest;
6+
using Xamarin.UITest.Queries;
7+
8+
namespace BasicPlainTextReaderApp.UITests
9+
{
10+
[TestFixture(Platform.Android)]
11+
//[TestFixture(Platform.iOS)]
12+
public class Tests
13+
{
14+
IApp app;
15+
Platform platform;
16+
17+
public Tests(Platform platform)
18+
{
19+
this.platform = platform;
20+
}
21+
22+
[SetUp]
23+
public void BeforeEachTest()
24+
{
25+
app = AppInitializer.StartApp(platform);
26+
}
27+
28+
[Test]
29+
public void WelcomeTextIsDisplayed()
30+
{
31+
AppResult[] results = app.WaitForElement(c => c.Marked("Basic Text Reader App"));
32+
//app.Screenshot("Welcome screen.");
33+
34+
Assert.IsTrue(results.Any());
35+
36+
app.Invoke("OpenTextFile");
37+
AppResult[] results2 = app.WaitForElement(c => c.Id("InfoItemAi"));
38+
}
39+
40+
41+
}
42+
}

BasicPlainTextReaderApp.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicPlainTextReaderApp.And
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicPlainTextReaderApp", "BasicPlainTextReaderApp\BasicPlainTextReaderApp\BasicPlainTextReaderApp.csproj", "{CA1AEA98-AE84-4763-A402-A993868615F1}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicPlainTextReaderApp.Library", "BasicPlainTextReaderApp.Library\BasicPlainTextReaderApp.Library.csproj", "{F889341B-08D1-4416-A989-D0E03D434B11}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicPlainTextReaderApp.Library", "BasicPlainTextReaderApp.Library\BasicPlainTextReaderApp.Library.csproj", "{F889341B-08D1-4416-A989-D0E03D434B11}"
1111
EndProject
12-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicPlainTextReaderApp.UnitTests", "BasicPlainTextReaderApp.UnitTests\BasicPlainTextReaderApp.UnitTests.csproj", "{CCA174D5-E76D-4A84-B434-99B45252426D}"
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicPlainTextReaderApp.UnitTests", "BasicPlainTextReaderApp.UnitTests\BasicPlainTextReaderApp.UnitTests.csproj", "{CCA174D5-E76D-4A84-B434-99B45252426D}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicPlainTextReaderApp.UITests", "BasicPlainTextReaderApp.UITests\BasicPlainTextReaderApp.UITests.csproj", "{F7E12857-6DFB-4327-9203-8BF1EEFA7B2F}"
1315
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -37,6 +39,10 @@ Global
3739
{CCA174D5-E76D-4A84-B434-99B45252426D}.Debug|Any CPU.Build.0 = Debug|Any CPU
3840
{CCA174D5-E76D-4A84-B434-99B45252426D}.Release|Any CPU.ActiveCfg = Release|Any CPU
3941
{CCA174D5-E76D-4A84-B434-99B45252426D}.Release|Any CPU.Build.0 = Release|Any CPU
42+
{F7E12857-6DFB-4327-9203-8BF1EEFA7B2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
43+
{F7E12857-6DFB-4327-9203-8BF1EEFA7B2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
44+
{F7E12857-6DFB-4327-9203-8BF1EEFA7B2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
45+
{F7E12857-6DFB-4327-9203-8BF1EEFA7B2F}.Release|Any CPU.Build.0 = Release|Any CPU
4046
EndGlobalSection
4147
GlobalSection(SolutionProperties) = preSolution
4248
HideSolutionNode = FALSE

BasicPlainTextReaderApp/BasicPlainTextReaderApp.Android/BasicPlainTextReaderApp.Android.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</PropertyGroup>
4747
<ItemGroup>
4848
<Reference Include="Mono.Android" />
49+
<Reference Include="Mono.Android.Export" />
4950
<Reference Include="System" />
5051
<Reference Include="System.Core" />
5152
<Reference Include="System.Numerics" />

BasicPlainTextReaderApp/BasicPlainTextReaderApp.Android/MainActivity.cs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
using Android.Provider;
1212
using System.Text;
1313
using BasicPlainTextReaderApp.Library;
14+
using Java.Interop;
15+
using Android.Util;
16+
using Java.IO;
1417

1518
namespace BasicPlainTextReaderApp.Droid
1619
{
@@ -24,36 +27,30 @@ public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompa
2427
{
2528
protected override void OnCreate(Bundle savedInstanceState)
2629
{
27-
string action = Intent.Action;
28-
string type = Intent.Type;
29-
3030
TextModel data = null;
31-
32-
if (Intent.ActionView.Equals(action) && !string.IsNullOrEmpty(type))
31+
32+
if (Intent.ActionView.Equals(Intent.Action) && Intent.Data != null && !string.IsNullOrEmpty(Intent.Type))
3333
{
34-
if (Intent.Data != null)
34+
try
3535
{
36-
try
36+
using (var parcelFileDescriptor = ContentResolver.OpenFileDescriptor(Intent.Data, "r"))
37+
using (Java.IO.FileDescriptor fileDescriptor = parcelFileDescriptor.FileDescriptor)
38+
using (var reader = new Java.IO.FileReader(fileDescriptor))
39+
using (var bufferedReader = new Java.IO.BufferedReader(reader))
3740
{
38-
using (var parcelFileDescriptor = ContentResolver.OpenFileDescriptor(Intent.Data, "r"))
39-
using (Java.IO.FileDescriptor fileDescriptor = parcelFileDescriptor.FileDescriptor)
40-
using (var reader = new Java.IO.FileReader(fileDescriptor))
41-
using (var bufferedReader = new Java.IO.BufferedReader(reader))
41+
string line;
42+
var sb = new StringBuilder();
43+
while ((line = bufferedReader.ReadLine()) != null)
4244
{
43-
string line;
44-
var sb = new StringBuilder();
45-
while ((line = bufferedReader.ReadLine()) != null)
46-
{
47-
sb.AppendLine(line);
48-
}
49-
data = new TextModel(sb.ToString(), Intent.DataString, Intent.Type, Intent.Data.Path);
45+
sb.AppendLine(line);
5046
}
51-
}
52-
catch (Exception e)
53-
{
54-
data = new TextModel(e.ToString(), Intent.DataString, Intent.Type, Intent.Data.Path);
47+
data = new TextModel(sb.ToString(), Intent.DataString, Intent.Type, Intent.Data.Path);
5548
}
5649
}
50+
catch (Exception e)
51+
{
52+
data = new TextModel(e.ToString(), Intent.DataString, Intent.Type, Intent.Data.Path);
53+
}
5754
}
5855

5956
TabLayoutResource = Resource.Layout.Tabbar;
@@ -71,5 +68,36 @@ public override void OnRequestPermissionsResult(int requestCode, string[] permis
7168

7269
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
7370
}
71+
72+
[Export("OpenTextFile")]
73+
public void OpenTextFile()
74+
{
75+
var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "test.txt");
76+
if (!System.IO.File.Exists(backingFile))
77+
{
78+
using (var writer = System.IO.File.CreateText(backingFile))
79+
{
80+
writer.WriteLine("Test file\r\nTest file line 2");
81+
writer.Flush();
82+
}
83+
}
84+
85+
//Read file
86+
//using (var reader = new StreamReader(backingFile, true))
87+
//{
88+
// string line;
89+
// while ((line = reader.ReadLine()) != null)
90+
// {
91+
// }
92+
//}
93+
94+
var uri = Android.Net.Uri.Parse("file:///data/user/0/dev.jonwolf.basicplaintextreaderapp/files/test.txt");
95+
var intent2 = new Intent(Intent.ActionView, uri, this, typeof(MainActivity));
96+
97+
intent2.SetDataAndType(uri, "text/plain");
98+
intent2.SetAction(Intent.ActionView);
99+
100+
StartActivity(intent2);
101+
}
74102
}
75103
}

BasicPlainTextReaderApp/BasicPlainTextReaderApp.Android/Properties/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="dev.jonwolf.basicplaintextreaderapp" android:installLocation="auto">
33
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
44
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
5+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
56
<application android:label="Basic Plain Text Reader" android:theme="@style/MainTheme"></application>
67
</manifest>

BasicPlainTextReaderApp/BasicPlainTextReaderApp/Views/AboutPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</StackLayout>
3030
<ScrollView Grid.Row="1">
3131
<StackLayout Orientation="Vertical" Padding="30,24,30,24" Spacing="10">
32-
<Label Text="Basic Text Reader App" FontSize="Title"/>
32+
<Label Text="Basic Text Reader App" FontSize="Title" AutomationId="TitleAboutAi" x:Uid="TitleAboutId" x:Name="TitleAboutName" />
3333
<Label Text="This app tries to read any file as plaint text! And it doesn't save any temporarily hidden file like other text editors do!" FontSize="16" Padding="0,0,0,0"/>
3434
<Label Text="Author" FontSize="Title"/>
3535
<Label Text="Made by @jonwolfdev"/>

0 commit comments

Comments
 (0)