1 /// Unit tests for canvas::Element
2 #define BOOST_TEST_MODULE canvas
3 #include <BoostTestTargetConfig.h>
4
5 #include "CanvasElement.hxx"
6 #include "CanvasGroup.hxx"
7
8 namespace sc = simgear::canvas;
9
BOOST_AUTO_TEST_CASE(attr_data)10 BOOST_AUTO_TEST_CASE( attr_data )
11 {
12 // http://www.w3.org/TR/html5/dom.html#attr-data-*
13
14 #define SG_CHECK_ATTR2PROP(attr, prop)\
15 BOOST_CHECK_EQUAL(sc::Element::attrToDataPropName(attr), prop)
16
17 // If name starts with "data-", for each "-" (U+002D) character in the name
18 // that is followed by a lowercase ASCII letter, remove the "-" (U+002D)
19 // character and replace the character that followed it by the same character
20 // converted to ASCII uppercase.
21
22 SG_CHECK_ATTR2PROP("no-data", "");
23 SG_CHECK_ATTR2PROP("data-blub", "blub");
24 SG_CHECK_ATTR2PROP("data-blub-x-y", "blubXY");
25 SG_CHECK_ATTR2PROP("data-blub-x-y-", "blubXY-");
26
27 #undef SG_CHECK_ATTR2PROP
28
29 #define SG_CHECK_PROP2ATTR(prop, attr)\
30 BOOST_CHECK_EQUAL(sc::Element::dataPropToAttrName(prop), attr)
31
32 // If name contains a "-" (U+002D) character followed by a lowercase ASCII
33 // letter, throw a SyntaxError exception (empty string) and abort these steps.
34 // For each uppercase ASCII letter in name, insert a "-" (U+002D) character
35 // before the character and replace the character with the same character
36 // converted to ASCII lowercase.
37 // Insert the string "data-" at the front of name.
38
39 SG_CHECK_PROP2ATTR("test", "data-test");
40 SG_CHECK_PROP2ATTR("testIt", "data-test-it");
41 SG_CHECK_PROP2ATTR("testIt-Hyphen", "data-test-it--hyphen");
42 SG_CHECK_PROP2ATTR("-test", "");
43 SG_CHECK_PROP2ATTR("test-", "data-test-");
44
45 #undef SG_CHECK_PROP2ATTR
46
47 SGPropertyNode_ptr node = new SGPropertyNode;
48 sc::ElementPtr el =
49 sc::Element::create<sc::Group>(sc::CanvasWeakPtr(), node);
50
51 el->setDataProp("myData", 3);
52 BOOST_CHECK_EQUAL( el->getDataProp<int>("myData"), 3 );
53 BOOST_CHECK_EQUAL( node->getIntValue("data-my-data"), 3 );
54
55 SGPropertyNode* prop = el->getDataProp<SGPropertyNode*>("notExistingProp");
56 BOOST_CHECK( !prop );
57 prop = el->getDataProp<SGPropertyNode*>("myData");
58 BOOST_CHECK( prop );
59 BOOST_CHECK_EQUAL( prop->getParent(), node );
60 BOOST_CHECK_EQUAL( prop->getIntValue(), 3 );
61
62 BOOST_CHECK( el->hasDataProp("myData") );
63 el->removeDataProp("myData");
64 BOOST_CHECK( !el->hasDataProp("myData") );
65 BOOST_CHECK_EQUAL( el->getDataProp("myData", 5), 5 );
66 }
67