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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/optional.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "content/public/common/manifest.h" | 14 #include "content/public/common/manifest.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 uint32_t ExtractColor(int64_t color) { | 21 uint32_t ExtractColor(int64_t color) { |
21 return reinterpret_cast<uint32_t&>(color); | 22 return reinterpret_cast<uint32_t&>(color); |
(...skipping 953 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 { | 976 { |
976 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\"," | 977 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\"," |
977 "\"purpose\": \"notification\" } ] }"); | 978 "\"purpose\": \"notification\" } ] }"); |
978 ASSERT_EQ(manifest.icons[0].purpose.size(), 1u); | 979 ASSERT_EQ(manifest.icons[0].purpose.size(), 1u); |
979 EXPECT_EQ(manifest.icons[0].purpose[0], Manifest::Icon::IconPurpose::ANY); | 980 EXPECT_EQ(manifest.icons[0].purpose[0], Manifest::Icon::IconPurpose::ANY); |
980 ASSERT_EQ(1u, GetErrorCount()); | 981 ASSERT_EQ(1u, GetErrorCount()); |
981 EXPECT_EQ(kPurposeInvalidValueError, errors()[0]); | 982 EXPECT_EQ(kPurposeInvalidValueError, errors()[0]); |
982 } | 983 } |
983 } | 984 } |
984 | 985 |
| 986 TEST_F(ManifestParserTest, ShareTargetParseRules) { |
| 987 // Contains share_target field but no keys. |
| 988 { |
| 989 Manifest manifest = ParseManifest("{ \"share_target\": {} }"); |
| 990 EXPECT_FALSE(manifest.share_target.has_value()); |
| 991 EXPECT_TRUE(manifest.IsEmpty()); |
| 992 EXPECT_EQ(0u, GetErrorCount()); |
| 993 } |
| 994 |
| 995 // Key in share_target that isn't valid. |
| 996 { |
| 997 Manifest manifest = ParseManifest( |
| 998 "{ \"share_target\": {\"incorrect_key\": \"some_value\" } }"); |
| 999 ASSERT_FALSE(manifest.share_target.has_value()); |
| 1000 EXPECT_TRUE(manifest.IsEmpty()); |
| 1001 EXPECT_EQ(0u, GetErrorCount()); |
| 1002 } |
| 1003 } |
| 1004 |
| 1005 TEST_F(ManifestParserTest, ShareTargetUrlTemplateParseRules) { |
| 1006 // Contains share_target and url_template, but url_template is empty. |
| 1007 { |
| 1008 Manifest manifest = |
| 1009 ParseManifest("{ \"share_target\": { \"url_template\": \"\" } }"); |
| 1010 ASSERT_TRUE(manifest.share_target.has_value()); |
| 1011 EXPECT_TRUE(base::EqualsASCII( |
| 1012 manifest.share_target.value().url_template.string(), "")); |
| 1013 EXPECT_FALSE(manifest.IsEmpty()); |
| 1014 EXPECT_EQ(0u, GetErrorCount()); |
| 1015 } |
| 1016 |
| 1017 // Don't parse if property isn't a string. |
| 1018 { |
| 1019 Manifest manifest = |
| 1020 ParseManifest("{ \"share_target\": { \"url_template\": {} } }"); |
| 1021 EXPECT_FALSE(manifest.share_target.has_value()); |
| 1022 EXPECT_TRUE(manifest.IsEmpty()); |
| 1023 EXPECT_EQ(1u, GetErrorCount()); |
| 1024 EXPECT_EQ("property 'url_template' ignored, type string expected.", |
| 1025 errors()[0]); |
| 1026 } |
| 1027 |
| 1028 // Don't parse if property isn't a string. |
| 1029 { |
| 1030 Manifest manifest = |
| 1031 ParseManifest("{ \"share_target\": { \"url_template\": 42 } }"); |
| 1032 EXPECT_FALSE(manifest.share_target.has_value()); |
| 1033 EXPECT_TRUE(manifest.IsEmpty()); |
| 1034 EXPECT_EQ(1u, GetErrorCount()); |
| 1035 EXPECT_EQ("property 'url_template' ignored, type string expected.", |
| 1036 errors()[0]); |
| 1037 } |
| 1038 |
| 1039 // Smoke test: Contains share_target and url_template, and url_template is |
| 1040 // valid template. |
| 1041 { |
| 1042 Manifest manifest = ParseManifest( |
| 1043 "{ \"share_target\": {\"url_template\": \"share/?title={title}\" } }"); |
| 1044 ASSERT_TRUE(manifest.share_target.has_value()); |
| 1045 EXPECT_TRUE( |
| 1046 base::EqualsASCII(manifest.share_target.value().url_template.string(), |
| 1047 "share/?title={title}")); |
| 1048 EXPECT_FALSE(manifest.IsEmpty()); |
| 1049 EXPECT_EQ(0u, GetErrorCount()); |
| 1050 } |
| 1051 |
| 1052 // Smoke test: Contains share_target and url_template, and url_template is |
| 1053 // invalid template. |
| 1054 { |
| 1055 Manifest manifest = ParseManifest( |
| 1056 "{ \"share_target\": {\"url_template\": \"share/?title={title\" } }"); |
| 1057 ASSERT_TRUE(manifest.share_target.has_value()); |
| 1058 EXPECT_TRUE( |
| 1059 base::EqualsASCII(manifest.share_target.value().url_template.string(), |
| 1060 "share/?title={title")); |
| 1061 EXPECT_FALSE(manifest.IsEmpty()); |
| 1062 EXPECT_EQ(0u, GetErrorCount()); |
| 1063 } |
| 1064 } |
| 1065 |
985 TEST_F(ManifestParserTest, RelatedApplicationsParseRules) { | 1066 TEST_F(ManifestParserTest, RelatedApplicationsParseRules) { |
986 // If no application, empty list. | 1067 // If no application, empty list. |
987 { | 1068 { |
988 Manifest manifest = ParseManifest( | 1069 Manifest manifest = ParseManifest( |
989 "{ \"related_applications\": []}"); | 1070 "{ \"related_applications\": []}"); |
990 EXPECT_EQ(manifest.related_applications.size(), 0u); | 1071 EXPECT_EQ(manifest.related_applications.size(), 0u); |
991 EXPECT_TRUE(manifest.IsEmpty()); | 1072 EXPECT_TRUE(manifest.IsEmpty()); |
992 EXPECT_EQ(0u, GetErrorCount()); | 1073 EXPECT_EQ(0u, GetErrorCount()); |
993 } | 1074 } |
994 | 1075 |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1518 { | 1599 { |
1519 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); | 1600 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); |
1520 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); | 1601 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
1521 EXPECT_EQ(1u, GetErrorCount()); | 1602 EXPECT_EQ(1u, GetErrorCount()); |
1522 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.", | 1603 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.", |
1523 errors()[0]); | 1604 errors()[0]); |
1524 } | 1605 } |
1525 } | 1606 } |
1526 | 1607 |
1527 } // namespace content | 1608 } // namespace content |
OLD | NEW |