Skip to content

Commit d8764e7

Browse files
dellis1972jonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] Fix an issue with actionLayout (#1381)
Our Resource Case converter for `@(AndroidResource)` did not handle the `actionLayout` attribute for the `http://schemas.android.com/apk/res-auto` namespace in menu xml files. As a result the value for the `actionLayout` would not be lowercased. This results in the following error No resource found that matches the given name (at ‘actionLayout’ with value ‘@layout/ServingLayout’). The fix is to add support for `actionLayout` to `@(AndroidResource)`. We also have added a unit test for this particular case and allowed the `Xamarin.Android.Build.Tests` access to the internals of the `Xamarin.Android.Build.Tasks` so it can unit test more of the internal classes it has.
1 parent 93acc40 commit d8764e7

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

src/Xamarin.Android.Build.Tasks/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@
3636
[assembly: AssemblyFileVersion ("1.0.0.0")]
3737

3838
[assembly: InternalsVisibleTo ("AndroidMSBuildTests")]
39+
[assembly: InternalsVisibleTo ("Xamarin.Android.Build.Tests")]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
using Xamarin.ProjectTools;
5+
using System.IO;
6+
using System.Linq;
7+
using Microsoft.Build.Framework;
8+
using System.Text;
9+
using Xamarin.Android.Tasks;
10+
using Microsoft.Build.Utilities;
11+
12+
namespace Xamarin.Android.Build.Tests {
13+
[TestFixture]
14+
[Parallelizable (ParallelScope.Self)]
15+
public class AndroidResourceTests : BaseTest {
16+
[Test]
17+
public void MenuActionLayout ()
18+
{
19+
var path = Path.Combine (Root, "temp", "MenuActionLayout");
20+
Directory.CreateDirectory (path);
21+
var layoutDir = Path.Combine (path, "res", "layout");
22+
var menuDir = Path.Combine (path, "res", "menu");
23+
Directory.CreateDirectory (layoutDir);
24+
Directory.CreateDirectory (menuDir);
25+
File.WriteAllText (Path.Combine (layoutDir, "servinglayout.xml"), @"<?xml version=""1.0"" encoding=""utf-8""?>
26+
<LinearLayout xmlns:android=""http://schemas.android.com/apk/res/android""
27+
android:orientation = ""horizontal""
28+
android:layout_width = ""match_parent""
29+
android:layout_height = ""match_parent"" >");
30+
31+
var actions = Path.Combine (menuDir, "actions.xml");
32+
File.WriteAllText (actions, @"<?xml version=""1.0"" encoding=""utf-8""?>
33+
<menu
34+
xmlns:android = ""http://schemas.android.com/apk/res/android""
35+
xmlns:app = ""http://schemas.android.com/apk/res-auto"">
36+
<item
37+
android:id = ""@+id/addToFavorites""
38+
android:title = ""Add to Favorites""
39+
app:showAsAction = ""always""
40+
/>
41+
<item
42+
android:title = ""Servings""
43+
android:icon = ""@drawable/ic_people_white_24dp""
44+
app:showAsAction = ""always"">
45+
<menu>
46+
<group android:checkableBehavior = ""single"">
47+
48+
<item
49+
android:id = ""@+id/oneServing""
50+
android:title = ""1 serving""
51+
android:checked= ""true""
52+
app:actionLayout = ""@layout/ServingLayout""
53+
/>
54+
<item
55+
android:id = ""@+id/twoServings""
56+
android:title = ""2 servings"" />
57+
<item
58+
android:id = ""@+id/fourServings""
59+
android:title = ""4 servings"" />
60+
</group>
61+
</menu>
62+
</item>
63+
<item
64+
android:id = ""@+id/about""
65+
android:title = ""About""
66+
app:showAsAction = ""never"" />
67+
68+
</menu>");
69+
Monodroid.AndroidResource.UpdateXmlResource (actions, new Dictionary<string, string> (), null);
70+
var actionsText = File.ReadAllText (actions);
71+
Assert.True (actionsText.Contains ("@layout/servinglayout"), "'@layout/ServingLayout' was not converted to '@layout/servinglayout'");
72+
Directory.Delete (path, recursive: true);
73+
}
74+
}
75+
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Xamarin.Android.Build.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,6 @@
7474
<Compile Include="AndroidRegExTests.cs" />
7575
<Compile Include="GetDependenciesTests.cs" />
7676
<Compile Include="ResolveSdksTaskTests.cs" />
77+
<Compile Include="AndroidResourceTests.cs" />
7778
</ItemGroup>
7879
</Project>

src/Xamarin.Android.Build.Tasks/Utilities/AndroidResource.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ private static bool TryFixResAuto (XAttribute attr, Dictionary<string, string> a
213213
switch (attr.Name.LocalName) {
214214
case "rectLayout":
215215
case "roundLayout":
216+
case "actionLayout":
216217
attr.Value = attr.Value.ToLowerInvariant ();
217218
return true;
218219
}

0 commit comments

Comments
 (0)