-
Notifications
You must be signed in to change notification settings - Fork 62
Auto-generate crc64 package name when package is empty in XmlImporter #1376
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
Draft
Copilot
wants to merge
6
commits into
main
Choose a base branch
from
copilot/fix-java-interop-blank-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+195
−13
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a1f174c
Initial plan
Copilot 23737bb
Auto-generate crc64 package name when package is empty in XmlImporter
Copilot f16fe0f
Remove unused using directive
Copilot da6d443
Use existing JavaNativeTypeManager.GetPackageName instead of duplicat…
Copilot 0e2df31
Apply suggestion from @jonathanpeppers
jonathanpeppers 2feda94
Add SetUp and TearDown to XmlImporterTests to save/restore PackageNam…
Copilot 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
144 changes: 144 additions & 0 deletions
144
...ls.JavaCallableWrappers-Tests/Java.Interop.Tools.JavaCallableWrappers/XmlImporterTests.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,144 @@ | ||
| using System.IO; | ||
| using System.Xml.Linq; | ||
| using Java.Interop.Tools.JavaCallableWrappers.Adapters; | ||
| using Java.Interop.Tools.TypeNameMappings; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Java.Interop.Tools.JavaCallableWrappersTests | ||
| { | ||
| [TestFixture] | ||
| public class XmlImporterTests | ||
| { | ||
| PackageNamingPolicy existingValue; | ||
|
|
||
| [SetUp] | ||
| public void SetUp () | ||
| { | ||
| existingValue = JavaNativeTypeManager.PackageNamingPolicy; | ||
| JavaNativeTypeManager.PackageNamingPolicy = PackageNamingPolicy.LowercaseCrc64; | ||
| } | ||
|
|
||
| [TearDown] | ||
| public void TearDown () | ||
| { | ||
| JavaNativeTypeManager.PackageNamingPolicy = existingValue; | ||
| } | ||
|
|
||
| [Test] | ||
| public void ImportType_EmptyPackage_GeneratesCrc64Package () | ||
| { | ||
| // Simulate the case where [Register("CustomView")] is used without a package prefix | ||
| // The XML has an empty package attribute, and we expect XmlImporter to generate a crc64 package | ||
| var xml = XElement.Parse (@" | ||
| <type name=""CustomView"" | ||
| package="""" | ||
| partial_assembly_qualified_name=""MyApp.Views.CustomView, MyApp.Android"" | ||
| extends_type=""android.view.View""> | ||
| <constructors /> | ||
| <methods /> | ||
| </type>"); | ||
|
|
||
| var type = XmlImporter.ImportType (xml); | ||
|
|
||
| // The generated package should be "crc64" + crc64hash of "MyApp.Views:MyApp.Android" | ||
| Assert.AreEqual ("crc64b8e52a012da2d805", type.Package); | ||
| Assert.AreEqual ("CustomView", type.Name); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ImportType_EmptyPackage_NestedType_GeneratesCrc64Package () | ||
| { | ||
| // Simulate the case with a nested type | ||
| var xml = XElement.Parse (@" | ||
| <type name=""InnerView"" | ||
| package="""" | ||
| partial_assembly_qualified_name=""MyApp.Views.OuterView+InnerView, MyApp.Android"" | ||
| extends_type=""android.view.View""> | ||
| <constructors /> | ||
| <methods /> | ||
| </type>"); | ||
|
|
||
| var type = XmlImporter.ImportType (xml); | ||
|
|
||
| // The generated package should be based on "MyApp.Views:MyApp.Android" (ignoring nested type part) | ||
| Assert.AreEqual ("crc64b8e52a012da2d805", type.Package); | ||
| Assert.AreEqual ("InnerView", type.Name); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ImportType_MissingPackageAttribute_GeneratesCrc64Package () | ||
| { | ||
| // Simulate the case where the package attribute is completely missing | ||
| var xml = XElement.Parse (@" | ||
| <type name=""CustomView"" | ||
| partial_assembly_qualified_name=""MyApp.Views.CustomView, MyApp.Android"" | ||
| extends_type=""android.view.View""> | ||
| <constructors /> | ||
| <methods /> | ||
| </type>"); | ||
|
|
||
| var type = XmlImporter.ImportType (xml); | ||
|
|
||
| // The generated package should be "crc64" + crc64hash of "MyApp.Views:MyApp.Android" | ||
| Assert.AreEqual ("crc64b8e52a012da2d805", type.Package); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ImportType_WithPackage_UsesProvidedPackage () | ||
| { | ||
| // Ensure that when a package is provided, it is used directly | ||
| var xml = XElement.Parse (@" | ||
| <type name=""CustomView"" | ||
| package=""my.custom.package"" | ||
| partial_assembly_qualified_name=""MyApp.Views.CustomView, MyApp.Android"" | ||
| extends_type=""android.view.View""> | ||
| <constructors /> | ||
| <methods /> | ||
| </type>"); | ||
|
|
||
| var type = XmlImporter.ImportType (xml); | ||
|
|
||
| Assert.AreEqual ("my.custom.package", type.Package); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ImportType_NoNamespace_ReturnsEmptyPackage () | ||
| { | ||
| // When there's no namespace in the type name, we can't generate a package | ||
| var xml = XElement.Parse (@" | ||
| <type name=""CustomView"" | ||
| package="""" | ||
| partial_assembly_qualified_name=""CustomView, MyApp.Android"" | ||
| extends_type=""android.view.View""> | ||
| <constructors /> | ||
| <methods /> | ||
| </type>"); | ||
|
|
||
| var type = XmlImporter.ImportType (xml); | ||
|
|
||
| // Without a namespace, we can't generate a crc64 package, so it should remain empty | ||
| Assert.AreEqual ("", type.Package); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ImportType_XamarinAndroidToolsTests_MatchesExpectedCrc64 () | ||
| { | ||
| // This test verifies the crc64 generation matches what JavaNativeTypeManager would generate | ||
| // Based on the test data in JavaCallableWrapperGeneratorTests, namespace "Xamarin.Android.ToolsTests" | ||
| // with assembly "Java.Interop.Tools.JavaCallableWrappers-Tests" should produce crc64197ae30a36756915 | ||
| var xml = XElement.Parse (@" | ||
| <type name=""ExportsMembers"" | ||
| package="""" | ||
| partial_assembly_qualified_name=""Xamarin.Android.ToolsTests.ExportsMembers, Java.Interop.Tools.JavaCallableWrappers-Tests"" | ||
| extends_type=""java.lang.Object""> | ||
| <constructors /> | ||
| <methods /> | ||
| </type>"); | ||
|
|
||
| var type = XmlImporter.ImportType (xml); | ||
|
|
||
| // This should match the expected crc64 from JavaCallableWrapperGeneratorTests | ||
| Assert.AreEqual ("crc64197ae30a36756915", type.Package); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests in this class depend on JavaNativeTypeManager.PackageNamingPolicy being set to LowercaseCrc64, but they don't explicitly set or restore this value. If other tests modify this static property, these tests could fail. Consider adding SetUp and TearDown methods to save and restore the PackageNamingPolicy, similar to what JavaNativeTypeManagerTests does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot can you follow your own suggestion here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added SetUp and TearDown methods to save and restore PackageNamingPolicy, following the pattern from JavaNativeTypeManagerTests. See commit 2feda94.