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 |
(...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
790 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\"," | 790 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\"," |
791 "\"sizes\": \"x 40xx 1x2x3 x42 42xx42\" } ] }"); | 791 "\"sizes\": \"x 40xx 1x2x3 x42 42xx42\" } ] }"); |
792 gfx::Size any = gfx::Size(0, 0); | 792 gfx::Size any = gfx::Size(0, 0); |
793 EXPECT_EQ(manifest.icons[0].sizes.size(), 0u); | 793 EXPECT_EQ(manifest.icons[0].sizes.size(), 0u); |
794 EXPECT_EQ(1u, GetErrorCount()); | 794 EXPECT_EQ(1u, GetErrorCount()); |
795 EXPECT_EQ("Manifest parsing error: found icon with no valid size.", | 795 EXPECT_EQ("Manifest parsing error: found icon with no valid size.", |
796 errors()[0]); | 796 errors()[0]); |
797 } | 797 } |
798 } | 798 } |
799 | 799 |
| 800 TEST_F(ManifestParserTest, RelatedApplicationsParseRules) { |
| 801 // If no application, empty list. |
| 802 { |
| 803 Manifest manifest = ParseManifest( |
| 804 "{ \"related_applications\": []}"); |
| 805 EXPECT_EQ(manifest.related_applications.size(), 0u); |
| 806 EXPECT_TRUE(manifest.IsEmpty()); |
| 807 EXPECT_EQ(0u, GetErrorCount()); |
| 808 } |
| 809 |
| 810 // If empty application, empty list. |
| 811 { |
| 812 Manifest manifest = ParseManifest( |
| 813 "{ \"related_applications\": [{}]}"); |
| 814 EXPECT_EQ(manifest.related_applications.size(), 0u); |
| 815 EXPECT_TRUE(manifest.IsEmpty()); |
| 816 EXPECT_EQ(1u, GetErrorCount()); |
| 817 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, " |
| 818 "related application ignored.", |
| 819 errors()[0]); |
| 820 } |
| 821 |
| 822 // If invalid platform, application is ignored. |
| 823 { |
| 824 Manifest manifest = ParseManifest( |
| 825 "{ \"related_applications\": [{\"platform\": 123}]}"); |
| 826 EXPECT_EQ(manifest.related_applications.size(), 0u); |
| 827 EXPECT_TRUE(manifest.IsEmpty()); |
| 828 EXPECT_EQ(2u, GetErrorCount()); |
| 829 EXPECT_EQ( |
| 830 "Manifest parsing error: property 'platform' ignored, type string " |
| 831 "expected.", |
| 832 errors()[0]); |
| 833 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, " |
| 834 "related application ignored.", |
| 835 errors()[1]); |
| 836 } |
| 837 |
| 838 // If missing platform, application is ignored. |
| 839 { |
| 840 Manifest manifest = ParseManifest( |
| 841 "{ \"related_applications\": [{\"id\": \"foo\"}]}"); |
| 842 EXPECT_EQ(manifest.related_applications.size(), 0u); |
| 843 EXPECT_TRUE(manifest.IsEmpty()); |
| 844 EXPECT_EQ(1u, GetErrorCount()); |
| 845 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, " |
| 846 "related application ignored.", |
| 847 errors()[0]); |
| 848 } |
| 849 |
| 850 // If missing id and url, application is ignored. |
| 851 { |
| 852 Manifest manifest = ParseManifest( |
| 853 "{ \"related_applications\": [{\"platform\": \"play\"}]}"); |
| 854 EXPECT_EQ(manifest.related_applications.size(), 0u); |
| 855 EXPECT_TRUE(manifest.IsEmpty()); |
| 856 EXPECT_EQ(1u, GetErrorCount()); |
| 857 EXPECT_EQ("Manifest parsing error: one of 'url' or 'id' is required, " |
| 858 "related application ignored.", |
| 859 errors()[0]); |
| 860 } |
| 861 |
| 862 // Valid application, with url. |
| 863 { |
| 864 Manifest manifest = ParseManifest( |
| 865 "{ \"related_applications\": [" |
| 866 "{\"platform\": \"play\", \"url\": \"http://www.foo.com\"}]}"); |
| 867 EXPECT_EQ(manifest.related_applications.size(), 1u); |
| 868 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), |
| 869 "play")); |
| 870 EXPECT_EQ(manifest.related_applications[0].url.spec(), |
| 871 "http://www.foo.com/"); |
| 872 EXPECT_FALSE(manifest.IsEmpty()); |
| 873 EXPECT_EQ(0u, GetErrorCount()); |
| 874 } |
| 875 |
| 876 // Valid application, with id. |
| 877 { |
| 878 Manifest manifest = ParseManifest( |
| 879 "{ \"related_applications\": [" |
| 880 "{\"platform\": \"itunes\", \"id\": \"foo\"}]}"); |
| 881 EXPECT_EQ(manifest.related_applications.size(), 1u); |
| 882 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), |
| 883 "itunes")); |
| 884 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].id.string(), |
| 885 "foo")); |
| 886 EXPECT_FALSE(manifest.IsEmpty()); |
| 887 EXPECT_EQ(0u, GetErrorCount()); |
| 888 } |
| 889 |
| 890 // All valid applications are in list. |
| 891 { |
| 892 Manifest manifest = ParseManifest( |
| 893 "{ \"related_applications\": [" |
| 894 "{\"platform\": \"play\", \"id\": \"foo\"}," |
| 895 "{\"platform\": \"itunes\", \"id\": \"bar\"}]}"); |
| 896 EXPECT_EQ(manifest.related_applications.size(), 2u); |
| 897 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), |
| 898 "play")); |
| 899 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].id.string(), |
| 900 "foo")); |
| 901 EXPECT_TRUE(EqualsASCII(manifest.related_applications[1].platform.string(), |
| 902 "itunes")); |
| 903 EXPECT_TRUE(EqualsASCII(manifest.related_applications[1].id.string(), |
| 904 "bar")); |
| 905 EXPECT_FALSE(manifest.IsEmpty()); |
| 906 EXPECT_EQ(0u, GetErrorCount()); |
| 907 } |
| 908 |
| 909 // Two invalid applications and one valid. Only the valid application should |
| 910 // be in the list. |
| 911 { |
| 912 Manifest manifest = ParseManifest( |
| 913 "{ \"related_applications\": [" |
| 914 "{\"platform\": \"itunes\"}," |
| 915 "{\"platform\": \"play\", \"id\": \"foo\"}," |
| 916 "{}]}"); |
| 917 EXPECT_EQ(manifest.related_applications.size(), 1u); |
| 918 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].platform.string(), |
| 919 "play")); |
| 920 EXPECT_TRUE(EqualsASCII(manifest.related_applications[0].id.string(), |
| 921 "foo")); |
| 922 EXPECT_FALSE(manifest.IsEmpty()); |
| 923 EXPECT_EQ(2u, GetErrorCount()); |
| 924 EXPECT_EQ("Manifest parsing error: one of 'url' or 'id' is required, " |
| 925 "related application ignored.", |
| 926 errors()[0]); |
| 927 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, " |
| 928 "related application ignored.", |
| 929 errors()[1]); |
| 930 } |
| 931 } |
| 932 |
| 933 TEST_F(ManifestParserTest, ParsePreferRelatedApplicationsParseRules) { |
| 934 // Smoke test. |
| 935 { |
| 936 Manifest manifest = |
| 937 ParseManifest("{ \"prefer_related_applications\": true }"); |
| 938 EXPECT_TRUE(manifest.prefer_related_applications); |
| 939 EXPECT_EQ(0u, GetErrorCount()); |
| 940 } |
| 941 |
| 942 // Don't parse if the property isn't a boolean. |
| 943 { |
| 944 Manifest manifest = |
| 945 ParseManifest("{ \"prefer_related_applications\": {} }"); |
| 946 EXPECT_FALSE(manifest.prefer_related_applications); |
| 947 EXPECT_EQ(1u, GetErrorCount()); |
| 948 EXPECT_EQ( |
| 949 "Manifest parsing error: property 'prefer_related_applications' " |
| 950 "ignored, type boolean expected.", |
| 951 errors()[0]); |
| 952 } |
| 953 { |
| 954 Manifest manifest = ParseManifest( |
| 955 "{ \"prefer_related_applications\": \"true\" }"); |
| 956 EXPECT_FALSE(manifest.prefer_related_applications); |
| 957 EXPECT_EQ(1u, GetErrorCount()); |
| 958 EXPECT_EQ( |
| 959 "Manifest parsing error: property 'prefer_related_applications' " |
| 960 "ignored, type boolean expected.", |
| 961 errors()[0]); |
| 962 } |
| 963 { |
| 964 Manifest manifest = ParseManifest("{ \"prefer_related_applications\": 1 }"); |
| 965 EXPECT_FALSE(manifest.prefer_related_applications); |
| 966 EXPECT_EQ(1u, GetErrorCount()); |
| 967 EXPECT_EQ( |
| 968 "Manifest parsing error: property 'prefer_related_applications' " |
| 969 "ignored, type boolean expected.", |
| 970 errors()[0]); |
| 971 } |
| 972 |
| 973 // "False" should set the boolean false without throwing errors. |
| 974 { |
| 975 Manifest manifest = |
| 976 ParseManifest("{ \"prefer_related_applications\": false }"); |
| 977 EXPECT_FALSE(manifest.prefer_related_applications); |
| 978 EXPECT_EQ(0u, GetErrorCount()); |
| 979 } |
| 980 } |
| 981 |
800 TEST_F(ManifestParserTest, GCMSenderIDParseRules) { | 982 TEST_F(ManifestParserTest, GCMSenderIDParseRules) { |
801 // Smoke test. | 983 // Smoke test. |
802 { | 984 { |
803 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); | 985 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }"); |
804 EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); | 986 EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo")); |
805 EXPECT_EQ(0u, GetErrorCount()); | 987 EXPECT_EQ(0u, GetErrorCount()); |
806 } | 988 } |
807 | 989 |
808 // Trim whitespaces. | 990 // Trim whitespaces. |
809 { | 991 { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 | 1053 |
872 // "False" should set the boolean false without throwing errors. | 1054 // "False" should set the boolean false without throwing errors. |
873 { | 1055 { |
874 Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": false }"); | 1056 Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": false }"); |
875 EXPECT_FALSE(manifest.gcm_user_visible_only); | 1057 EXPECT_FALSE(manifest.gcm_user_visible_only); |
876 EXPECT_EQ(0u, GetErrorCount()); | 1058 EXPECT_EQ(0u, GetErrorCount()); |
877 } | 1059 } |
878 } | 1060 } |
879 | 1061 |
880 } // namespace content | 1062 } // namespace content |
OLD | NEW |