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
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
Assembly
System.Collections.ObjectModel
System.Collections.Specialized
System.ComponentModel
System.Diagnostics
System.IO
System.IO.Packaging
System.Security.Permissions
System.Security.RightsManagement
System.Windows
System.Windows.Converters
System.Windows.Data
System.Windows.Input
System.Windows.Interop
System.Windows.Markup
System.Windows.Markup.Primitives
System.Windows.Media
System.Windows.Media.Converters
System.Windows.Threading
Test
System.Collections.ObjectModel
ObservableCollectionTest.cs
ReadOnlyObservableCollectionTest.cs
System.Collections.Specialized
System.ComponentModel
System.IO.Packaging
System.Windows
System.Windows.Markup
System.Windows.Media
System.Windows.Threading
ZipSharp
Makefile
WindowsBase.dll.sources
WindowsBase_test.dll.sources
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
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/WindowsBase/Test/System.Collections.ObjectModel/ObservableCollectionTest.cs

322 lines
10 KiB
C#
Raw Normal View History

// 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.
//
// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Chris Toshok (toshok@ximian.com)
// Brian O'Keefe (zer0keefie@gmail.com)
//
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using NUnit.Framework;
using System.ComponentModel;
using System.Collections.Generic;
using System;
using System.Collections;
using MonoTests.System.Collections.Specialized;
namespace MonoTests.System.Collections.ObjectModel {
[TestFixture]
public class ObservableCollectionTest {
public ObservableCollectionTest()
{
}
[Test]
public void Insert()
{
bool reached = false;
ObservableCollection<int> col = new ObservableCollection<int> ();
col.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
reached = true;
Assert.AreEqual (NotifyCollectionChangedAction.Add, e.Action, "INS_1");
Assert.AreEqual (0, e.NewStartingIndex, "INS_2");
Assert.AreEqual (-1, e.OldStartingIndex, "INS_3");
Assert.AreEqual (1, e.NewItems.Count, "INS_4");
Assert.AreEqual (5, (int)e.NewItems [0], "INS_5");
Assert.AreEqual (null, e.OldItems, "INS_6");
};
col.Insert (0, 5);
Assert.IsTrue (reached, "INS_5");
}
[Test]
public void RemoveAt()
{
bool reached = false;
ObservableCollection<int> col = new ObservableCollection<int> ();
col.Insert (0, 5);
col.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
reached = true;
Assert.AreEqual (NotifyCollectionChangedAction.Remove, e.Action, "REMAT_1");
Assert.AreEqual (-1, e.NewStartingIndex, "REMAT_2");
Assert.AreEqual (0, e.OldStartingIndex, "REMAT_3");
Assert.AreEqual (null, e.NewItems, "REMAT_4");
Assert.AreEqual (1, e.OldItems.Count, "REMAT_5");
Assert.AreEqual (5, (int)e.OldItems [0], "REMAT_6");
};
col.RemoveAt (0);
Assert.IsTrue (reached, "REMAT_7");
}
[Test]
public void Move()
{
bool reached = false;
ObservableCollection<int> col = new ObservableCollection<int> ();
col.Insert (0, 0);
col.Insert (1, 1);
col.Insert (2, 2);
col.Insert (3, 3);
col.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
reached = true;
Assert.AreEqual (NotifyCollectionChangedAction.Move, e.Action, "MOVE_1");
Assert.AreEqual (3, e.NewStartingIndex, "MOVE_2");
Assert.AreEqual (1, e.OldStartingIndex, "MOVE_3");
Assert.AreEqual (1, e.NewItems.Count, "MOVE_4");
Assert.AreEqual (1, e.NewItems [0], "MOVE_5");
Assert.AreEqual (1, e.OldItems.Count, "MOVE_6");
Assert.AreEqual (1, e.OldItems [0], "MOVE_7");
};
col.Move (1, 3);
Assert.IsTrue (reached, "MOVE_8");
}
[Test]
public void Add()
{
ObservableCollection<char> collection = new ObservableCollection<char> ();
bool propertyChanged = false;
List<string> changedProps = new List<string> ();
NotifyCollectionChangedEventArgs args = null;
((INotifyPropertyChanged)collection).PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
propertyChanged = true;
changedProps.Add (e.PropertyName);
};
collection.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
args = e;
};
collection.Add ('A');
Assert.IsTrue (propertyChanged, "ADD_1");
Assert.IsTrue (changedProps.Contains ("Count"), "ADD_2");
Assert.IsTrue (changedProps.Contains ("Item[]"), "ADD_3");
CollectionChangedEventValidators.ValidateAddOperation (args, new char [] { 'A' }, 0, "ADD_4");
}
[Test]
public void Remove()
{
ObservableCollection<char> collection = new ObservableCollection<char> ();
bool propertyChanged = false;
List<string> changedProps = new List<string> ();
NotifyCollectionChangedEventArgs args = null;
collection.Add ('A');
collection.Add ('B');
collection.Add ('C');
((INotifyPropertyChanged)collection).PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
propertyChanged = true;
changedProps.Add (e.PropertyName);
};
collection.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
args = e;
};
collection.Remove ('B');
Assert.IsTrue (propertyChanged, "REM_1");
Assert.IsTrue (changedProps.Contains ("Count"), "REM_2");
Assert.IsTrue (changedProps.Contains ("Item[]"), "REM_3");
CollectionChangedEventValidators.ValidateRemoveOperation (args, new char [] { 'B' }, 1, "REM_4");
}
[Test]
public void Set()
{
ObservableCollection<char> collection = new ObservableCollection<char> ();
bool propertyChanged = false;
List<string> changedProps = new List<string> ();
NotifyCollectionChangedEventArgs args = null;
collection.Add ('A');
collection.Add ('B');
collection.Add ('C');
((INotifyPropertyChanged)collection).PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
propertyChanged = true;
changedProps.Add (e.PropertyName);
};
collection.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
args = e;
};
collection [2] = 'I';
Assert.IsTrue (propertyChanged, "SET_1");
Assert.IsTrue (changedProps.Contains ("Item[]"), "SET_2");
CollectionChangedEventValidators.ValidateReplaceOperation (args, new char [] { 'C' }, new char [] { 'I' }, 2, "SET_3");
}
[Test]
public void Reentrant()
{
ObservableCollection<char> collection = new ObservableCollection<char> ();
bool propertyChanged = false;
List<string> changedProps = new List<string> ();
NotifyCollectionChangedEventArgs args = null;
collection.Add ('A');
collection.Add ('B');
collection.Add ('C');
PropertyChangedEventHandler pceh = delegate (object sender, PropertyChangedEventArgs e) {
propertyChanged = true;
changedProps.Add (e.PropertyName);
};
// Adding a PropertyChanged event handler
((INotifyPropertyChanged)collection).PropertyChanged += pceh;
collection.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
args = e;
};
collection.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
// This one will attempt to break reentrancy
try {
collection.Add ('X');
Assert.Fail ("Reentrancy should not be allowed.");
} catch (InvalidOperationException) {
}
};
collection [2] = 'I';
Assert.IsTrue (propertyChanged, "REENT_1");
Assert.IsTrue (changedProps.Contains ("Item[]"), "REENT_2");
CollectionChangedEventValidators.ValidateReplaceOperation (args, new char [] { 'C' }, new char [] { 'I' }, 2, "REENT_3");
// Removing the PropertyChanged event handler should work as well:
((INotifyPropertyChanged)collection).PropertyChanged -= pceh;
}
//Private test class for protected members of ObservableCollection
private class ObservableCollectionTestHelper : ObservableCollection<char> {
internal void DoubleEnterReentrant()
{
IDisposable object1 = BlockReentrancy ();
IDisposable object2 = BlockReentrancy ();
Assert.AreSame (object1, object2);
//With double block, try the reentrant:
NotifyCollectionChangedEventArgs args = null;
CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
args = e;
};
// We need a second callback for reentrancy to matter
CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
// Doesn't need to do anything; just needs more than one callback registered.
};
// Try adding - this should cause reentrancy, and fail
try {
Add ('I');
Assert.Fail ("Reentrancy should not be allowed. -- #2");
} catch (InvalidOperationException) {
}
// Release the first reentrant
object1.Dispose ();
// Try adding again - this should cause reentrancy, and fail again
try {
Add ('J');
Assert.Fail ("Reentrancy should not be allowed. -- #3");
} catch (InvalidOperationException) {
}
// Release the reentrant a second time
object1.Dispose ();
// This last add should work fine.
Add ('K');
CollectionChangedEventValidators.ValidateAddOperation (args, new char [] { 'K' }, 0, "REENTHELP_1");
}
}
[Test]
public void ReentrantReuseObject()
{
ObservableCollectionTestHelper helper = new ObservableCollectionTestHelper ();
helper.DoubleEnterReentrant ();
}
[Test]
public void Clear()
{
List<char> initial = new List<char> ();
initial.Add ('A');
initial.Add ('B');
initial.Add ('C');
ObservableCollection<char> collection = new ObservableCollection<char> (initial);
bool propertyChanged = false;
List<string> changedProps = new List<string> ();
NotifyCollectionChangedEventArgs args = null;
((INotifyPropertyChanged)collection).PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
propertyChanged = true;
changedProps.Add (e.PropertyName);
};
collection.CollectionChanged += delegate (object sender, NotifyCollectionChangedEventArgs e) {
args = e;
};
collection.Clear ();
Assert.IsTrue (propertyChanged, "CLEAR_1");
Assert.IsTrue (changedProps.Contains ("Count"), "CLEAR_2");
Assert.IsTrue (changedProps.Contains ("Item[]"), "CLEAR_3");
CollectionChangedEventValidators.ValidateResetOperation (args, "CLEAR_4");
}
}
}