public void BeginInitEndInit_WithInitializedEvent_CallsEvent()
        {
            using var bindingSource = new BindingSource();
            ISupportInitializeNotification source = bindingSource;
            int          callCount = 0;
            EventHandler handler   = (sender, e) =>
            {
                callCount++;
                Assert.Same(source, sender);
                Assert.Equal(EventArgs.Empty, e);
            };

            source.Initialized += handler;

            source.EndInit();
            Assert.True(source.IsInitialized);
            Assert.Equal(1, callCount);

            source.EndInit();
            Assert.True(source.IsInitialized);
            Assert.Equal(2, callCount);

            // Remove handler.
            source.Initialized -= handler;
            source.EndInit();
            Assert.True(source.IsInitialized);
            Assert.Equal(2, callCount);
        }
        public void BeginInitEndInit_WithISupportInitializeNotificationDataSourceInitialized_DoesNotWaitForDataSourceInitialize()
        {
            var dataSource = new ISupportInitializeNotificationDataSource {
                IsInitializedResult = true
            };

            using var bindingSource = new BindingSource(dataSource, nameof(ISupportInitializeNotificationDataSource.Member));
            ISupportInitializeNotification source = bindingSource;
            int          callCount = 0;
            EventHandler handler   = (sender, e) =>
            {
                callCount++;
                Assert.Same(source, sender);
                Assert.Equal(EventArgs.Empty, e);
            };

            source.Initialized += handler;

            source.BeginInit();
            Assert.False(source.IsInitialized);
            Assert.Equal(0, callCount);

            source.EndInit();
            Assert.True(source.IsInitialized);
            Assert.Equal(1, callCount);

            // Call again.
            source.EndInit();
            Assert.True(source.IsInitialized);
            Assert.Equal(2, callCount);
        }
Esempio n. 3
0
        void DataSourceEndInitHandler(object o, EventArgs args)
        {
            ((ISupportInitializeNotification)datasource).Initialized -= DataSourceEndInitHandler;

            ISupportInitializeNotification inotif = (ISupportInitializeNotification)this;

            inotif.EndInit();
        }
        public void BeginInitEndInit_SetDataSourceInInit_Success(object newDataSource)
        {
            var dataSource = new ISupportInitializeNotificationDataSource {
                IsInitializedResult = false
            };

            using var bindingSource = new BindingSource(dataSource, nameof(ISupportInitializeNotificationDataSource.Member));
            ISupportInitializeNotification source = bindingSource;
            int          callCount = 0;
            EventHandler handler   = (sender, e) =>
            {
                callCount++;
                Assert.Same(source, sender);
                Assert.Equal(EventArgs.Empty, e);
            };

            source.Initialized     += handler;
            dataSource.Initialized += (sender, e) =>
            {
                ((BindingSource)source).DataSource = newDataSource;
            };

            source.BeginInit();
            Assert.False(source.IsInitialized);
            Assert.Equal(0, callCount);

            // Call the source init - should not initialize and should instead wait for
            // the data source initialization.
            source.EndInit();
            Assert.False(source.IsInitialized);
            Assert.Equal(0, callCount);

            // Then, call the data source init.
            dataSource.EndInit();
            Assert.True(source.IsInitialized);
            Assert.Equal(1, callCount);

            // Call again.
            source.EndInit();
            Assert.True(source.IsInitialized);
            Assert.Equal(2, callCount);
        }
        public void BeginInitEndInit_Invoke_Success()
        {
            using var bindingSource = new BindingSource();
            ISupportInitializeNotification source = bindingSource;

            source.BeginInit();
            Assert.False(source.IsInitialized);

            source.EndInit();
            Assert.True(source.IsInitialized);
        }