OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/renderer/manifest/manifest_parser.h" |
| 6 |
| 7 #include "chrome/renderer/chrome_content_renderer_client.h" |
| 8 #include "content/public/common/content_client.h" |
| 9 #include "content/public/common/manifest.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using content::Manifest; |
| 13 using content::ManifestParser; |
| 14 |
| 15 class ChromeManifestParserTest : public testing::Test { |
| 16 protected: |
| 17 ChromeManifestParserTest() { |
| 18 SetContentClient(&test_content_client_); |
| 19 SetRendererClientForTesting(&test_content_renderer_client_); |
| 20 } |
| 21 |
| 22 ~ChromeManifestParserTest() override {} |
| 23 |
| 24 Manifest ParseManifestWithURLs(const base::StringPiece& data, |
| 25 const GURL& document_url, |
| 26 const GURL& manifest_url) { |
| 27 scoped_ptr<ManifestParser> parser = ManifestParser::Get(); |
| 28 parser->Parse(data, document_url, manifest_url); |
| 29 errors_ = parser->errors(); |
| 30 return parser->manifest(); |
| 31 } |
| 32 |
| 33 Manifest ParseManifest(const base::StringPiece& data) { |
| 34 return ParseManifestWithURLs( |
| 35 data, default_document_url, default_manifest_url); |
| 36 } |
| 37 |
| 38 const std::vector<std::string>& errors() const { |
| 39 return errors_; |
| 40 } |
| 41 |
| 42 unsigned int GetErrorCount() const { |
| 43 return errors_.size(); |
| 44 } |
| 45 |
| 46 static const GURL default_document_url; |
| 47 static const GURL default_manifest_url; |
| 48 |
| 49 private: |
| 50 std::vector<std::string> errors_; |
| 51 |
| 52 content::ContentClient test_content_client_; |
| 53 ChromeContentRendererClient test_content_renderer_client_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(ChromeManifestParserTest); |
| 56 }; |
| 57 |
| 58 const GURL ChromeManifestParserTest::default_document_url( |
| 59 "http://foo.com/index.html"); |
| 60 const GURL ChromeManifestParserTest::default_manifest_url( |
| 61 "http://foo.com/manifest.json"); |
| 62 |
| 63 // This is equivalent to ManifestParserTest.EmptyStringNull but tests properties |
| 64 // set by the ChromeManifestParser. |
| 65 TEST_F(ChromeManifestParserTest, EmptyStringNull) { |
| 66 Manifest manifest = ParseManifest(""); |
| 67 |
| 68 // This Manifest is not a valid JSON object, it's a parsing error. |
| 69 EXPECT_EQ(1u, GetErrorCount()); |
| 70 EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.", |
| 71 errors()[0]); |
| 72 |
| 73 // A parsing error is equivalent to an empty manifest. |
| 74 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
| 75 EXPECT_FALSE(manifest.gcm_user_visible_only); |
| 76 } |
| 77 |
| 78 // This is equivalent to ManifestParserTest.ValidNoContentParses but tests |
| 79 // properties set by the ChromeManifestParser. |
| 80 TEST_F(ChromeManifestParserTest, ValidNoContentParses) { |
| 81 Manifest manifest = ParseManifest("{}"); |
| 82 |
| 83 // Empty Manifest is not a parsing error. |
| 84 EXPECT_EQ(0u, GetErrorCount()); |
| 85 |
| 86 // Check that all the fields are null in that case. |
| 87 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
| 88 EXPECT_FALSE(manifest.gcm_user_visible_only); |
| 89 } |
| 90 |
| 91 TEST_F(ChromeManifestParserTest, GCMSenderIDParseRules) { |
| 92 // Smoke test. |
| 93 { |
| 94 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); |
| 95 EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
| 96 EXPECT_EQ(0u, GetErrorCount()); |
| 97 } |
| 98 |
| 99 // Trim whitespaces. |
| 100 { |
| 101 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \" foo \" }"); |
| 102 EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
| 103 EXPECT_EQ(0u, GetErrorCount()); |
| 104 } |
| 105 |
| 106 // Don't parse if the property isn't a string. |
| 107 { |
| 108 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": {} }"); |
| 109 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
| 110 EXPECT_EQ(1u, GetErrorCount()); |
| 111 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
| 112 " type string expected.", |
| 113 errors()[0]); |
| 114 } |
| 115 { |
| 116 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); |
| 117 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
| 118 EXPECT_EQ(1u, GetErrorCount()); |
| 119 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
| 120 " type string expected.", |
| 121 errors()[0]); |
| 122 } |
| 123 } |
| 124 |
| 125 TEST_F(ChromeManifestParserTest, GCMUserVisibleOnlyParseRules) { |
| 126 // Smoke test. |
| 127 { |
| 128 Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": true }"); |
| 129 EXPECT_TRUE(manifest.gcm_user_visible_only); |
| 130 EXPECT_EQ(0u, GetErrorCount()); |
| 131 } |
| 132 |
| 133 // Don't parse if the property isn't a boolean. |
| 134 { |
| 135 Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": {} }"); |
| 136 EXPECT_FALSE(manifest.gcm_user_visible_only); |
| 137 EXPECT_EQ(1u, GetErrorCount()); |
| 138 EXPECT_EQ( |
| 139 "Manifest parsing error: property 'gcm_user_visible_only' ignored," |
| 140 " type boolean expected.", |
| 141 errors()[0]); |
| 142 } |
| 143 { |
| 144 Manifest manifest = ParseManifest( |
| 145 "{ \"gcm_user_visible_only\": \"true\" }"); |
| 146 EXPECT_FALSE(manifest.gcm_user_visible_only); |
| 147 EXPECT_EQ(1u, GetErrorCount()); |
| 148 EXPECT_EQ( |
| 149 "Manifest parsing error: property 'gcm_user_visible_only' ignored," |
| 150 " type boolean expected.", |
| 151 errors()[0]); |
| 152 } |
| 153 { |
| 154 Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": 1 }"); |
| 155 EXPECT_FALSE(manifest.gcm_user_visible_only); |
| 156 EXPECT_EQ(1u, GetErrorCount()); |
| 157 EXPECT_EQ( |
| 158 "Manifest parsing error: property 'gcm_user_visible_only' ignored," |
| 159 " type boolean expected.", |
| 160 errors()[0]); |
| 161 } |
| 162 |
| 163 // "False" should set the boolean false without throwing errors. |
| 164 { |
| 165 Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": false }"); |
| 166 EXPECT_FALSE(manifest.gcm_user_visible_only); |
| 167 EXPECT_EQ(0u, GetErrorCount()); |
| 168 } |
| 169 } |
OLD | NEW |