-
Notifications
You must be signed in to change notification settings - Fork 578
[Xamarin.Android.Build.Tasks] merge targetSdkVersion into existing <uses-sdk>
#12290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+114
−11
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/ManifestDocumentTest.cs
This file contains hidden or 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,113 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Xml.Linq; | ||
| using Java.Interop.Tools.Cecil; | ||
| using Microsoft.Build.Utilities; | ||
| using Mono.Cecil; | ||
| using NUnit.Framework; | ||
| using Xamarin.Android.Tasks; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests | ||
| { | ||
| /// <summary> | ||
| /// In-process tests for how <see cref="ManifestDocument"/> merges the computed | ||
| /// minSdkVersion/targetSdkVersion into a user-authored <uses-sdk/> element. | ||
| /// </summary> | ||
| [TestFixture] | ||
| [Parallelizable (ParallelScope.Self)] | ||
| public class ManifestDocumentTest : BaseTest | ||
| { | ||
| const string DefaultMinSdkVersion = "21"; | ||
| const string DefaultTargetSdkVersion = "37"; | ||
|
|
||
| static readonly XNamespace AndroidNs = "http://schemas.android.com/apk/res/android"; | ||
|
|
||
| static string CreateManifest (string usesSdk) => $@"<?xml version=""1.0"" encoding=""utf-8""?> | ||
| <manifest xmlns:android=""http://schemas.android.com/apk/res/android"" package=""com.xamarin.usessdk""> | ||
| {usesSdk} | ||
| <application android:label=""UsesSdk""> | ||
| </application> | ||
| </manifest>"; | ||
|
|
||
| static XElement MergeAndGetUsesSdk (string usesSdk, out XDocument document) | ||
| { | ||
| var templateFile = Path.GetTempFileName (); | ||
| try { | ||
| File.WriteAllText (templateFile, CreateManifest (usesSdk)); | ||
|
|
||
| var manifest = new ManifestDocument (templateFile) { | ||
| PackageName = "com.xamarin.usessdk", | ||
| TargetSdkVersion = DefaultTargetSdkVersion, | ||
| MinSdkVersion = DefaultMinSdkVersion, | ||
| VersionResolver = new MockVersionResolver { | ||
| GetApiLevelFromIdFunc = id => int.TryParse (id, out var apiLevel) ? (int?) apiLevel : null, | ||
| GetIdFromApiLevelFunc = apiLevel => apiLevel, | ||
| }, | ||
| }; | ||
|
|
||
| var engine = new MockBuildEngine (TestContext.Out); | ||
| var log = new TaskLoggingHelper (engine, nameof (ManifestDocumentTest)); | ||
| manifest.Merge (log, new TypeDefinitionCache (), new List<TypeDefinition> (), applicationClass: null, embed: false, bundledWearApplicationName: null, mergedManifestDocuments: null); | ||
|
|
||
| var writer = new StringWriter (); | ||
| manifest.Save ((code, message) => { }, writer); | ||
|
|
||
| document = XDocument.Parse (writer.ToString ()); | ||
| var element = document.Root.Element ("uses-sdk"); | ||
| Assert.IsNotNull (element, "uses-sdk element should exist"); | ||
| return element; | ||
| } finally { | ||
| File.Delete (templateFile); | ||
| } | ||
| } | ||
|
|
||
| static void AssertUsesSdk (string usesSdk, string expectedMinSdkVersion, string expectedTargetSdkVersion) | ||
| { | ||
| var element = MergeAndGetUsesSdk (usesSdk, out var document); | ||
|
|
||
| Assert.AreEqual (expectedMinSdkVersion, element.Attribute (AndroidNs + "minSdkVersion")?.Value, "unexpected android:minSdkVersion"); | ||
| Assert.AreEqual (expectedTargetSdkVersion, element.Attribute (AndroidNs + "targetSdkVersion")?.Value, "unexpected android:targetSdkVersion"); | ||
|
|
||
| foreach (var comment in document.DescendantNodes ().OfType<XComment> ()) { | ||
| StringAssert.DoesNotContain ("UsesMinSdkAttributes", comment.Value, "the lint suppression comment should no longer be emitted"); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void NoUsesSdkElement () | ||
| { | ||
| AssertUsesSdk ("", DefaultMinSdkVersion, DefaultTargetSdkVersion); | ||
| } | ||
|
|
||
| [Test] | ||
| public void MinSdkVersionOnly () | ||
| { | ||
| // Regression test: targetSdkVersion was never written, so `aapt2` defaulted it to minSdkVersion. | ||
| AssertUsesSdk (@"<uses-sdk android:minSdkVersion=""24"" />", "24", DefaultTargetSdkVersion); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TargetSdkVersionOnly () | ||
| { | ||
| AssertUsesSdk (@"<uses-sdk android:targetSdkVersion=""30"" />", DefaultMinSdkVersion, "30"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void MinAndTargetSdkVersion () | ||
| { | ||
| AssertUsesSdk (@"<uses-sdk android:minSdkVersion=""24"" android:targetSdkVersion=""30"" />", "24", "30"); | ||
| } | ||
| } | ||
|
|
||
| class MockVersionResolver : IVersionResolver | ||
| { | ||
| public Func<string, int?> GetApiLevelFromIdFunc { get; set; } = _ => 99; | ||
| public Func<string, string> GetIdFromApiLevelFunc { get; set; } = _ => "API-99"; | ||
|
|
||
| public int? GetApiLevelFromId (string id) => GetApiLevelFromIdFunc (id); | ||
|
|
||
| public string GetIdFromApiLevel (string apiLevel) => GetIdFromApiLevelFunc (apiLevel); | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.