adds Invalid test

UIRichText reports error on console when parsing invalid xml
This commit is contained in:
Ricardo Quesada 2016-01-19 21:16:53 -08:00
parent 86e8f7d0b3
commit e36d71a24c
3 changed files with 53 additions and 1 deletions

View File

@ -526,12 +526,14 @@ bool RichText::initWithXML(const std::string& origxml)
// - makes sure that the xml well formed and starts with an element // - makes sure that the xml well formed and starts with an element
auto xml = "<font face=\"Verdana\" size=\"12\" color=\"#ffffff\">" + origxml + "</font>"; auto xml = "<font face=\"Verdana\" size=\"12\" color=\"#ffffff\">" + origxml + "</font>";
if (document.Parse(xml.c_str(), xml.length()) == tinyxml2::XML_SUCCESS) auto error = document.Parse(xml.c_str(), xml.length());
if (error == tinyxml2::XML_SUCCESS)
{ {
MyXMLVisitor visitor(this); MyXMLVisitor visitor(this);
document.Accept(&visitor); document.Accept(&visitor);
return true; return true;
} }
CCLOG("cocos2d: UI::RichText: Error parsing xml: %s, %s", document.GetErrorStr1(), document.GetErrorStr2());
} }
return false; return false;
} }

View File

@ -18,6 +18,7 @@ UIRichTextTests::UIRichTextTests()
ADD_TEST_CASE(UIRichTextXMLUrl); ADD_TEST_CASE(UIRichTextXMLUrl);
ADD_TEST_CASE(UIRichTextXMLFace); ADD_TEST_CASE(UIRichTextXMLFace);
ADD_TEST_CASE(UIRichTextXMLBR); ADD_TEST_CASE(UIRichTextXMLBR);
ADD_TEST_CASE(UIRichTextXMLInvalid);
} }
@ -797,3 +798,41 @@ void UIRichTextXMLBR::touchEvent(Ref *pSender, Widget::TouchEventType type)
break; break;
} }
} }
//
// UIRichTextXMLInvalid
//
bool UIRichTextXMLInvalid::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add the alert
Text *alert = Text::create("Invalid XML test", "fonts/Marker Felt.ttf", 30);
alert->setColor(Color3B(159, 168, 176));
alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.125));
_widget->addChild(alert);
// RichText
_richText = RichText::createWithXML("this is an invalid xml. <i>no closing tag");
if (_richText)
{
_richText->ignoreContentAdaptWithSize(false);
_richText->setContentSize(Size(100, 100));
_richText->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2));
_richText->setLocalZOrder(10);
_widget->addChild(_richText);
// test remove all children, this call won't effect the test
_richText->removeAllChildren();
}
return true;
}
return false;
}

View File

@ -139,6 +139,17 @@ protected:
cocos2d::ui::RichText* _richText; cocos2d::ui::RichText* _richText;
}; };
class UIRichTextXMLInvalid : public UIScene
{
public:
CREATE_FUNC(UIRichTextXMLInvalid);
bool init() override;
protected:
cocos2d::ui::RichText* _richText;
};
#endif /* defined(__TestCpp__UIRichTextTest__) */ #endif /* defined(__TestCpp__UIRichTextTest__) */