Files
acceptance-tests
data
debian
docs
external
ikvm-native
libgc
llvm
m4
man
mcs
build
class
Accessibility
Commons.Xml.Relaxng
Cscompmgd
CustomMarshalers
Facades
I18N
IBM.Data.DB2
ICSharpCode.SharpZipLib
Microsoft.Build
Microsoft.Build.Engine
Microsoft.Build.Framework
Microsoft.Build.Tasks
Microsoft.Build.Utilities
Microsoft.CSharp
Microsoft.NuGet.Build.Tasks
Microsoft.VisualC
Microsoft.Web.Infrastructure
MicrosoftAjaxLibrary
Mono.Btls.Interface
Mono.C5
Mono.CSharp
Mono.Cairo
Mono.Cecil
Mono.Cecil.Mdb
Mono.CodeContracts
Mono.CompilerServices.SymbolWriter
Mono.Data.Sqlite
Mono.Data.Tds
Mono.Debugger.Soft
Mono.Http
Mono.Management
Mono.Messaging
Mono.Messaging.RabbitMQ
Mono.Options
Mono.Parallel
Mono.Posix
Mono.Profiler.Log
Mono.Runtime.Tests
Mono.Security
Mono.Security.Win32
Mono.Simd
Mono.Tasklets
Mono.WebBrowser
Mono.XBuild.Tasks
Novell.Directory.Ldap
PEAPI
RabbitMQ.Client
SMDiagnostics
System
Assembly
Documentation
Internal.Cryptography
Microsoft.CSharp
Microsoft.VisualBasic
Microsoft.Win32
Microsoft.Win32.SafeHandles
Mono
Mono.AppleTls
Mono.Btls
Mono.Http
Mono.Net.Dns
Mono.Net.Security
Mono.Util
MonoTouch
ReferenceSources
System
System.CodeDom.Compiler
System.Configuration
System.Diagnostics
System.IO
System.IO.Compression
System.IO.Ports
System.Media
System.Net
System.Net.Configuration
System.Net.Mail
System.Net.NetworkInformation
System.Net.Security
System.Net.Sockets
System.Net.WebSockets
System.Runtime.InteropServices
System.Runtime.InteropServices.ComTypes
System.Security.AccessControl
System.Security.Authentication
System.Security.Authentication.ExtendedProtection
System.Security.Authentication.ExtendedProtection.Configuration
System.Security.Cryptography
System.Security.Cryptography.X509Certificates
System.Security.Permissions
System.Threading
System.Timers
System.Web
System.Windows.Input
Test
Microsoft.CSharp
Microsoft.VisualBasic
Microsoft.Win32
System
System.CodeDom
System.CodeDom.Compiler
System.Collections.Concurrent
System.Collections.Generic
System.Collections.ObjectModel
System.Collections.Specialized
System.ComponentModel
ArrayConverterTests.cs
AsyncOperationManagerTest.cs
AttributeCollectionTest.cs
AttributeProviderAttributeTest.cs
BackgroundWorkerTest.cs
BindingListTest.cs
ByteConverterTests.cs
CategoryAttributeTests.cs
CharConverterTest.cs
CollectionConverterTest.cs
ComplexBindingPropertiesAttributeTest.cs
ComponentConverterTests.cs
ComponentResourceManagerTest.cs
ContainerTest.cs
CultureInfoConverterTest.cs
DataObjectMethodAttributeTests.cs
DateTimeConverterTests.cs
DateTimeOffsetConverterTests.cs
DecimalConverterTests.cs
DefaultBindingPropertyAttributeTest.cs
DefaultValueAttributeTest.cs
DesignerAttributeTest.cs
DisplayNameAttributeTests.cs
DoubleConverterTests.cs
EnumConverterTests.cs
EventDescriptorCollectionTests.cs
EventHandlerListTests.cs
Int16ConverterTests.cs
Int32ConverterTests.cs
Int64ConverterTests.cs
InvalidEnumArgumentExceptionTest.cs
LicenseManagerTests.cs
ListChangedEventArgsTest.cs
LookupBindingPropertiesAttributeTest.cs
MaskedTextProviderTest.cs.REMOVED.git-id
MultilineStringConverterTest.cs
NestedContainerTest.cs
NullableConverterTest.cs
PropertyDescriptorCollectionTests.cs
PropertyDescriptorTests.cs
ReferenceConverterTest.cs
SByteConverterTests.cs
SingleConverterTests.cs
ToolboxItemAttributeTests.cs
TypeConverterTests.cs
TypeDescriptionProviderTest.cs
TypeDescriptorTests.cs
UInt16ConverterTests.cs
UInt32ConverterTests.cs
UInt64ConverterTests.cs
Win32ExceptionTest.cs
System.ComponentModel.Design
System.ComponentModel.Design.Serialization
System.Configuration
System.Diagnostics
System.IO
System.IO.Compression
System.IO.Ports
System.Net
System.Net.Configuration
System.Net.Mail
System.Net.Mime
System.Net.NetworkInformation
System.Net.Security
System.Net.Sockets
System.Net.WebSockets
System.Runtime.Versioning
System.Security.AccessControl
System.Security.Cryptography
System.Security.Cryptography.X509Certificates
System.Security.Permissions
System.Text.RegularExpressions
System.Threading
System.Timers
System.Web
compressed.bin
test-config-file
corefx
resources
Makefile
System_test.dll.sources
System_xtest.dll.sources
apple-certificates.sources
appletls.sources
basic_System.dll.sources
build_System.dll.sources
common.sources
common_networking.sources
corefx.unix.sources
linux_net_4_x_System.dll.sources
linux_networkinfo.sources
macos_net_4_x_System.dll.sources
macos_networkinfo.sources
mobile_System.dll.sources
monodroid_System.dll.exclude.sources
monodroid_System.dll.sources
monodroid_System_test.dll.exclude.sources
monodroid_System_test.dll.sources
monodroid_System_xtest.dll.exclude.sources
monotouch_System.dll.exclude.sources
monotouch_System.dll.sources
monotouch_System_test.dll.exclude.sources
monotouch_System_xtest.dll.exclude.sources
monotouch_runtime_System.dll.sources
monotouch_tv_System.dll.exclude.sources
monotouch_tv_System.dll.sources
monotouch_tv_System_test.dll.exclude.sources
monotouch_tv_System_xtest.dll.exclude.sources
monotouch_tv_runtime_System.dll.sources
monotouch_watch_System.dll.exclude.sources
monotouch_watch_System.dll.sources
monotouch_watch_System_test.dll.exclude.sources
monotouch_watch_System_xtest.dll.exclude.sources
monotouch_watch_runtime_System.dll.exclude.sources
monotouch_watch_runtime_System.dll.sources
net_4_x_System.dll.sources
orbis_System.dll.exclude.sources
orbis_System.dll.sources
testing_aot_full_System.dll.sources
testing_aot_full_System_test.dll.exclude.sources
testing_aot_full_System_test.dll.sources
testing_aot_hybrid_System.dll.sources
testing_aot_hybrid_System_test.dll.exclude.sources
unix_net_4_x_System.dll.sources
unix_networkinfo.sources
unreal_System.dll.sources
wasm_System.dll.sources
wasm_System_test.dll.exclude.sources
wasm_System_xtest.dll.exclude.sources
win32_net_4_x_System.dll.exclude.sources
win32_net_4_x_System.dll.sources
win32_networkinfo.sources
winaot_System.dll.exclude.sources
winaot_System.dll.sources
winaot_System_test.dll.exclude.sources
xammac_System.dll.sources
xammac_net_4_5_System.dll.sources
System.ComponentModel.Composition.4.5
System.ComponentModel.DataAnnotations
System.Configuration
System.Configuration.Install
System.Core
System.Data
System.Data.DataSetExtensions
System.Data.Entity
System.Data.Linq
System.Data.OracleClient
System.Data.Services
System.Data.Services.Client
System.Deployment
System.Design
System.DirectoryServices
System.DirectoryServices.Protocols
System.Drawing
System.Drawing.Design
System.Dynamic
System.EnterpriseServices
System.IO.Compression
System.IO.Compression.FileSystem
System.IdentityModel
System.IdentityModel.Selectors
System.Json
System.Json.Microsoft
System.Management
System.Messaging
System.Net
System.Net.Http
System.Net.Http.Formatting
System.Net.Http.WebRequest
System.Net.Http.WinHttpHandler
System.Numerics
System.Numerics.Vectors
System.Reactive.Core
System.Reactive.Debugger
System.Reactive.Experimental
System.Reactive.Interfaces
System.Reactive.Linq
System.Reactive.Observable.Aliases
System.Reactive.PlatformServices
System.Reactive.Providers
System.Reactive.Runtime.Remoting
System.Reactive.Windows.Forms
System.Reactive.Windows.Threading
System.Reflection.Context
System.Runtime.Caching
System.Runtime.CompilerServices.Unsafe
System.Runtime.DurableInstancing
System.Runtime.Remoting
System.Runtime.Serialization
System.Runtime.Serialization.Formatters.Soap
System.Security
System.ServiceModel
System.ServiceModel.Activation
System.ServiceModel.Discovery
System.ServiceModel.Internals
System.ServiceModel.Routing
System.ServiceModel.Web
System.ServiceProcess
System.Threading.Tasks.Dataflow
System.Transactions
System.Web
System.Web.Abstractions
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Extensions
System.Web.Extensions.Design
System.Web.Http
System.Web.Http.SelfHost
System.Web.Http.WebHost
System.Web.Mobile
System.Web.Mvc3
System.Web.Razor
System.Web.RegularExpressions
System.Web.Routing
System.Web.Services
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor
System.Windows
System.Windows.Forms
System.Windows.Forms.DataVisualization
System.Workflow.Activities
System.Workflow.ComponentModel
System.Workflow.Runtime
System.XML
System.Xaml
System.Xml.Linq
System.Xml.Serialization
SystemWebTestShim
WebMatrix.Data
WindowsBase
aot-compiler
corlib
dlr
doc
legacy
lib
monodoc
notes
reference-assemblies
referencesource
test-helpers
LICENSE
Makefile
Open.snk
README
ecma.pub
mono.pub
mono.snk
msfinal.pub
reactive.pub
silverlight.pub
winfx.pub
winfx3.pub
docs
errors
ilasm
jay
mcs
nunit24
packages
tests
tools
AUTHORS
COPYING
INSTALL.txt
Makefile
MonoIcon.png
README
ScalableMonoIcon.svg
mkinstalldirs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/mcs/class/System/Test/System.ComponentModel/ReferenceConverterTest.cs

