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 "chrome/common/chrome_content_client.h"
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/strings/string_split.h"
13 #include "content/public/common/content_switches.h"
14 #include "extensions/common/constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/origin.h"
18 #include "url/url_util.h"
22 void CheckUserAgentStringOrdering(bool mobile_device
) {
23 std::vector
<std::string
> pieces
;
25 // Check if the pieces of the user agent string come in the correct order.
26 ChromeContentClient content_client
;
27 std::string buffer
= content_client
.GetUserAgent();
29 base::SplitStringUsingSubstr(buffer
, "Mozilla/5.0 (", &pieces
);
30 ASSERT_EQ(2u, pieces
.size());
32 EXPECT_EQ("", pieces
[0]);
34 base::SplitStringUsingSubstr(buffer
, ") AppleWebKit/", &pieces
);
35 ASSERT_EQ(2u, pieces
.size());
37 std::string os_str
= pieces
[0];
39 base::SplitStringUsingSubstr(buffer
, " (KHTML, like Gecko) ", &pieces
);
40 ASSERT_EQ(2u, pieces
.size());
42 std::string webkit_version_str
= pieces
[0];
44 base::SplitStringUsingSubstr(buffer
, " Safari/", &pieces
);
45 ASSERT_EQ(2u, pieces
.size());
46 std::string product_str
= pieces
[0];
47 std::string safari_version_str
= pieces
[1];
49 // Not sure what can be done to better check the OS string, since it's highly
50 // platform-dependent.
51 EXPECT_TRUE(os_str
.size() > 0);
53 // Check that the version numbers match.
54 EXPECT_TRUE(webkit_version_str
.size() > 0);
55 EXPECT_TRUE(safari_version_str
.size() > 0);
56 EXPECT_EQ(webkit_version_str
, safari_version_str
);
58 EXPECT_EQ(0u, product_str
.find("Chrome/"));
60 // "Mobile" gets tacked on to the end for mobile devices, like phones.
61 const std::string kMobileStr
= " Mobile";
63 product_str
.substr(product_str
.size() - kMobileStr
.size()));
70 namespace chrome_common
{
72 TEST(ChromeContentClientTest
, Basic
) {
73 #if !defined(OS_ANDROID)
74 CheckUserAgentStringOrdering(false);
76 const char* const kArguments
[] = {"chrome"};
77 base::CommandLine::Reset();
78 base::CommandLine::Init(1, kArguments
);
79 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
81 // Do it for regular devices.
82 ASSERT_FALSE(command_line
->HasSwitch(switches::kUseMobileUserAgent
));
83 CheckUserAgentStringOrdering(false);
85 // Do it for mobile devices.
86 command_line
->AppendSwitch(switches::kUseMobileUserAgent
);
87 ASSERT_TRUE(command_line
->HasSwitch(switches::kUseMobileUserAgent
));
88 CheckUserAgentStringOrdering(true);
92 #if defined(ENABLE_PLUGINS)
93 TEST(ChromeContentClientTest
, FindMostRecent
) {
94 ScopedVector
<content::PepperPluginInfo
> vector1
;
95 // Test an empty vector.
96 EXPECT_EQ(ChromeContentClient::FindMostRecentPlugin(vector1
.get()), nullptr);
98 // Now test the vector with one element.
99 content::PepperPluginInfo
* info1
= new content::PepperPluginInfo();
100 info1
->version
= "1.0.0.0";
101 vector1
.push_back(info1
);
102 EXPECT_EQ(ChromeContentClient::FindMostRecentPlugin(vector1
.get()), info1
);
104 // Now do the generic test of a complex vector.
105 content::PepperPluginInfo
* info2
= new content::PepperPluginInfo();
106 info2
->version
= "2.0.0.1";
107 content::PepperPluginInfo
* info3
= new content::PepperPluginInfo();
108 info3
->version
= "3.5.6.7";
109 content::PepperPluginInfo
* info4
= new content::PepperPluginInfo();
110 info4
->version
= "4.0.0.153";
111 content::PepperPluginInfo
* info5
= new content::PepperPluginInfo();
112 info5
->version
= "5.0.12.1";
113 content::PepperPluginInfo
* info6_12
= new content::PepperPluginInfo();
114 info6_12
->version
= "6.0.0.12";
115 content::PepperPluginInfo
* info6_13
= new content::PepperPluginInfo();
116 info6_13
->version
= "6.0.0.13";
118 ScopedVector
<content::PepperPluginInfo
> vector2
;
119 vector2
.push_back(info4
);
120 vector2
.push_back(info2
);
121 vector2
.push_back(info6_13
);
122 vector2
.push_back(info3
);
123 vector2
.push_back(info5
);
124 vector2
.push_back(info6_12
);
126 EXPECT_EQ(ChromeContentClient::FindMostRecentPlugin(vector2
.get()), info6_13
);
128 #endif // defined(ENABLE_PLUGINS)
130 TEST(ChromeContentClientTest
, AdditionalSchemes
) {
131 EXPECT_TRUE(url::IsStandard(
132 extensions::kExtensionScheme
,
133 url::Component(0, strlen(extensions::kExtensionScheme
))));
136 "chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef/foo.html");
137 url::Origin
origin(extension_url
);
138 EXPECT_EQ("chrome-extension://abcdefghijklmnopqrstuvwxyzabcdef",
142 } // namespace chrome_common