Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions build-tools/automation/azure-pipelines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ extends:
artifactSource: bin/Test$(XA.Build.Configuration)/$(DotNetTargetFramework)-android/Mono.Android.NET_Tests-Signed.aab
artifactFolder: $(DotNetTargetFramework)-AotLlvm

- template: /build-tools/automation/yaml-templates/apk-instrumentation.yaml@self
parameters:
configuration: $(XA.Build.Configuration)
testName: Mono.Android.NET_Tests-IsAssignableFrom
project: tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj
testResultsFiles: TestResult-Mono.Android.NET_Tests-$(XA.Build.Configuration)IsAssignableFrom.xml
extraBuildArgs: -p:TestsFlavor=IsAssignableFrom -p:IncludeCategories=Intune -p:_AndroidIsAssignableFromCheck=false
artifactSource: bin/Test$(XA.Build.Configuration)/$(DotNetTargetFramework)-android/Mono.Android.NET_Tests-Signed.aab
artifactFolder: $(DotNetTargetFramework)-IsAssignableFrom

- template: /build-tools/automation/yaml-templates/apk-instrumentation.yaml@self
parameters:
configuration: $(XA.Build.Configuration)
Expand Down
17 changes: 16 additions & 1 deletion src/Mono.Android/Java.Interop/TypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public static Dictionary<Type, string> ManagedToJni {
}

public static partial class TypeManager {

const bool IsAssignableFromCheckEnabledByDefault = true;

const string FeatureSwitchPrefix = "Microsoft.Android.Runtime.RuntimeFeature.";

[FeatureSwitchDefinition ($"{FeatureSwitchPrefix}{nameof (IsAssignableFromCheck)}")]
internal static bool IsAssignableFromCheck { get; } =
AppContext.TryGetSwitch ($"{FeatureSwitchPrefix}{nameof (IsAssignableFromCheck)}", out bool isEnabled) ? isEnabled : IsAssignableFromCheckEnabledByDefault;

internal static string GetClassName (IntPtr class_ptr)
{
IntPtr ptr = RuntimeNativeMethods.monodroid_TypeManager_get_java_class_name (class_ptr);
Expand Down Expand Up @@ -336,7 +345,13 @@ internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership

handleClass = JniEnvironment.Types.GetObjectClass (new JniObjectReference (handle));
if (!JniEnvironment.Types.IsAssignableFrom (handleClass, typeClass)) {
return null;
if (Logger.LogAssembly) {
var message = $"Handle 0x{handle:x} is of type '{JNIEnv.GetClassNameFromInstance (handle)}' which is not assignable to '{typeSig.SimpleReference}'";
Logger.Log (LogLevel.Debug, "monodroid-assembly", message);
}
if (IsAssignableFromCheck) {
return null;
}
}
} finally {
JniObjectReference.Dispose (ref handleClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ This file contains the .NET 5-specific targets to customize ILLink

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<ItemGroup>
<RuntimeHostConfigurationOption Include="Microsoft.Android.Runtime.RuntimeFeature.IsAssignableFromCheck"
Condition="'$(_AndroidIsAssignableFromCheck)' != ''"
Value="$(_AndroidIsAssignableFromCheck)"
Trim="true"
/>
Comment on lines +13 to +17
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to set even in Debug mode.

</ItemGroup>

<Target Name="_PrepareLinking"
Condition=" '$(PublishTrimmed)' == 'true' "
AfterTargets="ComputeResolvedFilesToPublishList"
Expand Down
24 changes: 24 additions & 0 deletions tests/Mono.Android-Tests/Android.Views/LayoutInflaterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using Android.App;
using Android.Views;
using Java.Interop;

using NUnit.Framework;

namespace Android.ViewsTests;

[TestFixture]
public class LayoutInflaterTest
{
[Test]
[Category ("Intune")]
public void From ()
{
Console.WriteLine ($"{nameof (LayoutInflaterTest)}: TypeManager.IsAssignableFromCheck={TypeManager.IsAssignableFromCheck}");

// See: tests\Mono.Android-Tests\Runtime-Microsoft.Android.Sdk\IsAssignableFromRemaps.xml
// Remapped to "net/dot/android/test/MyLayoutInflater"
var from = LayoutInflater.From (Application.Context);
Assert.IsNotNull (from);
}
}
4 changes: 2 additions & 2 deletions tests/Mono.Android-Tests/Mono.Android-Test.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\JnienvArrayMarshaling.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\XmlReaderPullParserTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Runtime\XmlReaderResourceParserTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Views\LayoutInflaterTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Android.Widget\AdapterTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\JavaConvertTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Java.Interop\JavaListTest.cs" />
Expand Down Expand Up @@ -122,8 +123,7 @@
</ItemGroup>

<ItemGroup>
<AndroidJavaSource Include="$(MSBuildThisFileDirectory)java/net/dot/android/test/ValueProvider.java" />
<AndroidJavaSource Include="$(MSBuildThisFileDirectory)java/net/dot/android/test/Example.java" />
<AndroidJavaSource Include="$(MSBuildThisFileDirectory)java/net/dot/android/test/*.java" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<replacements>
<replace-type
from="android/view/LayoutInflater"
to="net/dot/android/test/MyLayoutInflater"
/>
</replacements>
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<ItemGroup>
<TrimmerRootAssembly Include="Java.Interop-Tests" RootMode="All" />
<_AndroidRemapMembers Include="Remaps.xml" />
<_AndroidRemapMembers Include="IsAssignableFromRemaps.xml" Condition=" '$(_AndroidIsAssignableFromCheck)' == 'false' " />
Comment on lines 47 to +48
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsAssignableFromRemaps.xml is in its own file, because we have a couple tests that need to use the real Android LayoutInflater.

We run only the Intune category under -p:_AndroidIsAssignableFromCheck=false.

</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.dot.android.test;

public class MyLayoutInflater extends android.view.LayoutInflater {
protected MyLayoutInflater (android.content.Context context) {
super (context);
}

@Override
public android.view.LayoutInflater cloneInContext (android.content.Context newContext) {
return new MyLayoutInflater (newContext);
}
}
Loading