1 // Copyright (c) 2012 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 "content/common/plugin_list.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
16 base::FilePath::CharType kFooPath
[] = FILE_PATH_LITERAL("/plugins/foo.plugin");
17 base::FilePath::CharType kBarPath
[] = FILE_PATH_LITERAL("/plugins/bar.plugin");
18 const char* kFooName
= "Foo Plugin";
19 const char* kFooMimeType
= "application/x-foo-mime-type";
20 const char* kFooFileType
= "foo";
22 bool Equals(const WebPluginInfo
& a
, const WebPluginInfo
& b
) {
23 return (a
.name
== b
.name
&&
25 a
.version
== b
.version
&&
29 bool Contains(const std::vector
<WebPluginInfo
>& list
,
30 const WebPluginInfo
& plugin
) {
31 for (std::vector
<WebPluginInfo
>::const_iterator it
= list
.begin();
32 it
!= list
.end(); ++it
) {
33 if (Equals(*it
, plugin
))
41 class PluginListTest
: public testing::Test
{
44 : foo_plugin_(base::ASCIIToUTF16(kFooName
),
45 base::FilePath(kFooPath
),
46 base::ASCIIToUTF16("1.2.3"),
47 base::ASCIIToUTF16("foo")),
48 bar_plugin_(base::ASCIIToUTF16("Bar Plugin"),
49 base::FilePath(kBarPath
),
50 base::ASCIIToUTF16("2.3.4"),
51 base::ASCIIToUTF16("bar")) {
54 void SetUp() override
{
55 plugin_list_
.DisablePluginsDiscovery();
56 plugin_list_
.RegisterInternalPlugin(bar_plugin_
, false);
57 foo_plugin_
.mime_types
.push_back(
58 WebPluginMimeType(kFooMimeType
, kFooFileType
, std::string()));
59 plugin_list_
.RegisterInternalPlugin(foo_plugin_
, false);
63 PluginList plugin_list_
;
64 WebPluginInfo foo_plugin_
;
65 WebPluginInfo bar_plugin_
;
68 TEST_F(PluginListTest
, GetPlugins
) {
69 std::vector
<WebPluginInfo
> plugins
;
70 plugin_list_
.GetPlugins(&plugins
, true);
71 EXPECT_EQ(2u, plugins
.size());
72 EXPECT_TRUE(Contains(plugins
, foo_plugin_
));
73 EXPECT_TRUE(Contains(plugins
, bar_plugin_
));
76 TEST_F(PluginListTest
, BadPluginDescription
) {
77 WebPluginInfo
plugin_3043(
78 base::string16(), base::FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")),
79 base::string16(), base::string16());
80 // Simulate loading of the plugins.
81 plugin_list_
.RegisterInternalPlugin(plugin_3043
, false);
82 // Now we should have them in the state we specified above.
83 plugin_list_
.RefreshPlugins();
84 std::vector
<WebPluginInfo
> plugins
;
85 plugin_list_
.GetPlugins(&plugins
, true);
86 ASSERT_TRUE(Contains(plugins
, plugin_3043
));
89 TEST_F(PluginListTest
, GetPluginInfoArray
) {
90 const char kTargetUrl
[] = "http://example.com/test.foo";
91 GURL
target_url(kTargetUrl
);
92 std::vector
<WebPluginInfo
> plugins
;
93 std::vector
<std::string
> actual_mime_types
;
95 // The file type of the URL is supported by foo_plugin_. However,
96 // GetPluginInfoArray should not match foo_plugin_ because the MIME type is
97 // application/octet-stream.
98 plugin_list_
.GetPluginInfoArray(target_url
,
99 "application/octet-stream",
100 false, // allow_wildcard
102 false, // include_npapi
105 EXPECT_EQ(0u, plugins
.size());
106 EXPECT_EQ(0u, actual_mime_types
.size());
108 // foo_plugin_ matches due to the MIME type.
110 actual_mime_types
.clear();
111 plugin_list_
.GetPluginInfoArray(target_url
,
113 false, // allow_wildcard
115 false, // include_npapi
118 EXPECT_EQ(1u, plugins
.size());
119 EXPECT_TRUE(Contains(plugins
, foo_plugin_
));
120 ASSERT_EQ(1u, actual_mime_types
.size());
121 EXPECT_EQ(kFooMimeType
, actual_mime_types
.front());
123 // foo_plugin_ matches due to the file type and empty MIME type.
125 actual_mime_types
.clear();
126 plugin_list_
.GetPluginInfoArray(target_url
,
128 false, // allow_wildcard
130 false, // include_npapi
133 EXPECT_EQ(1u, plugins
.size());
134 EXPECT_TRUE(Contains(plugins
, foo_plugin_
));
135 ASSERT_EQ(1u, actual_mime_types
.size());
136 EXPECT_EQ(kFooMimeType
, actual_mime_types
.front());
139 } // namespace content