forked from autodesk-forks/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObserver.cpp
156 lines (137 loc) · 5.61 KB
/
Observer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// TM & (c) 2017 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXTest/Catch/catch.hpp>
#include <MaterialXCore/Observer.h>
#include <MaterialXFormat/XmlIo.h>
namespace mx = MaterialX;
TEST_CASE("Observer", "[observer]")
{
class DocObserver : public mx::Observer
{
public:
DocObserver() :
_beginUpdateCount(0),
_endUpdateCount(0),
_addElementCount(0),
_removeElementCount(0),
_setAttributeCount(0),
_removeAttributeCount(0),
_copyContentCount(0),
_clearContentCount(0),
_readCount(0),
_writeCount(0)
{
}
void onBeginUpdate() override { _beginUpdateCount++; }
void onEndUpdate() override { _endUpdateCount++; }
void onAddElement(mx::ElementPtr parent, mx::ElementPtr elem) override { _addElementCount++; }
void onRemoveElement(mx::ElementPtr parent, mx::ElementPtr elem) override { _removeElementCount++; }
void onSetAttribute(mx::ElementPtr elem, const std::string&, const std::string&) override { _setAttributeCount++; }
void onRemoveAttribute(mx::ElementPtr elem, const std::string&) override { _removeAttributeCount++; }
void onCopyContent(mx::ElementPtr elem) override { _copyContentCount++; }
void onClearContent(mx::ElementPtr elem) override { _clearContentCount++; }
void onRead() override { _readCount++; }
void onWrite() override { _writeCount++; }
void clear()
{
_beginUpdateCount = 0;
_endUpdateCount = 0;
_addElementCount = 0;
_setAttributeCount = 0;
_removeElementCount = 0;
_removeAttributeCount = 0;
_copyContentCount = 0;
_clearContentCount = 0;
_readCount = 0;
_writeCount = 0;
}
void verifyCountsPreWrite()
{
REQUIRE(_beginUpdateCount == 23);
REQUIRE(_endUpdateCount == 23);
REQUIRE(_addElementCount == 11);
REQUIRE(_setAttributeCount == 12);
REQUIRE(_removeElementCount == 0);
REQUIRE(_removeAttributeCount == 0);
REQUIRE(_copyContentCount == 0);
REQUIRE(_clearContentCount == 0);
REQUIRE(_readCount == 0);
REQUIRE(_writeCount == 0);
}
void verifyCountsPostRead()
{
REQUIRE(_beginUpdateCount == 4);
REQUIRE(_endUpdateCount == 4);
REQUIRE(_addElementCount == 11);
REQUIRE(_setAttributeCount == 13);
REQUIRE(_removeElementCount == 3);
REQUIRE(_removeAttributeCount == 0);
REQUIRE(_copyContentCount == 0);
REQUIRE(_clearContentCount == 1);
REQUIRE(_readCount == 1);
REQUIRE(_writeCount == 1);
}
void verifyCountsDisabled()
{
REQUIRE(_beginUpdateCount == 0);
REQUIRE(_endUpdateCount == 0);
REQUIRE(_addElementCount == 0);
REQUIRE(_setAttributeCount == 0);
REQUIRE(_removeElementCount == 0);
REQUIRE(_removeAttributeCount == 0);
REQUIRE(_copyContentCount == 0);
REQUIRE(_clearContentCount == 0);
REQUIRE(_readCount == 0);
REQUIRE(_writeCount == 0);
}
protected:
// Set of counts for verification.
unsigned int _beginUpdateCount;
unsigned int _endUpdateCount;
unsigned int _addElementCount;
unsigned int _removeElementCount;
unsigned int _setAttributeCount;
unsigned int _removeAttributeCount;
unsigned int _copyContentCount;
unsigned int _clearContentCount;
unsigned int _readCount;
unsigned int _writeCount;
};
// Create an observed document.
mx::ObservedDocumentPtr doc = mx::Document::createDocument<mx::ObservedDocument>();
// Register a single observer.
std::shared_ptr<DocObserver> testObserver = std::make_shared<DocObserver>();
doc->addObserver("testObserver", testObserver);
// Create a node graph with a constant color output.
mx::NodeGraphPtr nodeGraph = doc->addNodeGraph();
mx::NodePtr constant = nodeGraph->addNode("constant");
constant->setParameterValue("value", mx::Color3(0.1f, 0.2f, 0.3f));
mx::OutputPtr output = nodeGraph->addOutput();
output->setConnectedNode(constant);
// Create a simple shader interface.
mx::NodeDefPtr shader = doc->addNodeDef("ND_simpleSrf", "surfaceshader", "simpleSrf");
shader->addInput("diffColor", "color3");
shader->addInput("specColor", "color3");
shader->addParameter("roughness", "float");
// Create a material that instantiates the shader.
mx::MaterialPtr material = doc->addMaterial();
material->addShaderRef("", "simpleSrf");
REQUIRE(material->getShaderRefs().size() == 1);
// Check that observer tracked the correct number of changes of each type
testObserver->verifyCountsPreWrite();
testObserver->clear();
// Serialize and deserialize the document.
std::string xmlString = mx::writeToXmlString(doc);
doc->initialize();
mx::readFromXmlString(doc, xmlString);
// Check that observer tracked the correct number of changes during initialize and read
testObserver->verifyCountsPostRead();
// Check observer callback disabling. All counts should be 0 after being cleared.
testObserver->clear();
doc->disableCallbacks();
doc->initialize();
mx::readFromXmlString(doc, xmlString);
testObserver->verifyCountsDisabled();
}