| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/manifest/manifest_parser.h" | 5 #include "content/renderer/manifest/manifest_parser.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "content/public/common/manifest.h" | 8 #include "content/public/common/manifest.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace content { | 11 namespace content { |
| 12 | 12 |
| 13 TEST(ManifestParserTest, EmptyStringNull) { | 13 class ManifestParserTest : public testing::Test { |
| 14 Manifest manifest = ManifestParser::Parse(""); | 14 protected: |
| 15 ManifestParserTest() {} |
| 16 virtual ~ManifestParserTest() {} |
| 17 |
| 18 Manifest ParseManifest(const base::StringPiece& json, |
| 19 const GURL& document_url = default_document_url, |
| 20 const GURL& manifest_url = default_manifest_url) { |
| 21 return ManifestParser::Parse(json, document_url, manifest_url); |
| 22 } |
| 23 |
| 24 static const GURL default_document_url; |
| 25 static const GURL default_manifest_url; |
| 26 |
| 27 private: |
| 28 DISALLOW_COPY_AND_ASSIGN(ManifestParserTest); |
| 29 }; |
| 30 |
| 31 const GURL ManifestParserTest::default_document_url( |
| 32 "http://foo.com/index.html"); |
| 33 const GURL ManifestParserTest::default_manifest_url( |
| 34 "http://foo.com/manifest.json"); |
| 35 |
| 36 TEST_F(ManifestParserTest, EmptyStringNull) { |
| 37 Manifest manifest = ParseManifest(""); |
| 15 | 38 |
| 16 // A parsing error is equivalent to an empty manifest. | 39 // A parsing error is equivalent to an empty manifest. |
| 17 ASSERT_TRUE(manifest.IsEmpty()); | 40 ASSERT_TRUE(manifest.IsEmpty()); |
| 18 ASSERT_TRUE(manifest.name.is_null()); | 41 ASSERT_TRUE(manifest.name.is_null()); |
| 19 ASSERT_TRUE(manifest.short_name.is_null()); | 42 ASSERT_TRUE(manifest.short_name.is_null()); |
| 43 ASSERT_TRUE(manifest.start_url.is_empty()); |
| 20 } | 44 } |
| 21 | 45 |
| 22 TEST(ManifestParserTest, ValidNoContentParses) { | 46 TEST_F(ManifestParserTest, ValidNoContentParses) { |
| 23 Manifest manifest = ManifestParser::Parse("{}"); | 47 Manifest manifest = ParseManifest("{}"); |
| 24 | 48 |
| 25 // Check that all the fields are null in that case. | 49 // Check that all the fields are null in that case. |
| 26 ASSERT_TRUE(manifest.IsEmpty()); | 50 ASSERT_TRUE(manifest.IsEmpty()); |
| 27 ASSERT_TRUE(manifest.name.is_null()); | 51 ASSERT_TRUE(manifest.name.is_null()); |
| 28 ASSERT_TRUE(manifest.short_name.is_null()); | 52 ASSERT_TRUE(manifest.short_name.is_null()); |
| 53 ASSERT_TRUE(manifest.start_url.is_empty()); |
| 29 } | 54 } |
| 30 | 55 |
| 31 TEST(ManifestParserTest, NameParseRules) { | 56 TEST_F(ManifestParserTest, NameParseRules) { |
| 32 // Smoke test. | 57 // Smoke test. |
| 33 { | 58 { |
| 34 Manifest manifest = ManifestParser::Parse("{ \"name\": \"foo\" }"); | 59 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }"); |
| 35 ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo")); | 60 ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo")); |
| 36 } | 61 } |
| 37 | 62 |
| 38 // Trim whitespaces. | 63 // Trim whitespaces. |
| 39 { | 64 { |
| 40 Manifest manifest = ManifestParser::Parse("{ \"name\": \" foo \" }"); | 65 Manifest manifest = ParseManifest("{ \"name\": \" foo \" }"); |
| 41 ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo")); | 66 ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo")); |
| 42 } | 67 } |
| 43 | 68 |
| 44 // Don't parse if name isn't a string. | 69 // Don't parse if name isn't a string. |
| 45 { | 70 { |
| 46 Manifest manifest = ManifestParser::Parse("{ \"name\": {} }"); | 71 Manifest manifest = ParseManifest("{ \"name\": {} }"); |
| 47 ASSERT_TRUE(manifest.name.is_null()); | 72 ASSERT_TRUE(manifest.name.is_null()); |
| 48 } | 73 } |
| 49 | 74 |
| 50 // Don't parse if name isn't a string. | 75 // Don't parse if name isn't a string. |
| 51 { | 76 { |
| 52 Manifest manifest = ManifestParser::Parse("{ \"name\": 42 }"); | 77 Manifest manifest = ParseManifest("{ \"name\": 42 }"); |
| 53 ASSERT_TRUE(manifest.name.is_null()); | 78 ASSERT_TRUE(manifest.name.is_null()); |
| 54 } | 79 } |
| 55 } | 80 } |
| 56 | 81 |
| 57 TEST(ManifestParserTest, ShortNameParseRules) { | 82 TEST_F(ManifestParserTest, ShortNameParseRules) { |
| 58 // Smoke test. | 83 // Smoke test. |
| 59 { | 84 { |
| 60 Manifest manifest = ManifestParser::Parse("{ \"short_name\": \"foo\" }"); | 85 Manifest manifest = ParseManifest("{ \"short_name\": \"foo\" }"); |
| 61 ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo")); | 86 ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo")); |
| 62 } | 87 } |
| 63 | 88 |
| 64 // Trim whitespaces. | 89 // Trim whitespaces. |
| 65 { | 90 { |
| 66 Manifest manifest = | 91 Manifest manifest = ParseManifest("{ \"short_name\": \" foo \" }"); |
| 67 ManifestParser::Parse("{ \"short_name\": \" foo \" }"); | |
| 68 ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo")); | 92 ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo")); |
| 69 } | 93 } |
| 70 | 94 |
| 71 // Don't parse if name isn't a string. | 95 // Don't parse if name isn't a string. |
| 72 { | 96 { |
| 73 Manifest manifest = ManifestParser::Parse("{ \"short_name\": {} }"); | 97 Manifest manifest = ParseManifest("{ \"short_name\": {} }"); |
| 74 ASSERT_TRUE(manifest.short_name.is_null()); | 98 ASSERT_TRUE(manifest.short_name.is_null()); |
| 75 } | 99 } |
| 76 | 100 |
| 77 // Don't parse if name isn't a string. | 101 // Don't parse if name isn't a string. |
| 78 { | 102 { |
| 79 Manifest manifest = ManifestParser::Parse("{ \"short_name\": 42 }"); | 103 Manifest manifest = ParseManifest("{ \"short_name\": 42 }"); |
| 80 ASSERT_TRUE(manifest.short_name.is_null()); | 104 ASSERT_TRUE(manifest.short_name.is_null()); |
| 81 } | 105 } |
| 82 } | 106 } |
| 83 | 107 |
| 108 TEST_F(ManifestParserTest, StartURLParseRules) { |
| 109 // Smoke test. |
| 110 { |
| 111 Manifest manifest = ParseManifest("{ \"start_url\": \"land.html\" }"); |
| 112 ASSERT_EQ(manifest.start_url.spec(), |
| 113 default_document_url.Resolve("land.html").spec()); |
| 114 } |
| 115 |
| 116 // Whitespaces. |
| 117 { |
| 118 Manifest manifest = ParseManifest("{ \"start_url\": \" land.html \" }"); |
| 119 ASSERT_EQ(manifest.start_url.spec(), |
| 120 default_document_url.Resolve("land.html").spec()); |
| 121 } |
| 122 |
| 123 // Don't parse if property isn't a string. |
| 124 { |
| 125 Manifest manifest = ParseManifest("{ \"start_url\": {} }"); |
| 126 ASSERT_TRUE(manifest.start_url.is_empty()); |
| 127 } |
| 128 |
| 129 // Don't parse if property isn't a string. |
| 130 { |
| 131 Manifest manifest = ParseManifest("{ \"start_url\": 42 }"); |
| 132 ASSERT_TRUE(manifest.start_url.is_empty()); |
| 133 } |
| 134 |
| 135 // Absolute start_url, same origin with document. |
| 136 { |
| 137 Manifest manifest = |
| 138 ParseManifest("{ \"start_url\": \"http://foo.com/land.html\" }", |
| 139 GURL("http://foo.com/manifest.json"), |
| 140 GURL("http://foo.com/index.html")); |
| 141 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/land.html"); |
| 142 } |
| 143 |
| 144 // Absolute start_url, cross origin with document. |
| 145 { |
| 146 Manifest manifest = |
| 147 ParseManifest("{ \"start_url\": \"http://bar.com/land.html\" }", |
| 148 GURL("http://foo.com/manifest.json"), |
| 149 GURL("http://foo.com/index.html")); |
| 150 ASSERT_TRUE(manifest.start_url.is_empty()); |
| 151 } |
| 152 |
| 153 // Resolving has to happen based on the manifest_url. |
| 154 { |
| 155 Manifest manifest = |
| 156 ParseManifest("{ \"start_url\": \"land.html\" }", |
| 157 GURL("http://foo.com/landing/manifest.json"), |
| 158 GURL("http://foo.com/index.html")); |
| 159 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/landing/land.html"); |
| 160 } |
| 161 } |
| 162 |
| 84 } // namespace content | 163 } // namespace content |
| OLD | NEW |