Skip to content

Commit

Permalink
Add Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
dellis1972 committed Sep 7, 2022
1 parent 177f987 commit e9f9259
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
2 changes: 0 additions & 2 deletions samples/HelloWorld/HelloLibrary/LibraryActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ protected override void OnCreate(Bundle bundle)
base.OnCreate(bundle);

// Create your application here
var a = ObtainStyledAttributes(Resource.Styleable.MyLibraryWidget);
bool value = a.GetBoolean (Resource.Styleable.MyLibraryWidget_library_bool_attr, defValue: false);
}
}

Expand Down
3 changes: 0 additions & 3 deletions samples/HelloWorld/HelloWorld/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ protected override void OnCreate (Bundle savedInstanceState)
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);

var a = ObtainStyledAttributes(Resource.Styleable.MyWidget);
bool value = a.GetBoolean (0, defValue: false);

button.Click += delegate {
button.Text = string.Format ("{0} clicks!", (byte)count++);
};
Expand Down
60 changes: 60 additions & 0 deletions tests/MSBuildDeviceIntegration/Tests/InstallAndRunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -684,5 +684,65 @@ public void SingleProject_ApplicationId ()
Path.Combine (Root, builder.ProjectDirectory, "startup-logcat.log"));
Assert.IsTrue (didStart, "Activity should have started.");
}

[Test]
public void AppWithStyleableUsageRuns ([Values (true, false)] bool isRelease, [Values (true, false)] bool linkResources)
{
AssertHasDevices ();
proj = new XamarinAndroidApplicationProject () {
IsRelease = isRelease,
};

proj.AndroidResources.Add (new AndroidItem.AndroidResource ("Resources\\values\\styleables.xml") {
TextContent = () => @"<?xml version='1.0' encoding='utf-8'?>
<resources>
<declare-styleable name='MyView'>
<attr name='MyBool' format='boolean' />
<attr name='MyInt' format='integer' />
</declare-styleable>
</resources>",
});
proj.SetProperty ("AndroidLinkResources", linkResources ? "False" : "True");
proj.LayoutMain = proj.LayoutMain.Replace ("<LinearLayout", "<UnnamedProject.MyLayout xmlns:app='http://schemas.android.com/apk/res-auto' app:MyBool='true' app:MyInt='128'")
.Replace ("</LinearLayout>", "</UnnamedProject.MyLayout>");

proj.MainActivity = proj.DefaultMainActivity.Replace ("//${AFTER_MAINACTIVITY}",
@"public class MyLayout : Android.Widget.LinearLayout
{
public MyLayout (Android.Content.Context context, Android.Util.IAttributeSet attrs) : base (context, attrs)
{
Android.Content.Res.TypedArray a = context.Theme.ObtainStyledAttributes (attrs, Resource.Styleable.MyView, 0,0);
try {
bool b = a.GetBoolean (Resource.Styleable.MyView_MyBool, defValue: false);
if (!b)
throw new Exception (""MyBool was not true."");
int i = a.GetInteger (Resource.Styleable.MyView_MyInt, defValue: -1);
if (i != 128)
throw new Exception (""MyInt was not 128."");
}
finally {
a.Recycle();
}
}
}
");

var abis = new string [] { "armeabi-v7a", "arm64-v8a", "x86", "x86_64" };
proj.SetAndroidSupportedAbis (abis);
builder = CreateApkBuilder ();
Assert.IsTrue (builder.Install (proj), "Install should have succeeded.");

if (Builder.UseDotNet)
Assert.True (builder.RunTarget (proj, "Run"), "Project should have run.");
else if (CommercialBuildAvailable)
Assert.True (builder.RunTarget (proj, "_Run"), "Project should have run.");
else
AdbStartActivity ($"{proj.PackageName}/{proj.JavaPackageName}.MainActivity");

var didStart = WaitForActivityToStart (proj.PackageName, "MainActivity",
Path.Combine (Root, builder.ProjectDirectory, "startup-logcat.log"));
Assert.IsTrue (didStart, "Activity should have started.");
}
}
}

0 comments on commit e9f9259

Please sign in to comment.