Esempio n. 1
0
        public void UndoService_Uses_WeakReferences()
        {
            // Create a root, but don't keep any references to the object that it is for.
            UndoRoot      undoRoot = null;
            WeakReference wrDoc    = GetWeakReference(out undoRoot);

            // Confirm the object is still alive.
            Assert.IsNotNull(undoRoot);
            Assert.IsTrue(wrDoc.IsAlive);

            // Force a GC to collect the document used for the undo root.
            // Don't do this in normal code... this is just for testing.
            GC.Collect(1, GCCollectionMode.Forced, true);

            // Confirm the RootDocument was collected.
            // This shows that we're not leaking memory via the UndoService's _Roots dictionary.
            Assert.IsFalse(wrDoc.IsAlive);
            Assert.IsNull(wrDoc.Target);


            // Inline function.
            // This method is part of UndoService_Uses_WeakReferences.
            // Code is in a separate method so that the variables are properly scoped
            // which allows the GC call to work properly.
            WeakReference GetWeakReference(out UndoRoot undoRoot)
            {
                var document = new RootDocument();          // Constructed, but not returned.

                undoRoot = UndoService.Current[document];   // This creates a weakly referenced undo root.
                return(new WeakReference(document));        // Return a weak reference so that we can test if the document was collected.
            }
        }
Esempio n. 2
0
        public void TestSetup()
        {
            Document1   = new RootDocument();
            Document1.A = new ChildA()
            {
                Name = "Document1.ChildA", UndoableSometimes = "Value1"
            };
            Document1.Bs.Add(new ChildB()
            {
                Name = "Document1.ChildB[0]"
            });
            Document1.Bs.Add(new ChildB()
            {
                Name = "Document1.ChildB[1]"
            });
            Document1.Bs.Add(new ChildB()
            {
                Name = "Document1.ChildB[2]"
            });

            Document1.KeyValuePairs[0] = new ChildA()
            {
                Name = "Document1.ChildA.0"
            };
            Document1.KeyValuePairs[1] = new ChildA()
            {
                Name = "Document1.ChildA.1"
            };
            Document1.KeyValuePairs[2] = new ChildA()
            {
                Name = "Document1.ChildA.2"
            };

            Assert.IsNotNull(Document1.A.Root);
            Assert.AreSame(Document1, Document1.A.Root);
            Assert.AreSame(Document1, Document1.Bs[0].Root);
            Assert.AreSame(Document1, Document1.Bs[1].Root);
            Assert.AreSame(Document1, Document1.Bs[2].Root);

            Document2   = new RootDocument();
            Document2.A = new ChildA()
            {
                Name = "Document2.ChildA", UndoableSometimes = "Value2"
            };
            Document2.Bs.Add(new ChildB()
            {
                Name = "Document2.ChildB[0]"
            });
            Document2.Bs.Add(new ChildB()
            {
                Name = "Document2.ChildB[1]"
            });
            Document2.Bs.Add(new ChildB()
            {
                Name = "Document2.ChildB[2]"
            });

            UndoService.Current.Clear();
        }