|
18 | 18 | using NUnit.Framework;
|
19 | 19 | using UnityEngine;
|
20 | 20 |
|
21 |
| -namespace com.google.apps.peltzer.client.model.core { |
22 |
| - |
23 |
| - [TestFixture] |
24 |
| - // Tests for Command and implementations. |
25 |
| - public class CommandTest { |
26 |
| - |
27 |
| - [Test] |
28 |
| - public void TestCompositeMove() { |
29 |
| - int meshOne = 1; |
30 |
| - int meshTwo = 2; |
31 |
| - |
32 |
| - Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
33 |
| - |
34 |
| - // Add two meshes. |
35 |
| - model.ApplyCommand(new AddMeshCommand(Primitives.AxisAlignedBox( |
36 |
| - meshOne, Vector3.zero, Vector3.one, /* materialId */ 3))); |
37 |
| - model.ApplyCommand(new AddMeshCommand(Primitives.AxisAlignedBox( |
38 |
| - meshTwo, Vector3.one, Vector3.one, /* materialId */ 4))); |
39 |
| - |
40 |
| - // Check the meshes are where we think they are. |
41 |
| - NUnit.Framework.Assert.Less( |
42 |
| - Vector3.Distance(model.GetMesh(meshOne).offset, |
43 |
| - new Vector3(0, 0, 0)), 0.001f); |
44 |
| - NUnit.Framework.Assert.Less( |
45 |
| - Vector3.Distance(model.GetMesh(meshTwo).offset, |
46 |
| - new Vector3(1, 1, 1)), 0.001f); |
47 |
| - |
48 |
| - // Make and apply a CompositeCommand that moves both of them. |
49 |
| - MoveMeshCommand move1 = new MoveMeshCommand( |
50 |
| - meshOne, new Vector3(1, 0, 0), Quaternion.identity); |
51 |
| - MoveMeshCommand move2 = new MoveMeshCommand( |
52 |
| - meshTwo, new Vector3(0, 1, 0), Quaternion.identity); |
53 |
| - List<Command> commands = new List<Command>(); |
54 |
| - commands.Add(move1); |
55 |
| - commands.Add(move2); |
56 |
| - |
57 |
| - model.ApplyCommand(new CompositeCommand(commands)); |
58 |
| - |
59 |
| - // Check the meshes' new locations. |
60 |
| - NUnit.Framework.Assert.Less( |
61 |
| - Vector3.Distance(model.GetMesh(meshOne).offset, |
62 |
| - new Vector3(1, 0, 0)), 0.001f); |
63 |
| - NUnit.Framework.Assert.Less( |
64 |
| - Vector3.Distance(model.GetMesh(meshTwo).offset, |
65 |
| - new Vector3(1, 2, 1)), 0.001f); |
66 |
| - |
67 |
| - // Undo and make sure they are back. |
68 |
| - model.Undo(); |
69 |
| - NUnit.Framework.Assert.Less( |
70 |
| - Vector3.Distance(model.GetMesh(meshOne).offset, |
71 |
| - new Vector3(0, 0, 0)), 0.001f); |
72 |
| - NUnit.Framework.Assert.Less( |
73 |
| - Vector3.Distance(model.GetMesh(meshTwo).offset, |
74 |
| - new Vector3(1, 1, 1)), 0.001f); |
| 21 | +namespace com.google.apps.peltzer.client.model.core |
| 22 | +{ |
| 23 | + |
| 24 | + [TestFixture] |
| 25 | + // Tests for Command and implementations. |
| 26 | + public class CommandTest |
| 27 | + { |
| 28 | + |
| 29 | + [Test] |
| 30 | + public void TestCompositeMove() |
| 31 | + { |
| 32 | + int meshOne = 1; |
| 33 | + int meshTwo = 2; |
| 34 | + |
| 35 | + Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
| 36 | + |
| 37 | + // Add two meshes. |
| 38 | + model.ApplyCommand(new AddMeshCommand(Primitives.AxisAlignedBox( |
| 39 | + meshOne, Vector3.zero, Vector3.one, /* materialId */ 3))); |
| 40 | + model.ApplyCommand(new AddMeshCommand(Primitives.AxisAlignedBox( |
| 41 | + meshTwo, Vector3.one, Vector3.one, /* materialId */ 4))); |
| 42 | + |
| 43 | + // Check the meshes are where we think they are. |
| 44 | + NUnit.Framework.Assert.Less( |
| 45 | + Vector3.Distance(model.GetMesh(meshOne).offset, |
| 46 | + new Vector3(0, 0, 0)), 0.001f); |
| 47 | + NUnit.Framework.Assert.Less( |
| 48 | + Vector3.Distance(model.GetMesh(meshTwo).offset, |
| 49 | + new Vector3(1, 1, 1)), 0.001f); |
| 50 | + |
| 51 | + // Make and apply a CompositeCommand that moves both of them. |
| 52 | + MoveMeshCommand move1 = new MoveMeshCommand( |
| 53 | + meshOne, new Vector3(1, 0, 0), Quaternion.identity); |
| 54 | + MoveMeshCommand move2 = new MoveMeshCommand( |
| 55 | + meshTwo, new Vector3(0, 1, 0), Quaternion.identity); |
| 56 | + List<Command> commands = new List<Command>(); |
| 57 | + commands.Add(move1); |
| 58 | + commands.Add(move2); |
| 59 | + |
| 60 | + model.ApplyCommand(new CompositeCommand(commands)); |
| 61 | + |
| 62 | + // Check the meshes' new locations. |
| 63 | + NUnit.Framework.Assert.Less( |
| 64 | + Vector3.Distance(model.GetMesh(meshOne).offset, |
| 65 | + new Vector3(1, 0, 0)), 0.001f); |
| 66 | + NUnit.Framework.Assert.Less( |
| 67 | + Vector3.Distance(model.GetMesh(meshTwo).offset, |
| 68 | + new Vector3(1, 2, 1)), 0.001f); |
| 69 | + |
| 70 | + // Undo and make sure they are back. |
| 71 | + model.Undo(); |
| 72 | + NUnit.Framework.Assert.Less( |
| 73 | + Vector3.Distance(model.GetMesh(meshOne).offset, |
| 74 | + new Vector3(0, 0, 0)), 0.001f); |
| 75 | + NUnit.Framework.Assert.Less( |
| 76 | + Vector3.Distance(model.GetMesh(meshTwo).offset, |
| 77 | + new Vector3(1, 1, 1)), 0.001f); |
| 78 | + } |
| 79 | + |
| 80 | + [Test] |
| 81 | + public void TestCompositeUndo() |
| 82 | + { |
| 83 | + int meshOne = 1; |
| 84 | + |
| 85 | + Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
| 86 | + |
| 87 | + // Add a Mesh. |
| 88 | + model.ApplyCommand(new AddMeshCommand(Primitives.AxisAlignedBox( |
| 89 | + meshOne, Vector3.zero, Vector3.one, /* materialId */ 3))); |
| 90 | + |
| 91 | + // Create a CompositeCommand that deletes and adds the same mesh. |
| 92 | + List<Command> commands = new List<Command>(); |
| 93 | + commands.Add(new DeleteMeshCommand(meshOne)); |
| 94 | + commands.Add(new AddMeshCommand(Primitives.AxisAlignedBox( |
| 95 | + meshOne, Vector3.zero, Vector3.one, /* materialId */ 3))); |
| 96 | + |
| 97 | + CompositeCommand composite = new CompositeCommand(commands); |
| 98 | + |
| 99 | + CompositeCommand undoCommand = |
| 100 | + (CompositeCommand)composite.GetUndoCommand(model); |
| 101 | + |
| 102 | + List<Command> undoList = undoCommand.GetCommands(); |
| 103 | + |
| 104 | + // The order of commands should be the same, we invert the commands |
| 105 | + // *and* invert the order. |
| 106 | + NUnit.Framework.Assert.NotNull(undoList[1] as AddMeshCommand); |
| 107 | + NUnit.Framework.Assert.NotNull(undoList[0] as DeleteMeshCommand); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public void TestRotation() |
| 112 | + { |
| 113 | + Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
| 114 | + |
| 115 | + MMesh mesh = Primitives.AxisAlignedBox(1, Vector3.zero, Vector3.one, /* materialId */ 2); |
| 116 | + model.AddMesh(mesh); |
| 117 | + |
| 118 | + CheckBounds(model.GetMesh(1).bounds, new Bounds(Vector3.zero, Vector3.one * 2.0f)); |
| 119 | + |
| 120 | + MoveMeshCommand cmd = new MoveMeshCommand(1, Vector3.zero, Quaternion.AngleAxis(45, Vector3.up)); |
| 121 | + model.ApplyCommand(cmd); |
| 122 | + // Bounds should have grown, since the box is no longer axis-aligned. |
| 123 | + float extends = 2.0f * Mathf.Sqrt(2); |
| 124 | + CheckBounds(model.GetMesh(1).bounds, new Bounds(Vector3.zero, new Vector3(extends, 2.0f, extends))); |
| 125 | + |
| 126 | + model.Undo(); |
| 127 | + // Make sure we are back: |
| 128 | + CheckBounds(model.GetMesh(1).bounds, new Bounds(Vector3.zero, Vector3.one * 2.0f)); |
| 129 | + } |
| 130 | + |
| 131 | + private void CheckBounds(Bounds left, Bounds right) |
| 132 | + { |
| 133 | + NUnit.Framework.Assert.Less(Vector3.Distance(left.center, right.center), 0.0001); |
| 134 | + NUnit.Framework.Assert.Less(Vector3.Distance(left.extents, right.extents), 0.0001); |
| 135 | + } |
| 136 | + |
| 137 | + [Test] |
| 138 | + public void TestChangeFacePropertiesCommand() |
| 139 | + { |
| 140 | + Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
| 141 | + |
| 142 | + int baseMaterialId = 2; |
| 143 | + int newMaterial1 = 3; |
| 144 | + int newMaterial2 = 4; |
| 145 | + MMesh mesh = Primitives.AxisAlignedBox(1, Vector3.zero, Vector3.one, baseMaterialId); |
| 146 | + model.AddMesh(mesh); |
| 147 | + |
| 148 | + Dictionary<int, FaceProperties> props = new Dictionary<int, FaceProperties>(); |
| 149 | + props[1] = new FaceProperties(newMaterial1); |
| 150 | + props[2] = new FaceProperties(newMaterial2); |
| 151 | + |
| 152 | + ChangeFacePropertiesCommand command = new ChangeFacePropertiesCommand(1, props); |
| 153 | + |
| 154 | + // Apply the command, ensure the mesh was updated. |
| 155 | + model.ApplyCommand(command); |
| 156 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(0).properties.materialId, baseMaterialId); |
| 157 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(1).properties.materialId, newMaterial1); |
| 158 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(2).properties.materialId, newMaterial2); |
| 159 | + |
| 160 | + // Undo, faces should be back to normal. |
| 161 | + model.Undo(); |
| 162 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(0).properties.materialId, baseMaterialId); |
| 163 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(1).properties.materialId, baseMaterialId); |
| 164 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(2).properties.materialId, baseMaterialId); |
| 165 | + |
| 166 | + // Redo, changes should be applied again. |
| 167 | + model.Redo(); |
| 168 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(0).properties.materialId, baseMaterialId); |
| 169 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(1).properties.materialId, newMaterial1); |
| 170 | + NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(2).properties.materialId, newMaterial2); |
| 171 | + } |
75 | 172 | }
|
76 |
| - |
77 |
| - [Test] |
78 |
| - public void TestCompositeUndo() { |
79 |
| - int meshOne = 1; |
80 |
| - |
81 |
| - Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
82 |
| - |
83 |
| - // Add a Mesh. |
84 |
| - model.ApplyCommand(new AddMeshCommand(Primitives.AxisAlignedBox( |
85 |
| - meshOne, Vector3.zero, Vector3.one, /* materialId */ 3))); |
86 |
| - |
87 |
| - // Create a CompositeCommand that deletes and adds the same mesh. |
88 |
| - List<Command> commands = new List<Command>(); |
89 |
| - commands.Add(new DeleteMeshCommand(meshOne)); |
90 |
| - commands.Add(new AddMeshCommand(Primitives.AxisAlignedBox( |
91 |
| - meshOne, Vector3.zero, Vector3.one, /* materialId */ 3))); |
92 |
| - |
93 |
| - CompositeCommand composite = new CompositeCommand(commands); |
94 |
| - |
95 |
| - CompositeCommand undoCommand = |
96 |
| - (CompositeCommand) composite.GetUndoCommand(model); |
97 |
| - |
98 |
| - List<Command> undoList = undoCommand.GetCommands(); |
99 |
| - |
100 |
| - // The order of commands should be the same, we invert the commands |
101 |
| - // *and* invert the order. |
102 |
| - NUnit.Framework.Assert.NotNull(undoList[1] as AddMeshCommand); |
103 |
| - NUnit.Framework.Assert.NotNull(undoList[0] as DeleteMeshCommand); |
104 |
| - } |
105 |
| - |
106 |
| - [Test] |
107 |
| - public void TestRotation() { |
108 |
| - Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
109 |
| - |
110 |
| - MMesh mesh = Primitives.AxisAlignedBox(1, Vector3.zero, Vector3.one, /* materialId */ 2); |
111 |
| - model.AddMesh(mesh); |
112 |
| - |
113 |
| - CheckBounds(model.GetMesh(1).bounds, new Bounds(Vector3.zero, Vector3.one * 2.0f)); |
114 |
| - |
115 |
| - MoveMeshCommand cmd = new MoveMeshCommand(1, Vector3.zero, Quaternion.AngleAxis(45, Vector3.up)); |
116 |
| - model.ApplyCommand(cmd); |
117 |
| - // Bounds should have grown, since the box is no longer axis-aligned. |
118 |
| - float extends = 2.0f * Mathf.Sqrt(2); |
119 |
| - CheckBounds(model.GetMesh(1).bounds, new Bounds(Vector3.zero, new Vector3(extends, 2.0f, extends))); |
120 |
| - |
121 |
| - model.Undo(); |
122 |
| - // Make sure we are back: |
123 |
| - CheckBounds(model.GetMesh(1).bounds, new Bounds(Vector3.zero, Vector3.one * 2.0f)); |
124 |
| - } |
125 |
| - |
126 |
| - private void CheckBounds(Bounds left, Bounds right) { |
127 |
| - NUnit.Framework.Assert.Less(Vector3.Distance(left.center, right.center), 0.0001); |
128 |
| - NUnit.Framework.Assert.Less(Vector3.Distance(left.extents, right.extents), 0.0001); |
129 |
| - } |
130 |
| - |
131 |
| - [Test] |
132 |
| - public void TestChangeFacePropertiesCommand() { |
133 |
| - Model model = new Model(new Bounds(Vector3.zero, Vector3.one * 10)); |
134 |
| - |
135 |
| - int baseMaterialId = 2; |
136 |
| - int newMaterial1 = 3; |
137 |
| - int newMaterial2 = 4; |
138 |
| - MMesh mesh = Primitives.AxisAlignedBox(1, Vector3.zero, Vector3.one, baseMaterialId); |
139 |
| - model.AddMesh(mesh); |
140 |
| - |
141 |
| - Dictionary<int, FaceProperties> props = new Dictionary<int, FaceProperties>(); |
142 |
| - props[1] = new FaceProperties(newMaterial1); |
143 |
| - props[2] = new FaceProperties(newMaterial2); |
144 |
| - |
145 |
| - ChangeFacePropertiesCommand command = new ChangeFacePropertiesCommand(1, props); |
146 |
| - |
147 |
| - // Apply the command, ensure the mesh was updated. |
148 |
| - model.ApplyCommand(command); |
149 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(0).properties.materialId, baseMaterialId); |
150 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(1).properties.materialId, newMaterial1); |
151 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(2).properties.materialId, newMaterial2); |
152 |
| - |
153 |
| - // Undo, faces should be back to normal. |
154 |
| - model.Undo(); |
155 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(0).properties.materialId, baseMaterialId); |
156 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(1).properties.materialId, baseMaterialId); |
157 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(2).properties.materialId, baseMaterialId); |
158 |
| - |
159 |
| - // Redo, changes should be applied again. |
160 |
| - model.Redo(); |
161 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(0).properties.materialId, baseMaterialId); |
162 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(1).properties.materialId, newMaterial1); |
163 |
| - NUnit.Framework.Assert.AreEqual(model.GetMesh(1).GetFace(2).properties.materialId, newMaterial2); |
164 |
| - } |
165 |
| - } |
166 | 173 | }
|
0 commit comments