Index: chrome/renderer/manifest/manifest_parser_unittest.cc |
diff --git a/chrome/renderer/manifest/manifest_parser_unittest.cc b/chrome/renderer/manifest/manifest_parser_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4e556213517e2eacd1a022c74fb9fa7a07c31f6d |
--- /dev/null |
+++ b/chrome/renderer/manifest/manifest_parser_unittest.cc |
@@ -0,0 +1,169 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/renderer/manifest/manifest_parser.h" |
+ |
+#include "chrome/renderer/chrome_content_renderer_client.h" |
+#include "content/public/common/content_client.h" |
+#include "content/public/common/manifest.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using content::Manifest; |
+using content::ManifestParser; |
+ |
+class ChromeManifestParserTest : public testing::Test { |
+ protected: |
+ ChromeManifestParserTest() { |
+ SetContentClient(&test_content_client_); |
+ SetRendererClientForTesting(&test_content_renderer_client_); |
+ } |
+ |
+ ~ChromeManifestParserTest() override {} |
+ |
+ Manifest ParseManifestWithURLs(const base::StringPiece& data, |
+ const GURL& document_url, |
+ const GURL& manifest_url) { |
+ scoped_ptr<ManifestParser> parser = ManifestParser::Get(); |
+ parser->Parse(data, document_url, manifest_url); |
+ errors_ = parser->errors(); |
+ return parser->manifest(); |
+ } |
+ |
+ Manifest ParseManifest(const base::StringPiece& data) { |
+ return ParseManifestWithURLs( |
+ data, default_document_url, default_manifest_url); |
+ } |
+ |
+ const std::vector<std::string>& errors() const { |
+ return errors_; |
+ } |
+ |
+ unsigned int GetErrorCount() const { |
+ return errors_.size(); |
+ } |
+ |
+ static const GURL default_document_url; |
+ static const GURL default_manifest_url; |
+ |
+ private: |
+ std::vector<std::string> errors_; |
+ |
+ content::ContentClient test_content_client_; |
+ ChromeContentRendererClient test_content_renderer_client_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChromeManifestParserTest); |
+}; |
+ |
+const GURL ChromeManifestParserTest::default_document_url( |
+ "http://foo.com/index.html"); |
+const GURL ChromeManifestParserTest::default_manifest_url( |
+ "http://foo.com/manifest.json"); |
+ |
+// This is equivalent to ManifestParserTest.EmptyStringNull but tests properties |
+// set by the ChromeManifestParser. |
+TEST_F(ChromeManifestParserTest, EmptyStringNull) { |
+ Manifest manifest = ParseManifest(""); |
+ |
+ // This Manifest is not a valid JSON object, it's a parsing error. |
+ EXPECT_EQ(1u, GetErrorCount()); |
+ EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.", |
+ errors()[0]); |
+ |
+ // A parsing error is equivalent to an empty manifest. |
+ EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
+ EXPECT_FALSE(manifest.gcm_user_visible_only); |
+} |
+ |
+// This is equivalent to ManifestParserTest.ValidNoContentParses but tests |
+// properties set by the ChromeManifestParser. |
+TEST_F(ChromeManifestParserTest, ValidNoContentParses) { |
+ Manifest manifest = ParseManifest("{}"); |
+ |
+ // Empty Manifest is not a parsing error. |
+ EXPECT_EQ(0u, GetErrorCount()); |
+ |
+ // Check that all the fields are null in that case. |
+ EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
+ EXPECT_FALSE(manifest.gcm_user_visible_only); |
+} |
+ |
+TEST_F(ChromeManifestParserTest, GCMSenderIDParseRules) { |
+ // Smoke test. |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); |
+ EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
+ EXPECT_EQ(0u, GetErrorCount()); |
+ } |
+ |
+ // Trim whitespaces. |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \" foo \" }"); |
+ EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
+ EXPECT_EQ(0u, GetErrorCount()); |
+ } |
+ |
+ // Don't parse if the property isn't a string. |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_sender_id\": {} }"); |
+ EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
+ EXPECT_EQ(1u, GetErrorCount()); |
+ EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
+ " type string expected.", |
+ errors()[0]); |
+ } |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); |
+ EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
+ EXPECT_EQ(1u, GetErrorCount()); |
+ EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
+ " type string expected.", |
+ errors()[0]); |
+ } |
+} |
+ |
+TEST_F(ChromeManifestParserTest, GCMUserVisibleOnlyParseRules) { |
+ // Smoke test. |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": true }"); |
+ EXPECT_TRUE(manifest.gcm_user_visible_only); |
+ EXPECT_EQ(0u, GetErrorCount()); |
+ } |
+ |
+ // Don't parse if the property isn't a boolean. |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": {} }"); |
+ EXPECT_FALSE(manifest.gcm_user_visible_only); |
+ EXPECT_EQ(1u, GetErrorCount()); |
+ EXPECT_EQ( |
+ "Manifest parsing error: property 'gcm_user_visible_only' ignored," |
+ " type boolean expected.", |
+ errors()[0]); |
+ } |
+ { |
+ Manifest manifest = ParseManifest( |
+ "{ \"gcm_user_visible_only\": \"true\" }"); |
+ EXPECT_FALSE(manifest.gcm_user_visible_only); |
+ EXPECT_EQ(1u, GetErrorCount()); |
+ EXPECT_EQ( |
+ "Manifest parsing error: property 'gcm_user_visible_only' ignored," |
+ " type boolean expected.", |
+ errors()[0]); |
+ } |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": 1 }"); |
+ EXPECT_FALSE(manifest.gcm_user_visible_only); |
+ EXPECT_EQ(1u, GetErrorCount()); |
+ EXPECT_EQ( |
+ "Manifest parsing error: property 'gcm_user_visible_only' ignored," |
+ " type boolean expected.", |
+ errors()[0]); |
+ } |
+ |
+ // "False" should set the boolean false without throwing errors. |
+ { |
+ Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": false }"); |
+ EXPECT_FALSE(manifest.gcm_user_visible_only); |
+ EXPECT_EQ(0u, GetErrorCount()); |
+ } |
+} |