Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / common / manifest_handlers / shared_module_manifest_unittest.cc
blob9fad6187ff5903a976b45f64f135abd9c13b1dd2
1 // Copyright 2013 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.
5 #include "base/version.h"
6 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
7 #include "extensions/common/extension.h"
8 #include "extensions/common/manifest_handlers/shared_module_info.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
13 const char* kValidImportPath =
14 "_modules/abcdefghijklmnopabcdefghijklmnop/foo/bar.html";
15 const char* kValidImportPathID = "abcdefghijklmnopabcdefghijklmnop";
16 const char* kValidImportPathRelative = "foo/bar.html";
17 const char* kInvalidImportPath = "_modules/abc/foo.html";
18 const char* kImportId1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
19 const char* kImportId2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
20 const char* kNoImport = "cccccccccccccccccccccccccccccccc";
22 } // namespace
24 namespace extensions {
26 class SharedModuleManifestTest : public ExtensionManifestTest {
29 TEST_F(SharedModuleManifestTest, ExportsAll) {
30 Manifest manifest("shared_module_export.json");
32 scoped_refptr<Extension> extension = LoadAndExpectSuccess(manifest);
34 EXPECT_TRUE(SharedModuleInfo::IsSharedModule(extension.get()))
35 << manifest.name();
36 EXPECT_FALSE(SharedModuleInfo::ImportsModules(extension.get()))
37 << manifest.name();
38 EXPECT_TRUE(SharedModuleInfo::IsExportAllowed(extension.get(), "foo"))
39 << manifest.name();
40 EXPECT_TRUE(SharedModuleInfo::IsExportAllowed(extension.get(), "foo/bar"))
41 << manifest.name();
43 EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
44 kImportId1)) << manifest.name();
45 EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
46 kImportId2)) << manifest.name();
47 EXPECT_FALSE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
48 kNoImport)) << manifest.name();
51 TEST_F(SharedModuleManifestTest, ExportWhitelistAll) {
52 Manifest manifest("shared_module_export_no_whitelist.json");
54 scoped_refptr<Extension> extension = LoadAndExpectSuccess(manifest);
56 EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
57 kImportId1)) << manifest.name();
58 EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
59 kImportId2)) << manifest.name();
60 EXPECT_TRUE(SharedModuleInfo::IsExportAllowedByWhitelist(extension.get(),
61 kNoImport)) << manifest.name();
64 TEST_F(SharedModuleManifestTest, ExportFoo) {
65 Manifest manifest("shared_module_export_foo.json");
67 scoped_refptr<Extension> extension = LoadAndExpectSuccess(manifest);
69 EXPECT_TRUE(SharedModuleInfo::IsSharedModule(extension.get()))
70 << manifest.name();
71 EXPECT_FALSE(SharedModuleInfo::ImportsModules(extension.get()))
72 << manifest.name();
73 EXPECT_TRUE(SharedModuleInfo::IsExportAllowed(extension.get(), "foo"))
74 << manifest.name();
75 EXPECT_FALSE(SharedModuleInfo::IsExportAllowed(extension.get(), "foo/bar"))
76 << manifest.name();
79 TEST_F(SharedModuleManifestTest, ExportParseErrors) {
80 Testcase testcases[] = {
81 Testcase("shared_module_export_and_import.json",
82 "Simultaneous 'import' and 'export' are not allowed."),
83 Testcase("shared_module_export_not_dict.json",
84 "Invalid value for 'export'."),
85 Testcase("shared_module_export_resources_not_list.json",
86 "Invalid value for 'export.resources'."),
87 Testcase("shared_module_export_resource_not_string.json",
88 "Invalid value for 'export.resources[1]'."),
89 Testcase("shared_module_export_whitelist_item_not_id.json",
90 "Invalid value for 'export.whitelist[0]'."),
91 Testcase("shared_module_export_whitelist_item_not_string.json",
92 "Invalid value for 'export.whitelist[0]'."),
93 Testcase("shared_module_export_whitelist_not_list.json",
94 "Invalid value for 'export.whitelist'."),
96 RunTestcases(testcases, arraysize(testcases), EXPECT_TYPE_ERROR);
99 TEST_F(SharedModuleManifestTest, SharedModuleStaticFunctions) {
100 EXPECT_TRUE(SharedModuleInfo::IsImportedPath(kValidImportPath));
101 EXPECT_FALSE(SharedModuleInfo::IsImportedPath(kInvalidImportPath));
103 std::string id;
104 std::string relative;
105 SharedModuleInfo::ParseImportedPath(kValidImportPath, &id, &relative);
106 EXPECT_EQ(id, kValidImportPathID);
107 EXPECT_EQ(relative, kValidImportPathRelative);
110 TEST_F(SharedModuleManifestTest, Import) {
111 Manifest manifest("shared_module_import.json");
113 scoped_refptr<Extension> extension = LoadAndExpectSuccess(manifest);
115 EXPECT_FALSE(SharedModuleInfo::IsSharedModule(extension.get()))
116 << manifest.name();
117 EXPECT_TRUE(SharedModuleInfo::ImportsModules(extension.get()))
118 << manifest.name();
119 const std::vector<SharedModuleInfo::ImportInfo>& imports =
120 SharedModuleInfo::GetImports(extension.get());
121 ASSERT_EQ(2U, imports.size());
122 EXPECT_EQ(imports[0].extension_id, kImportId1);
123 EXPECT_EQ(imports[0].minimum_version, "");
124 EXPECT_EQ(imports[1].extension_id, kImportId2);
125 EXPECT_TRUE(base::Version(imports[1].minimum_version).IsValid());
126 EXPECT_TRUE(
127 SharedModuleInfo::ImportsExtensionById(extension.get(), kImportId1));
128 EXPECT_TRUE(
129 SharedModuleInfo::ImportsExtensionById(extension.get(), kImportId2));
130 EXPECT_FALSE(
131 SharedModuleInfo::ImportsExtensionById(extension.get(), kNoImport));
134 TEST_F(SharedModuleManifestTest, ImportParseErrors) {
135 Testcase testcases[] = {
136 Testcase("shared_module_import_not_list.json",
137 "Invalid value for 'import'."),
138 Testcase("shared_module_import_invalid_id.json",
139 "Invalid value for 'import[0].id'."),
140 Testcase("shared_module_import_invalid_version.json",
141 "Invalid value for 'import[0].minimum_version'."),
143 RunTestcases(testcases, arraysize(testcases), EXPECT_TYPE_ERROR);
146 } // namespace extensions