1 #include <QDomDocument>
2
3 #include "test/mixxxtest.h"
4 #include "skin/legacy/skincontext.h"
5
6 class SkinContextTest : public MixxxTest {
7 public:
SkinContextTest()8 SkinContextTest()
9 : m_context(config(), "test") {
10 }
11
~SkinContextTest()12 virtual ~SkinContextTest() {
13 }
14
15 protected:
16 SkinContext m_context;
17 };
18
TEST_F(SkinContextTest,TestVariable)19 TEST_F(SkinContextTest, TestVariable) {
20 // Basic check that variable lookup works.
21 m_context.setVariable("test", "asdf");
22 EXPECT_QSTRING_EQ("asdf", m_context.variable("test"));
23 }
24
TEST_F(SkinContextTest,UpdateVariables)25 TEST_F(SkinContextTest, UpdateVariables) {
26 // Verify that updateVariables works on all 3 types of <SetVariable> nodes.
27 m_context.setVariable("test", "asdf");
28 m_context.setVariable("test2", "1234");
29 m_context.setVariable("test3", "foo");
30 m_context.setVariable("test4", "bar");
31
32 QDomDocument doc;
33 QDomElement tmpl = doc.createElement("Template");
34
35 // Set test to 'zxcv'.
36 QDomElement var1 = doc.createElement("SetVariable");
37 var1.setAttribute("name", "test");
38 var1.appendChild(doc.createTextNode("zxcv"));
39 tmpl.appendChild(var1);
40
41 // Set test2 to the pattern %1_%1.
42 QDomElement var2 = doc.createElement("SetVariable");
43 var2.setAttribute("name", "test2");
44 var2.setAttribute("format", "%1_%1");
45 tmpl.appendChild(var2);
46
47 // Set test5 to the result of test3 + test4.
48 QDomElement var3 = doc.createElement("SetVariable");
49 var3.setAttribute("name", "test5");
50 var3.setAttribute("expression", "test3 + test4");
51 tmpl.appendChild(var3);
52
53 m_context.updateVariables(tmpl);
54
55 EXPECT_QSTRING_EQ("zxcv", m_context.variable("test"));
56 EXPECT_QSTRING_EQ("1234_1234", m_context.variable("test2"));
57 EXPECT_QSTRING_EQ("foo", m_context.variable("test3"));
58 EXPECT_QSTRING_EQ("bar", m_context.variable("test4"));
59 EXPECT_QSTRING_EQ("foobar", m_context.variable("test5"));
60 }
61
TEST_F(SkinContextTest,UpdateVariables_EmbeddedVariable)62 TEST_F(SkinContextTest, UpdateVariables_EmbeddedVariable) {
63 // Check that an embedded <Variable> node inside a <SetVariable> node is
64 // evaluated before assigning the variable.
65 m_context.setVariable("test", "asdf");
66 QDomDocument doc;
67 QDomElement tmpl = doc.createElement("Template");
68 QDomElement outerVar = doc.createElement("SetVariable");
69 outerVar.setAttribute("name", "test2");
70 outerVar.appendChild(doc.createTextNode("zxcv"));
71 QDomElement innerVar = doc.createElement("Variable");
72 innerVar.setAttribute("name", "test");
73 outerVar.appendChild(innerVar);
74 tmpl.appendChild(outerVar);
75
76 m_context.updateVariables(tmpl);
77
78 EXPECT_QSTRING_EQ("zxcvasdf", m_context.variable("test2"));
79 }
80
TEST_F(SkinContextTest,NoVariable)81 TEST_F(SkinContextTest, NoVariable) {
82 // Test that converting a node to string with no variable reference works.
83 QDomDocument doc;
84 QDomElement test = doc.createElement("Test");
85 test.appendChild(doc.createTextNode("Hello There"));
86 EXPECT_QSTRING_EQ("Hello There", m_context.nodeToString(test));
87 }
88
TEST_F(SkinContextTest,VariableByName)89 TEST_F(SkinContextTest, VariableByName) {
90 // Evaluate a variable by name.
91 QDomDocument doc;
92 QDomElement test = doc.createElement("Test");
93 test.appendChild(doc.createTextNode("Hello "));
94 QDomElement variableNode = doc.createElement("Variable");
95 variableNode.setAttribute("name", "name");
96 test.appendChild(variableNode);
97 m_context.setVariable("name", "Mixxx");
98 EXPECT_QSTRING_EQ("Hello Mixxx", m_context.nodeToString(test));
99 }
100
TEST_F(SkinContextTest,VariableWithFormat)101 TEST_F(SkinContextTest, VariableWithFormat) {
102 // Evaluate a variable with a format string.
103 QDomDocument doc;
104 QDomElement test = doc.createElement("Test");
105 test.appendChild(doc.createTextNode("Hello "));
106 QDomElement variableNode = doc.createElement("Variable");
107 variableNode.setAttribute("name", "name");
108 variableNode.setAttribute("format", "-%1-");
109 test.appendChild(variableNode);
110 m_context.setVariable("name", "Mixxx");
111 EXPECT_QSTRING_EQ("Hello -Mixxx-", m_context.nodeToString(test));
112 }
113
TEST_F(SkinContextTest,VariableWithExpression)114 TEST_F(SkinContextTest, VariableWithExpression) {
115 // Evaluate an ECMAScript expression in the current context.
116 QDomDocument doc;
117 QDomElement test = doc.createElement("Test");
118 test.appendChild(doc.createTextNode("Hello "));
119 QDomElement variableNode = doc.createElement("Variable");
120 variableNode.setAttribute(
121 "expression", "'Mixxx, value + 1 = ' + (parseInt(value) + 1)");
122 test.appendChild(variableNode);
123 test.appendChild(doc.createTextNode(". Isn't that great?"));
124 m_context.setVariable("name", "Mixxx");
125 m_context.setVariable("value", "2");
126 EXPECT_QSTRING_EQ("Hello Mixxx, value + 1 = 3. Isn't that great?",
127 m_context.nodeToString(test));
128 }
129