2016-05-26 16:06:52 +03:00
|
|
|
//
|
|
|
|
// Copyright 2015 Xamarin Inc
|
|
|
|
//
|
|
|
|
using System;
|
2020-12-18 13:45:06 +03:00
|
|
|
using System.Collections.Generic;
|
2020-12-15 15:16:07 +03:00
|
|
|
using System.IO;
|
2016-05-26 16:06:52 +03:00
|
|
|
using Foundation;
|
|
|
|
using CoreFoundation;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace MonoTouchFixtures.CoreFoundation {
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
[Preserve (AllMembers = true)]
|
|
|
|
public class BundleTest {
|
2016-05-31 20:15:02 +03:00
|
|
|
#if __WATCHOS__
|
|
|
|
const string ExpectedAppName = "monotouchtest.appex";
|
2021-02-24 18:07:04 +03:00
|
|
|
#elif MONOMAC && !NET
|
2017-01-12 21:13:56 +03:00
|
|
|
const string ExpectedAppName = "xammac_tests.app";
|
2016-05-31 20:15:02 +03:00
|
|
|
#else
|
|
|
|
const string ExpectedAppName = "monotouchtest.app";
|
|
|
|
#endif
|
|
|
|
|
2016-05-26 16:06:52 +03:00
|
|
|
[Test]
|
|
|
|
public void TestGetAll ()
|
|
|
|
{
|
|
|
|
var bundles = CFBundle.GetAll ();
|
|
|
|
Assert.IsTrue (bundles.Length > 0);
|
|
|
|
foreach (CFBundle b in bundles) {
|
|
|
|
Assert.IsFalse (String.IsNullOrEmpty (b.Url.ToString ()),
|
|
|
|
String.Format("Found bundle with null url and id {0}", b.Identifier));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetBundleIdMissing ()
|
|
|
|
{
|
|
|
|
var bundle = CFBundle.Get ("????");
|
|
|
|
Assert.IsNull (bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetBundleId ()
|
|
|
|
{
|
|
|
|
// grab all bundles and make sure we do get the correct ones using their id
|
|
|
|
var bundles = CFBundle.GetAll ();
|
|
|
|
Assert.IsTrue (bundles.Length > 0);
|
2020-12-18 13:45:06 +03:00
|
|
|
|
|
|
|
// There may be multiple apps providing the same bundle ID (the typical example is that we usually have multiple Xcodes installed)
|
|
|
|
// So compute a map for bundle id -> bundle paths that's used in the second part here to verify the CFBundle.Get results.
|
|
|
|
var dict = new Dictionary<string, List<string>> ();
|
|
|
|
foreach (var bundle in bundles) {
|
|
|
|
var id = bundle.Identifier;
|
|
|
|
if (string.IsNullOrEmpty (id))
|
|
|
|
continue;
|
|
|
|
if (!dict.TryGetValue (id, out var list))
|
|
|
|
dict [id] = list = new List<string> ();
|
|
|
|
list.Add ((string) ((NSString) bundle.Url.Path).ResolveSymlinksInPath ());
|
|
|
|
}
|
|
|
|
|
2016-05-26 16:06:52 +03:00
|
|
|
foreach (CFBundle b in bundles) {
|
|
|
|
var id = b.Identifier;
|
|
|
|
if (!String.IsNullOrEmpty (id)) {
|
|
|
|
var otherBundle = CFBundle.Get (id);
|
|
|
|
Assert.AreEqual (b.Info.Type, otherBundle.Info.Type,
|
|
|
|
String.Format("Found bundle with diff type and id {0}", id));
|
2018-07-03 18:19:04 +03:00
|
|
|
var bPath = (string) ((NSString) b.Url.Path).ResolveSymlinksInPath ();
|
2020-12-18 13:45:06 +03:00
|
|
|
var list = dict [id];
|
|
|
|
Assert.That (list, Does.Contain (bPath), "None of the bundles for {0} matches the path {1}", id, bPath);
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestGetBundleIdNull (string id)
|
|
|
|
{
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => CFBundle.Get (id));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetMain ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2016-05-31 20:15:02 +03:00
|
|
|
#if __WATCHOS__
|
2019-05-24 16:30:05 +03:00
|
|
|
var expectedBundleId = "com.xamarin.monotouch-test_watch.watchkitapp.watchkitextension";
|
2021-02-24 18:07:04 +03:00
|
|
|
#elif MONOMAC && !NET
|
2017-01-12 21:13:56 +03:00
|
|
|
var expectedBundleId = "com.xamarin.xammac_tests";
|
2016-05-31 20:15:02 +03:00
|
|
|
#else
|
|
|
|
var expectedBundleId = "com.xamarin.monotouch-test";
|
|
|
|
#endif
|
|
|
|
Assert.AreEqual (expectedBundleId, main.Identifier);
|
2016-05-26 16:06:52 +03:00
|
|
|
Assert.IsTrue (main.HasLoadedExecutable);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestBuiltInPlugInsUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
Assert.That(main.BuiltInPlugInsUrl.ToString (), Contains.Substring ("PlugIns/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestExecutableUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2021-02-24 18:07:04 +03:00
|
|
|
#if MONOMAC && !NET
|
2020-12-15 15:16:07 +03:00
|
|
|
var executableRelativePath = Path.Combine (ExpectedAppName, "Contents", "MacOS", "xammac_tests");
|
2021-02-24 18:07:04 +03:00
|
|
|
#elif __MACCATALYST__ || __MACOS__
|
2020-12-15 15:16:07 +03:00
|
|
|
var executableRelativePath = Path.Combine (ExpectedAppName, "Contents", "MacOS", "monotouchtest");
|
2017-01-12 21:13:56 +03:00
|
|
|
#else
|
2020-12-15 15:16:07 +03:00
|
|
|
var executableRelativePath = Path.Combine (ExpectedAppName, "monotouchtest");
|
2017-01-12 21:13:56 +03:00
|
|
|
#endif
|
2020-12-15 15:16:07 +03:00
|
|
|
Assert.That (main.ExecutableUrl.ToString (), Contains.Substring (executableRelativePath));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestPrivateFrameworksUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
Assert.That(main.PrivateFrameworksUrl.ToString (), Contains.Substring ("Frameworks/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestResourcesDirectoryUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2016-05-31 20:15:02 +03:00
|
|
|
Assert.That(main.ResourcesDirectoryUrl.ToString (), Contains.Substring (ExpectedAppName + "/"));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestSharedFrameworksUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
Assert.That(main.SharedFrameworksUrl.ToString (), Contains.Substring ("SharedFrameworks/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestSharedSupportUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
Assert.That(main.SharedSupportUrl.ToString (), Contains.Substring ("SharedSupport/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestSupportFilesDirectoryUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2016-05-31 20:15:02 +03:00
|
|
|
Assert.That(main.SupportFilesDirectoryUrl.ToString (), Contains.Substring (ExpectedAppName + "/"));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestArchitectures ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
Assert.IsTrue (main.Architectures.Length > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestUrl ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2016-05-31 20:15:02 +03:00
|
|
|
Assert.That(main.Url.ToString (), Contains.Substring (ExpectedAppName + "/"));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestDevelopmentRegion ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2017-12-01 18:52:25 +03:00
|
|
|
Assert.IsTrue (String.IsNullOrEmpty (main.DevelopmentRegion));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestLocalizations ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
var localizations = CFBundle.GetLocalizations (main.Url);
|
2018-01-20 22:00:01 +03:00
|
|
|
Assert.AreEqual (1, localizations.Length);
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestLocalizationsNull ()
|
|
|
|
{
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentNullException> (() => CFBundle.GetLocalizations (null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestPreferredLocalizations ()
|
|
|
|
{
|
|
|
|
var preferred = new string [] {"en", "es"};
|
|
|
|
var used = CFBundle.GetPreferredLocalizations (preferred);
|
|
|
|
Assert.IsTrue (used.Length > 0);
|
2018-04-27 19:00:17 +03:00
|
|
|
foreach (var u in used)
|
|
|
|
Assert.That (preferred, Contains.Item (u), u);
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestPreferredLocalizationsNull ()
|
|
|
|
{
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentNullException> (() => CFBundle.GetPreferredLocalizations (null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestAuxiliaryExecutableUrlNull (string executable)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetAuxiliaryExecutableUrl (executable));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetAuxiliaryExecutableUrlNull ()
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
var url = main.GetAuxiliaryExecutableUrl ("fake-exe");
|
|
|
|
Assert.IsNull (url);
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlNullName (string resourceName)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrl (resourceName, "type", null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlNullType (string resourceType)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrl ("resourceName", resourceType, null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestStaticResourceUrlNull ()
|
|
|
|
{
|
|
|
|
NSUrl url = null;
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentNullException> (() => CFBundle.GetResourceUrl (url, "resourceName", "resourceType", null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestStaticResourceUrlNullName (string resourceName)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => CFBundle.GetResourceUrl (main.Url, resourceName, "resourceType", null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestStaticResourceUrlNullType (string resourceType)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => CFBundle.GetResourceUrl (main.Url, "resourceName", resourceType, null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlsNullType (string resourceType)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrls (resourceType, null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestStaticResourceUrlsNullType ()
|
|
|
|
{
|
|
|
|
NSUrl url = null;
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentNullException> (() => CFBundle.GetResourceUrls (url, "resourceType", null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestStaticResourceUrlsNullType (string resourceType)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => CFBundle.GetResourceUrls (main.Url, resourceType, null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlLocalNameNullName (string resourceName)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrl (resourceName, "resourceType", null, "en"));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlLocalNameNullType (string resourceType)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrl ("resourceName", resourceType, null, "en"));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlLocalNameNullLocale (string locale)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrl ("resourceName", "resourceType", null, locale));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlsLocalNameNullType (string type)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrls (type, null, "en"));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestResourceUrlsLocalNameNullLocale (string locale)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetResourceUrls ("jpg", null, locale));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestLocalizedStringNullKey (string key)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetLocalizedString (key, null, "tableName"));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[TestCase ("")]
|
|
|
|
[TestCase (null)]
|
|
|
|
public void TestLocalizedStringNullTable (string tableName)
|
|
|
|
{
|
|
|
|
var main = CFBundle.GetMain ();
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentException> (() => main.GetLocalizedString ("key", null, tableName));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetLocalizationsForPreferencesNullLocalArray ()
|
|
|
|
{
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentNullException> (() => CFBundle.GetLocalizationsForPreferences (null, new string [0]));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetLocalizationsForPreferencesNullPrefArray ()
|
|
|
|
{
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentNullException> (() => CFBundle.GetLocalizationsForPreferences (new string [0], null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestGetInfoDictionaryNull ()
|
|
|
|
{
|
2020-07-16 09:19:33 +03:00
|
|
|
Assert.Throws<ArgumentNullException> (() => CFBundle.GetInfoDictionary (null));
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
2020-10-08 13:38:38 +03:00
|
|
|
|
|
|
|
#if MONOMAC
|
|
|
|
[Test]
|
|
|
|
public void TestIsArchitectureLoadable ()
|
|
|
|
{
|
|
|
|
TestRuntime.AssertXcodeVersion (12, 2);
|
|
|
|
|
2021-05-10 15:49:48 +03:00
|
|
|
var isX64Executable = global::System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture == global::System.Runtime.InteropServices.Architecture.X64;
|
|
|
|
var isArm64Executable = global::System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture == global::System.Runtime.InteropServices.Architecture.Arm64;
|
|
|
|
|
2020-10-08 13:38:38 +03:00
|
|
|
bool loadable_x86_64 = CFBundle.IsArchitectureLoadable (CFBundle.Architecture.X86_64);
|
2021-05-12 08:34:11 +03:00
|
|
|
// Due to Rosetta, both x64 and arm64 executables are loadable on Apple Silicon.
|
2021-05-10 15:49:48 +03:00
|
|
|
if (isX64Executable || isArm64Executable)
|
2020-10-08 13:38:38 +03:00
|
|
|
Assert.IsTrue (loadable_x86_64, "x86_64 Expected => true");
|
|
|
|
else
|
|
|
|
Assert.IsFalse (loadable_x86_64, "x86_64 Expected => false");
|
|
|
|
|
|
|
|
bool loadable_arm64 = CFBundle.IsArchitectureLoadable (CFBundle.Architecture.ARM64);
|
2021-05-10 15:49:48 +03:00
|
|
|
if (isArm64Executable)
|
2020-10-08 13:38:38 +03:00
|
|
|
Assert.IsTrue (loadable_arm64, "arm64 Expected => true");
|
2021-09-07 19:52:53 +03:00
|
|
|
// Due to Rosetta, we can't determine whether ARM64 is loadable or not if we're an X64 executable ourselves.
|
2020-10-08 13:38:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void TestIsExecutableLoadable ()
|
|
|
|
{
|
|
|
|
TestRuntime.AssertXcodeVersion (12, 2);
|
|
|
|
|
|
|
|
var main = CFBundle.GetMain ();
|
|
|
|
var loadableBundle = CFBundle.IsExecutableLoadable (main);
|
|
|
|
Assert.IsTrue (loadableBundle, "loadableBundle");
|
|
|
|
|
|
|
|
var loadableBundleUrl = CFBundle.IsExecutableLoadable (main.ExecutableUrl);
|
|
|
|
Assert.IsTrue (loadableBundleUrl, "loadableBundleUrl");
|
|
|
|
}
|
|
|
|
#endif
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
}
|