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"
7 #include "base/command_line.h"
8 #include "base/strings/string_split.h"
9 #include "content/public/common/content_switches.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 void CheckUserAgentStringOrdering(bool mobile_device
) {
15 std::vector
<std::string
> pieces
;
17 // Check if the pieces of the user agent string come in the correct order.
18 ChromeContentClient content_client
;
19 std::string buffer
= content_client
.GetUserAgent();
21 base::SplitStringUsingSubstr(buffer
, "Mozilla/5.0 (", &pieces
);
22 ASSERT_EQ(2u, pieces
.size());
24 EXPECT_EQ("", pieces
[0]);
26 base::SplitStringUsingSubstr(buffer
, ") AppleWebKit/", &pieces
);
27 ASSERT_EQ(2u, pieces
.size());
29 std::string os_str
= pieces
[0];
31 base::SplitStringUsingSubstr(buffer
, " (KHTML, like Gecko) ", &pieces
);
32 ASSERT_EQ(2u, pieces
.size());
34 std::string webkit_version_str
= pieces
[0];
36 base::SplitStringUsingSubstr(buffer
, " Safari/", &pieces
);
37 ASSERT_EQ(2u, pieces
.size());
38 std::string product_str
= pieces
[0];
39 std::string safari_version_str
= pieces
[1];
41 // Not sure what can be done to better check the OS string, since it's highly
42 // platform-dependent.
43 EXPECT_TRUE(os_str
.size() > 0);
45 // Check that the version numbers match.
46 EXPECT_TRUE(webkit_version_str
.size() > 0);
47 EXPECT_TRUE(safari_version_str
.size() > 0);
48 EXPECT_EQ(webkit_version_str
, safari_version_str
);
50 EXPECT_EQ(0u, product_str
.find("Chrome/"));
52 // "Mobile" gets tacked on to the end for mobile devices, like phones.
53 const std::string kMobileStr
= " Mobile";
55 product_str
.substr(product_str
.size() - kMobileStr
.size()));
62 namespace chrome_common
{
64 TEST(ChromeContentClientTest
, Basic
) {
65 #if !defined(OS_ANDROID)
66 CheckUserAgentStringOrdering(false);
68 const char* kArguments
[] = {"chrome"};
70 CommandLine::Init(1, kArguments
);
71 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
73 // Do it for regular devices.
74 ASSERT_FALSE(command_line
->HasSwitch(switches::kUseMobileUserAgent
));
75 CheckUserAgentStringOrdering(false);
77 // Do it for mobile devices.
78 command_line
->AppendSwitch(switches::kUseMobileUserAgent
);
79 ASSERT_TRUE(command_line
->HasSwitch(switches::kUseMobileUserAgent
));
80 CheckUserAgentStringOrdering(true);
84 } // namespace chrome_common