From 77ada99cd53fd5884cf9779a5a04455e74512e16 Mon Sep 17 00:00:00 2001 From: Jakub Izydorczyk Date: Fri, 13 Mar 2015 07:35:45 +0100 Subject: [PATCH] Add missing unit tests for manifest util 10/12 tests PASSES GetNodeText tests fails due to double addition of bidi control characters Change-Id: I766827bd7b61da469fd38376bdee1c6715034e2f --- ...idget_manifest_parser_manifest_util_unittest.cc | 212 ++++++++++++++++++++- src/widget-manifest-parser/manifest_util.cc | 27 +-- src/widget-manifest-parser/manifest_util.h | 10 + 3 files changed, 230 insertions(+), 19 deletions(-) diff --git a/src/unit_tests/widget_manifest_parser_manifest_util_unittest.cc b/src/unit_tests/widget_manifest_parser_manifest_util_unittest.cc index ab62cd5..ac3c314 100644 --- a/src/unit_tests/widget_manifest_parser_manifest_util_unittest.cc +++ b/src/unit_tests/widget_manifest_parser_manifest_util_unittest.cc @@ -11,6 +11,7 @@ #include "widget-manifest-parser/application_data.h" #include "widget-manifest-parser/application_manifest_constants.h" #include "widget-manifest-parser/manifest.h" + #include "widget-manifest-parser/manifest_util.h" using common_installer::widget_manifest_parser::ApplicationData; @@ -20,7 +21,7 @@ namespace keys = common_installer::application_manifest_keys; namespace bf = boost::filesystem; namespace common_installer { -namespace widget_mnanifest_parser { +namespace widget_manifest_parser { class ManifestUtilTest : public testing::Test { }; @@ -59,5 +60,212 @@ TEST_F(ManifestUtilTest, ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); } -} // namespace widget_mnanifest_parser +// Tests IsPropSupportDir method +TEST_F(ManifestUtilTest, IsPropSupportDirTest) { + xmlChar kWidgetNodeKey[] = "widget"; + xmlChar kNameNodeKey[] = "name"; + xmlChar kVersionAttributeKey[] = "version"; + xmlChar kShortAttributeKey[] = "short"; + xmlNode node, sec_node; + node.name = kWidgetNodeKey; + sec_node.name = kNameNodeKey; + xmlAttr attr, sec_attr; + attr.name = kVersionAttributeKey; + sec_attr.name = kShortAttributeKey; + ASSERT_TRUE(IsPropSupportDir(&node, &attr)); + ASSERT_TRUE(IsPropSupportDir(&sec_node, &sec_attr)); + ASSERT_TRUE(!IsPropSupportDir(&node, &sec_attr)); + ASSERT_TRUE(!IsPropSupportDir(&sec_node, &attr)); + ASSERT_TRUE(!IsPropSupportDir(&node, &sec_attr)); +} + +// Tests IsElementSupportSpanAndDir method +TEST_F(ManifestUtilTest, IsElementSupportSpanAndDirTest) { + std::map nodeNames = { + { "name", true }, + { "description", true }, + { "author", true }, + { "license", true }, + { "badlicense", false } + }; + xmlNode node; + for (const auto& p : nodeNames) { + node.name = reinterpret_cast(const_cast(p.first)); + EXPECT_TRUE(IsElementSupportSpanAndDir(&node) == p.second); + } +} + +// Tests IsSingletonElement method +TEST_F(ManifestUtilTest, IsSingletonElementTest) { + std::map kSingletonElements = { + { "allow-navigation", true }, + { "author", true }, + { "content-security-policy-report-only", true }, + { "content-security-policy", true }, + { "content", false }, + { "badsingleton", false } + }; + for (const auto& p : kSingletonElements) + EXPECT_TRUE(IsSingletonElement(p.first) == p.second); +} + +// Tests IsTrimRequiredForElement +TEST_F(ManifestUtilTest, IsTrimRequiredForElementTest) { + std::map elements { + { "name", true }, + { "author", true }, + { "badname", false }, + { "badauthor", false } + }; + xmlNode node; + for (const auto& p : elements) { + node.name = reinterpret_cast(const_cast(p.first)); + EXPECT_TRUE(IsTrimRequiredForElement(&node) == p.second); + } +} + +// Tests IsTrimRequiredForProp method +TEST_F(ManifestUtilTest, IsTrimRequiredForPropTest) { + struct node{ + bool operator <(const node& rhs) const { + return node_name_ < rhs.node_name_; + } + const char* node_name_; + const char* attr_name_; + }; + + std::map nodes = { + { {"widget", "id"}, true }, + { {"widget", "version"}, true }, + { {"widget", "defaultlocale"}, true }, + { {"name", "short"}, true }, + { {"author", "email"}, true }, + { {"author", "href"}, true }, + { {"license", "href"}, true }, + { {"icon", "path"}, true }, + { {"widget", "email"}, false }, + { {"name", "path"}, false }, + { {"author", "id"}, false } + }; + + xmlNode node; + xmlAttr attr; + for (const auto& p : nodes) { + node.name = + reinterpret_cast(const_cast(p.first.node_name_)); + attr.name = + reinterpret_cast(const_cast(p.first.attr_name_)); + EXPECT_TRUE(IsTrimRequiredForProp(&node, &attr) == p.second); + } +} + +// Tests GetNodeDir method with proper values +TEST_F(ManifestUtilTest, GetNodeDirTestProperValues) { + const char* xml = "" + "ppa emoS" + ""; + xmlDoc* doc = xmlParseMemory(xml, strlen(xml)); + ASSERT_TRUE(doc); + xmlNode* root = doc->children; + ASSERT_TRUE(root); + std::string inherit_dir("ltr"); + EXPECT_STREQ("rtl", GetNodeDir(root, inherit_dir).c_str()); +} + +// Tests GetNodeDir method with default dir +TEST_F(ManifestUtilTest, GetNodeDirTestDefaultValues) { + const char* xml = "" + "Some app" + ""; + xmlDoc* doc = xmlParseMemory(xml, strlen(xml)); + ASSERT_TRUE(doc); + xmlNode* root = doc->children; + ASSERT_TRUE(root); + std::string inherit_dir("ltr"); + EXPECT_STREQ("ltr", GetNodeDir(root, inherit_dir).c_str()); +} + +// Tests GetNodeText method with rtl text direction +TEST_F(ManifestUtilTest, GetNodeTextTestXmlElementNode) { + const char* xml = "" + "ppa emoS" + ""; + std::map control_chars = { + { '\xe2', 0 }, + { '\x80', 1 }, + { '\xab', 2 }, + { '\xe2', 11 }, + { '\x80', 12 }, + { '\xac', 13 }, + }; + xmlDoc* doc = xmlParseMemory(xml, strlen(xml)); + ASSERT_TRUE(doc); + xmlNode* root = doc->children; + ASSERT_TRUE(root); + std::string inherit_dir("ltr"); + std::string s = GetNodeText(root, inherit_dir); + for (const auto& p : control_chars) + EXPECT_EQ(p.first, s[p.second]); +} + +// Tests GetNodeText method with rtl and ltr text direction +TEST_F(ManifestUtilTest, GetNodeTextTestTwoXmlElementNodes) { + const char* xml = "" + "ppa emoS" + "Desc" + ""; + std::map control_chars = { + { '\xe2', 0 }, { '\x80', 1 }, + { '\xab', 2 }, { '\xe2', 11 }, + { '\x80', 12 }, { '\xac', 13 }, + { '\xe2', 14 }, { '\x80', 15 }, + { '\xaa', 16 }, { '\xe2', 21 }, + { '\x80', 22 }, { '\xac', 23 }, + }; + xmlDoc* doc = xmlParseMemory(xml, strlen(xml)); + ASSERT_TRUE(doc); + xmlNode* root = doc->children; + ASSERT_TRUE(root); + std::string inherit_dir("ltr"); + std::string s = GetNodeText(root, inherit_dir); + for (const auto& p : control_chars) + EXPECT_EQ(p.first, s[p.second]); +} + +// Tests LoadXMLNode method with proper xml tree +TEST_F(ManifestUtilTest, LoadXMLNodeTestProperXMLTree) { + const char* xml = "" + "" + "Some app" + "Desc" + "enoemoS" + "" + ""; + std::map expected_vals = { + { "widget.@dir", "ltr" }, + { "widget.name.@short", + std::string("\xE2\x80\xAA") + "SA" + "\xE2\x80\xAC" }, + { "widget.name.#text", + std::string("\xE2\x80\xAA") + "Some app" + "\xE2\x80\xAC" }, + { "widget.description.#text", + std::string("\xE2\x80\xAA") + "Desc" + "\xE2\x80\xAC" }, + { "widget.author.@dir", "rtl" }, + { "widget.author.#text", + std::string("\xE2\x80\xAB") + "enoemoS" + "\xE2\x80\xAC" } + }; + + xmlDoc* doc = xmlParseMemory(xml, strlen(xml)); + ASSERT_TRUE(doc); + xmlNode* root = doc->children; + std::unique_ptr val = + common_installer::widget_manifest_parser::LoadXMLNode(root); + + std::string test_str; + for (const auto& p : expected_vals) { + ASSERT_TRUE(val->GetString(p.first, &test_str)); + EXPECT_STREQ(p.second.c_str(), test_str.c_str()); + } +} + +} // namespace widget_manifest_parser } // namespace common_installer diff --git a/src/widget-manifest-parser/manifest_util.cc b/src/widget-manifest-parser/manifest_util.cc index b2b5eb7..15b618e 100644 --- a/src/widget-manifest-parser/manifest_util.cc +++ b/src/widget-manifest-parser/manifest_util.cc @@ -6,7 +6,6 @@ #include "widget-manifest-parser/manifest_util.h" #include -#include #include #include @@ -57,6 +56,11 @@ const char* kSingletonElements[] = { "content" }; +} // namespace + +namespace common_installer { +namespace widget_manifest_parser { + std::string GetNodeDir(xmlNode* node, const std::string& inherit_dir) { assert(node); std::string dir(inherit_dir); @@ -82,8 +86,6 @@ std::string GetNodeText(xmlNode* root, const std::string& inherit_dir) { std::string current_dir(GetNodeDir(root, inherit_dir)); std::string text; for (xmlNode* node = root->children; node; node = node->next) { -// TODO(jizydorczyk): -// i18n support is needed if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE) { text = text + common_installer::utils::StripWrappingBidiControlCharactersUTF8( @@ -97,7 +99,7 @@ std::string GetNodeText(xmlNode* root, const std::string& inherit_dir) { // According to widget specification, this two prop need to support dir. // see detail on http://www.w3.org/TR/widgets/#the-dir-attribute -inline bool IsPropSupportDir(xmlNode* root, xmlAttr* prop) { +bool IsPropSupportDir(xmlNode* root, xmlAttr* prop) { if (xmlStrEqual(root->name, kWidgetNodeKey) && xmlStrEqual(prop->name, kVersionAttributeKey)) return true; @@ -110,7 +112,7 @@ inline bool IsPropSupportDir(xmlNode* root, xmlAttr* prop) { // Only this four items need to support span and ignore other element. // Besides xmlNodeListGetString can not support dir prop of span. // See http://www.w3.org/TR/widgets/#the-span-element-and-its-attributes -inline bool IsElementSupportSpanAndDir(xmlNode* root) { +bool IsElementSupportSpanAndDir(xmlNode* root) { if (xmlStrEqual(root->name, kNameNodeKey) || xmlStrEqual(root->name, kDescriptionNodeKey) || xmlStrEqual(root->name, kAuthorNodeKey) @@ -129,7 +131,7 @@ bool IsSingletonElement(const std::string& name) { // According to spec 'name' and 'author' should be result of applying the rule // for getting text content with normalized white space to this element. // http://www.w3.org/TR/widgets/#rule-for-getting-text-content-with-normalized-white-space-0 -inline bool IsTrimRequiredForElement(xmlNode* root) { +bool IsTrimRequiredForElement(xmlNode* root) { if (xmlStrEqual(root->name, kNameNodeKey) || xmlStrEqual(root->name, kAuthorNodeKey)) { return true; @@ -140,7 +142,7 @@ inline bool IsTrimRequiredForElement(xmlNode* root) { // According to spec some attributes requaire applying the rule for getting // a single attribute value. // http://www.w3.org/TR/widgets/#rule-for-getting-a-single-attribute-value-0 -inline bool IsTrimRequiredForProp(xmlNode* root, xmlAttr* prop) { +bool IsTrimRequiredForProp(xmlNode* root, xmlAttr* prop) { if (xmlStrEqual(root->name, kWidgetNodeKey) && (xmlStrEqual(prop->name, kIdAttributeKey) || xmlStrEqual(prop->name, kVersionAttributeKey) || @@ -167,13 +169,6 @@ inline bool IsTrimRequiredForProp(xmlNode* root, xmlAttr* prop) { return false; } -} // namespace - -namespace common_installer { -namespace widget_manifest_parser { - -namespace { - // Load XML node into Dictionary structure. // The keys for the XML node to Dictionary mapping are described below: // XML Dictionary @@ -218,7 +213,7 @@ namespace { // std::map> std::unique_ptr LoadXMLNode( - xmlNode* root, const std::string& inherit_dir = "") { + xmlNode* root, const std::string& inherit_dir) { std::unique_ptr value(new utils::DictionaryValue()); if (root->type != XML_ELEMENT_NODE) return nullptr; @@ -313,8 +308,6 @@ std::unique_ptr LoadXMLNode( return value; } -} // namespace - std::unique_ptr LoadManifest(const std::string& manifest_path, std::string* error) { xmlDoc * doc = nullptr; diff --git a/src/widget-manifest-parser/manifest_util.h b/src/widget-manifest-parser/manifest_util.h index 754fa42..7b4b764 100644 --- a/src/widget-manifest-parser/manifest_util.h +++ b/src/widget-manifest-parser/manifest_util.h @@ -7,6 +7,7 @@ #define WIDGET_MANIFEST_PARSER_MANIFEST_UTIL_H_ #include +#include #include #include @@ -22,6 +23,15 @@ namespace widget_manifest_parser { // on failure, with a description of the error in |error|. std::unique_ptr LoadManifest( const std::string& file_path, std::string* error); +std::string GetNodeDir(xmlNode* node, const std::string& inherit_dir); +std::string GetNodeText(xmlNode* root, const std::string& inherit_dir); +bool IsPropSupportDir(xmlNode* root, xmlAttr* prop); +bool IsElementSupportSpanAndDir(xmlNode* root); +bool IsSingletonElement(const std::string& name); +bool IsTrimRequiredForElement(xmlNode* root); +bool IsTrimRequiredForProp(xmlNode* root, xmlAttr* prop); +std::unique_ptr LoadXMLNode( + xmlNode* root, const std::string& inherit_dir = ""); } // namespace widget_manifest_parser } // namespace common_installer -- 2.7.4