252 lines
7.1 KiB
C#
Raw Normal View History

//
// MonoTests.System.ComponentModel.ReferenceConverterTest
//
// Author:
// Ivan N. Zlatev <contact@i-nz.net>
//
// Copyright (C) 2008 Ivan N. Zlatev
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
using NUnit.Framework;
namespace MonoTests.System.ComponentModel
{
[TestFixture]
public class ReferenceConverterTest
{
class TestReferenceService : IReferenceService
{
private Dictionary<string, object> references;
public TestReferenceService ()
{
references = new Dictionary<string, object> ();
}
public void AddReference (string name, object reference)
{
references[name] = reference;
}
public void ClearReferences ()
{
references.Clear ();
}
public IComponent GetComponent (object reference)
{
return null;
}
public string GetName (object reference)
{
foreach (KeyValuePair<string, object> entry in references) {
if (entry.Value == reference)
return entry.Key;
}
return null;
}
public object GetReference (string name)
{
if (!references.ContainsKey (name))
return null;
return references[name];
}
public object[] GetReferences()
{
object[] array = new object[references.Values.Count];
references.Values.CopyTo (array, 0);
return array;
}
public object[] GetReferences(Type baseType)
{
object[] references = GetReferences ();
List<object> filtered = new List<object> ();
foreach (object reference in references) {
if (baseType.IsInstanceOfType(reference))
filtered.Add (reference);
}
return filtered.ToArray();
}
}
class TestTypeDescriptorContext : ITypeDescriptorContext
{
private IReferenceService reference_service = null;
private IContainer container = null;
public TestTypeDescriptorContext ()
{
}
public TestTypeDescriptorContext (IReferenceService referenceService)
{
reference_service = referenceService;
}
public IContainer Container {
get { return container; }
set { container = value; }
}
public object Instance {
get { return null; }
}
public PropertyDescriptor PropertyDescriptor {
get { return null; }
}
public void OnComponentChanged ()
{
}
public bool OnComponentChanging ()
{
return true;
}
public object GetService (Type serviceType)
{
if (serviceType == typeof (IReferenceService))
return reference_service;
return null;
}
}
interface ITestInterface
{
}
class TestComponent : Component
{
}
[Test]
public void CanConvertFrom ()
{
ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
// without context
Assert.IsFalse (converter.CanConvertFrom (null, typeof(string)), "#1");
// with context
Assert.IsTrue (converter.CanConvertFrom (new TestTypeDescriptorContext (), typeof(string)), "#2");
}
#if !MOBILE
[Test]
public void ConvertFrom ()
{
ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
string referenceName = "reference name";
// no context
Assert.IsNull (converter.ConvertFrom (null, null, referenceName), "#1");
TestComponent component = new TestComponent();
// context with IReferenceService
TestReferenceService referenceService = new TestReferenceService ();
referenceService.AddReference (referenceName, component);
TestTypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);
Assert.AreSame (component, converter.ConvertFrom (context, null, referenceName), "#2");
// context with Component without IReferenceService
Container container = new Container ();
container.Add (component, referenceName);
context = new TestTypeDescriptorContext ();
context.Container = container;
Assert.AreSame (component, converter.ConvertFrom (context, null, referenceName), "#3");
}
[Test]
public void ConvertTo ()
{
ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
string referenceName = "reference name";
Assert.AreEqual ("(none)", (string)converter.ConvertTo (null, null, null, typeof(string)), "#1");
TestComponent component = new TestComponent();
// no context
Assert.AreEqual (String.Empty, (string)converter.ConvertTo (null, null, component, typeof(string)), "#2");
// context with IReferenceService
TestReferenceService referenceService = new TestReferenceService ();
referenceService.AddReference (referenceName, component);
TestTypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);
Assert.AreEqual (referenceName, (string)converter.ConvertTo (context, null, component, typeof(string)), "#3");
// context with Component without IReferenceService
Container container = new Container ();
container.Add (component, referenceName);
context = new TestTypeDescriptorContext ();
context.Container = container;
Assert.AreEqual (referenceName, (string)converter.ConvertTo (context, null, component, typeof(string)), "#4");
}
#endif
[Test]
public void CanConvertTo ()
{
ReferenceConverter converter = new ReferenceConverter (typeof(ITestInterface));
Assert.IsTrue (converter.CanConvertTo (new TestTypeDescriptorContext (), typeof(string)), "#1");
}
[Test]
public void GetStandardValues ()
{
ReferenceConverter converter = new ReferenceConverter (typeof(TestComponent));
TestComponent component1 = new TestComponent();
TestComponent component2 = new TestComponent();
TestReferenceService referenceService = new TestReferenceService ();
referenceService.AddReference ("reference name 1", component1);
referenceService.AddReference ("reference name 2", component2);
ITypeDescriptorContext context = new TestTypeDescriptorContext (referenceService);
TypeConverter.StandardValuesCollection values = converter.GetStandardValues (context);
Assert.IsNotNull (values, "#1");
// 2 components + 1 null value
Assert.AreEqual (3, values.Count, "#2");
}
}
}