public void TestTypeScope() { Init(users: 3); var array0 = Arrays[Users[0]]; var text0 = new YText(); var text1 = new YText(); array0.Insert(0, new[] { text0, text1 }); var undoManager = new UndoManager(text0); var undoManagerBoth = new UndoManager(new[] { text0, text1 }, 500, it => true, new HashSet <object> { null }); text1.Insert(0, "abc"); Assert.AreEqual(0, undoManager.Count); Assert.AreEqual(1, undoManagerBoth.Count); Assert.AreEqual("abc", text1.ToString()); undoManager.Undo(); Assert.AreEqual("abc", text1.ToString()); undoManagerBoth.Undo(); Assert.AreEqual(string.Empty, text1.ToString()); }
public void TestUndoText() { Init(users: 3); var text0 = Texts[Users[0]]; var text1 = Texts[Users[1]]; var undoManager = new UndoManager(text0); // Items that are added and deleted in the same transaction won't be undone. text0.Insert(0, "test"); text0.Delete(0, 4); undoManager.Undo(); Assert.AreEqual(string.Empty, text0.ToString()); // Follow redone items. text0.Insert(0, "a"); undoManager.StopCapturing(); text0.Delete(0, 1); undoManager.StopCapturing(); undoManager.Undo(); Assert.AreEqual("a", text0.ToString()); undoManager.Undo(); Assert.AreEqual(string.Empty, text0.ToString()); text0.Insert(0, "abc"); text1.Insert(0, "xyz"); Connector.SyncAll(); undoManager.Undo(); Assert.AreEqual("xyz", text0.ToString()); undoManager.Redo(); Assert.AreEqual("abcxyz", text0.ToString()); Connector.SyncAll(); text1.Delete(0, 1); Connector.SyncAll(); undoManager.Undo(); Assert.AreEqual("xyz", text0.ToString()); undoManager.Redo(); Assert.AreEqual("bcxyz", text0.ToString()); // Test formats. text0.Format(1, 3, new Dictionary <string, object> { { "bold", true } }); var delta = text0.ToDelta(); Assert.AreEqual(3, delta?.Count); Assert.AreEqual("b", delta[0].Insert); Assert.IsNull(delta[0].Attributes); Assert.AreEqual("cxy", delta[1].Insert); Assert.AreEqual(true, delta[1].Attributes["bold"]); undoManager.Undo(); delta = text0.ToDelta(); Assert.AreEqual(1, delta?.Count); Assert.AreEqual("bcxyz", delta[0].Insert); Assert.IsNull(delta[0].Attributes); undoManager.Redo(); delta = text0.ToDelta(); Assert.AreEqual(3, delta?.Count); Assert.AreEqual("b", delta[0].Insert); Assert.IsNull(delta[0].Attributes); Assert.AreEqual("cxy", delta[1].Insert); Assert.AreEqual(true, delta[1].Attributes["bold"]); }
public void TestUndoArray() { Init(users: 3); var array0 = Arrays[Users[0]]; var array1 = Arrays[Users[1]]; var undoManager = new UndoManager(array0); array0.Insert(0, new object[] { 1, 2, 3 }); array1.Insert(9, new object[] { 4, 5, 6 }); Connector.SyncAll(); CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, (ICollection)array0.ToArray()); undoManager.Undo(); CollectionAssert.AreEqual(new[] { 4, 5, 6 }, (ICollection)array0.ToArray()); undoManager.Redo(); CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, (ICollection)array0.ToArray()); Connector.SyncAll(); // User1 deletes [1] array1.Delete(0, 1); Connector.SyncAll(); undoManager.Undo(); CollectionAssert.AreEqual(new[] { 4, 5, 6 }, (ICollection)array0.ToArray()); undoManager.Redo(); CollectionAssert.AreEqual(new[] { 2, 3, 4, 5, 6 }, (ICollection)array0.ToArray()); array0.Delete(0, 5); // Test nested structure. var ymap = new YMap(); array0.Insert(0, new[] { ymap }); Assert.AreEqual(0, (array0.Get(0) as YMap)?.Count); undoManager.StopCapturing(); ymap.Set("a", 1); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Count); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Get("a")); undoManager.Undo(); Assert.AreEqual(0, (array0.Get(0) as YMap)?.Count); undoManager.Undo(); CollectionAssert.AreEqual(new[] { 2, 3, 4, 5, 6 }, (ICollection)array0.ToArray()); undoManager.Redo(); Assert.AreEqual(0, (array0.Get(0) as YMap)?.Count); undoManager.Redo(); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Count); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Get("a")); Connector.SyncAll(); (array1.Get(0) as YMap).Set("b", 2); Connector.SyncAll(); Assert.AreEqual(2, (array0.Get(0) as YMap)?.Count); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Get("a")); Assert.AreEqual(2, (array0.Get(0) as YMap)?.Get("b")); undoManager.Undo(); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Count); Assert.AreEqual(2, (array0.Get(0) as YMap)?.Get("b")); undoManager.Undo(); CollectionAssert.AreEqual(new[] { 2, 3, 4, 5, 6 }, (ICollection)array0.ToArray()); undoManager.Redo(); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Count); Assert.AreEqual(2, (array0.Get(0) as YMap)?.Get("b")); undoManager.Redo(); Assert.AreEqual(2, (array0.Get(0) as YMap)?.Count); Assert.AreEqual(1, (array0.Get(0) as YMap)?.Get("a")); Assert.AreEqual(2, (array0.Get(0) as YMap)?.Get("b")); }