public void DesignerSerializationManager_GetService_IContainer_ReturnsExpected(IServiceProvider provider, object expected)
        {
            var manager = new SubDesignerSerializationManager(provider);

            Assert.Same(expected, manager.GetService(typeof(IContainer)));
            Assert.Same(expected, ((IServiceProvider)manager).GetService(typeof(IContainer)));
        }
        public void OnSessionDisposed_InvokeWithSessionDisposed_CallsHandler(EventArgs eventArgs)
        {
            var          manager   = new SubDesignerSerializationManager();
            int          callCount = 0;
            EventHandler handler   = (sender, e) =>
            {
                Assert.Same(manager, sender);
                Assert.Same(eventArgs, e);
                callCount++;
            };

            manager.SessionDisposed += handler;

            manager.OnSessionDisposed(eventArgs);
            Assert.Equal(1, callCount);

            // Call again.
            manager.OnSessionDisposed(eventArgs);
            Assert.Equal(2, callCount);

            // Remove handler.
            manager.SessionDisposed -= handler;
            manager.OnSessionDisposed(eventArgs);
            Assert.Equal(2, callCount);
        }
        public void DesignerSerializationManager_Properties_SetWithExistingProperties_Resets()
        {
            var manager = new SubDesignerSerializationManager
            {
                PropertyProvider = new PropertyProvider()
            };
            PropertyDescriptorCollection properties = manager.Properties;
            PropertyDescriptor           property   = Assert.IsAssignableFrom <PropertyDescriptor>(Assert.Single(properties));

            Assert.Equal(nameof(PropertyProvider.Value), property.Name);
            Assert.Same(properties, manager.Properties);

            var provider = new OtherPropertyProvider();

            manager.PropertyProvider = provider;
            Assert.Same(provider, manager.PropertyProvider);
            PropertyDescriptorCollection otherProperties = manager.Properties;
            PropertyDescriptor           otherProperty   = Assert.IsAssignableFrom <PropertyDescriptor>(Assert.Single(otherProperties));

            Assert.Equal(nameof(OtherPropertyProvider.OtherValue), otherProperty.Name);
            Assert.Same(otherProperties, manager.Properties);

            // Set same.
            manager.PropertyProvider = provider;
            Assert.Same(provider, manager.PropertyProvider);
            Assert.Same(otherProperties, manager.Properties);
        }
        public void DesignerSerializationManager_OnSessionDisposed_InvokeWithSerializationComplete_CallsHandler(EventArgs eventArgs)
        {
            var          manager   = new SubDesignerSerializationManager();
            int          callCount = 0;
            EventHandler handler   = (sender, e) =>
            {
                Assert.Same(manager, sender);
                Assert.Same(eventArgs, e);
                callCount++;
            };
            IDisposable session = manager.CreateSession();

            manager.SerializationComplete += handler;
            manager.OnSessionDisposed(eventArgs);
            Assert.Equal(1, callCount);
            session.Dispose();

            // Call again.
            session = manager.CreateSession();
            session.Dispose();
            Assert.Equal(1, callCount);

            // Remove handler.
            session = manager.CreateSession();
            manager.SerializationComplete += handler;
            manager.SerializationComplete -= handler;
            session.Dispose();
            Assert.Equal(1, callCount);
        }
        public void DesignerSerializationManager_GetService_NoProvider_ReturnsNull(Type serviceType)
        {
            var manager = new SubDesignerSerializationManager();

            Assert.Null(manager.GetService(serviceType));
            Assert.Null(((IServiceProvider)manager).GetService(serviceType));
        }
        public void DesignerSerializationManager_Context_GetWithSession_ReturnsExpected()
        {
            var          manager = new SubDesignerSerializationManager();
            IDisposable  session = manager.CreateSession();
            ContextStack context = manager.Context;

            Assert.Null(context.Current);
            Assert.Same(context, manager.Context);
        }
        public void DesignerSerializationManager_SerializationComplete_AddNoSession_ThrowsInvalidOperationException()
        {
            var          manager   = new SubDesignerSerializationManager();
            int          callCount = 0;
            EventHandler handler   = (sender, e) => callCount++;

            Assert.Throws <InvalidOperationException>(() => manager.SerializationComplete += handler);
            manager.SerializationComplete -= handler;
            Assert.Equal(0, callCount);
        }
        public void DesignerSerializationManager_Context_GetNoSessionAfterGetting_ThrowsInvalidOperationException()
        {
            var          manager = new SubDesignerSerializationManager();
            IDisposable  session = manager.CreateSession();
            ContextStack context = manager.Context;

            Assert.NotNull(context);
            Assert.Same(context, manager.Context);
            session.Dispose();
            Assert.Throws <InvalidOperationException>(() => manager.Context);
        }
        public void DesignerSerializationManager_Ctor_Default()
        {
            var manager = new SubDesignerSerializationManager();

            Assert.Null(manager.Container);
            Assert.True(manager.PreserveNames);
            Assert.Empty(manager.Properties);
            Assert.Same(manager.Properties, manager.Properties);
            Assert.Null(manager.PropertyProvider);
            Assert.False(manager.RecycleInstances);
            Assert.True(manager.ValidateRecycledTypes);;
        }
        public void DesignerSerializationManager_Ctor_IServiceProvider(IServiceProvider provider, IContainer expectedContainer)
        {
            var manager = new SubDesignerSerializationManager(provider);

            Assert.Same(expectedContainer, manager.Container);
            Assert.Same(manager.Container, manager.Container);
            Assert.True(manager.PreserveNames);
            Assert.Empty(manager.Properties);
            Assert.Same(manager.Properties, manager.Properties);
            Assert.Null(manager.PropertyProvider);
            Assert.False(manager.RecycleInstances);
            Assert.True(manager.ValidateRecycledTypes);;
        }
        public void GetType_ValidProvider_ReturnsExpected(string typeName, Type resolvedType, int typeDescriptionProviderServiceCount, bool supportedType, Type expected)
        {
            var mockTypeResolutionService = new Mock <ITypeResolutionService>(MockBehavior.Strict);

            mockTypeResolutionService
            .Setup(s => s.GetType(typeName))
            .Returns(resolvedType)
            .Verifiable();
            var mockTypeDescriptionProvider = new Mock <TypeDescriptionProvider>(MockBehavior.Strict);

            mockTypeDescriptionProvider
            .Setup(p => p.IsSupportedType(resolvedType))
            .Returns(supportedType)
            .Verifiable();
            var mockTypeDescriptionProviderService = new Mock <TypeDescriptionProviderService>(MockBehavior.Strict);

            mockTypeDescriptionProviderService
            .Setup(s => s.GetProvider(resolvedType))
            .Returns(mockTypeDescriptionProvider.Object)
            .Verifiable();
            var mockServiceProvider = new Mock <IServiceProvider>(MockBehavior.Strict);

            mockServiceProvider
            .Setup(p => p.GetService(typeof(IDesignerHost)))
            .Returns((IDesignerHost)null);
            mockServiceProvider
            .Setup(p => p.GetService(typeof(ITypeResolutionService)))
            .Returns(mockTypeResolutionService.Object)
            .Verifiable();
            mockServiceProvider
            .Setup(p => p.GetService(typeof(TypeDescriptionProviderService)))
            .Returns(mockTypeDescriptionProviderService.Object)
            .Verifiable();
            var manager = new SubDesignerSerializationManager(mockServiceProvider.Object);

            Assert.Same(expected, manager.GetType(typeName));
            mockServiceProvider.Verify(p => p.GetService(typeof(ITypeResolutionService)), Times.Once());
            mockTypeResolutionService.Verify(s => s.GetType(typeName), Times.Once());
            mockServiceProvider.Verify(p => p.GetService(typeof(TypeDescriptionProviderService)), Times.Exactly(typeDescriptionProviderServiceCount));
            mockTypeDescriptionProviderService.Verify(s => s.GetProvider(resolvedType), Times.Exactly(typeDescriptionProviderServiceCount));
            mockTypeDescriptionProvider.Verify(s => s.IsSupportedType(resolvedType), Times.Exactly(typeDescriptionProviderServiceCount));

            // Call again.
            Assert.Same(expected, manager.GetType(typeName));
            mockServiceProvider.Verify(p => p.GetService(typeof(ITypeResolutionService)), Times.Once());
            mockTypeResolutionService.Verify(s => s.GetType(typeName), Times.Exactly(2));
            mockServiceProvider.Verify(p => p.GetService(typeof(TypeDescriptionProviderService)), Times.Exactly(typeDescriptionProviderServiceCount * 2));
            mockTypeDescriptionProviderService.Verify(s => s.GetProvider(resolvedType), Times.Exactly(typeDescriptionProviderServiceCount * 2));
            mockTypeDescriptionProvider.Verify(s => s.IsSupportedType(resolvedType), Times.Exactly(typeDescriptionProviderServiceCount * 2));
        }
        public void DesignerSerializationManager_Container_Set_GetReturnsExpected(IContainer value)
        {
            var manager = new SubDesignerSerializationManager
            {
                Container = value
            };

            Assert.Same(value, manager.Container);
            Assert.Same(value, manager.GetService(typeof(IContainer)));

            // Set same.
            manager.Container = value;
            Assert.Same(value, manager.Container);
            Assert.Same(value, manager.GetService(typeof(IContainer)));
        }
        public void DesignerSerializationManager_GetService_WithProvider_ReturnsExpected(Type serviceType)
        {
            var service             = new object();
            var mockServiceProvider = new Mock <IServiceProvider>(MockBehavior.Strict);

            mockServiceProvider
            .Setup(p => p.GetService(serviceType))
            .Returns(service)
            .Verifiable();
            var manager = new SubDesignerSerializationManager(mockServiceProvider.Object);

            Assert.Same(service, manager.GetService(serviceType));
            mockServiceProvider.Verify(p => p.GetService(serviceType), Times.Once());

            Assert.Same(service, ((IServiceProvider)manager).GetService(serviceType));
            mockServiceProvider.Verify(p => p.GetService(serviceType), Times.Exactly(2));
        }
        public void DesignerSerializationManager_Properties_GetWithPropertyProvider_ReturnExpected()
        {
            var provider = new PropertyProvider();
            var manager  = new SubDesignerSerializationManager
            {
                PropertyProvider = provider
            };
            PropertyDescriptorCollection properties = manager.Properties;

            Assert.Same(properties, manager.Properties);
            PropertyDescriptor property = Assert.IsAssignableFrom <PropertyDescriptor>(Assert.Single(properties));

            Assert.NotEmpty(property.Attributes);
            Assert.Equal("Category", property.Category);
            Assert.IsType <Int64Converter>(property.Converter);
            Assert.Equal(typeof(PropertyProvider), property.ComponentType);
            Assert.Equal("Description", property.Description);
            Assert.True(property.DesignTimeOnly);
            Assert.Equal("DisplayName", property.DisplayName);
            Assert.True(property.IsBrowsable);
            Assert.True(property.IsLocalizable);
            Assert.False(property.IsReadOnly);
            Assert.Equal("Value", property.Name);
            Assert.Equal(typeof(int), property.PropertyType);
            Assert.Equal(DesignerSerializationVisibility.Content, property.SerializationVisibility);
            Assert.False(property.SupportsChangeEvents);

            // Should be wrapped.
            Assert.False(property.CanResetValue(new Component()));
            Assert.Equal(0, property.GetValue(new Component()));
            property.SetValue(new Component(), 1);
            Assert.Equal(1, property.GetValue(new Component()));
            property.ResetValue(new Component());
            Assert.Equal(1, property.GetValue(new Component()));
            Assert.True(property.ShouldSerializeValue(new Component()));
        }
        public void DesignerSerializationManager_Context_GetNoSession_ThrowsInvalidOperationException()
        {
            var manager = new SubDesignerSerializationManager();

            Assert.Throws <InvalidOperationException>(() => manager.Context);
        }
        public void DesignerSerializationManager_GetType_NullTypeName_ThrowsArgumentNullException()
        {
            var manager = new SubDesignerSerializationManager();

            Assert.Throws <ArgumentNullException>("typeName", () => manager.GetType(null));
        }
        public void DesignerSerializationManager_GetType_NoProvider_ReturnsExpected(string typeName, Type expected)
        {
            var manager = new SubDesignerSerializationManager();

            Assert.Same(expected, manager.GetType(typeName));
        }
        public void DesignerSerializationManager_GetType_InvalidProvider_ReturnsExpected(IServiceProvider provider)
        {
            var manager = new SubDesignerSerializationManager(provider);

            Assert.Equal(typeof(int), manager.GetType(typeof(int).FullName));
        }