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. 4 5 #include "chrome/common/chrome_content_client.h" 6 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" 11 12 namespace { 13 14 void CheckUserAgentStringOrdering(bool mobile_device) { 15 std::vector<std::string> pieces; 16 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(); 20 21 base::SplitStringUsingSubstr(buffer, "Mozilla/5.0 (", &pieces); 22 ASSERT_EQ(2u, pieces.size()); 23 buffer = pieces[1]; 24 EXPECT_EQ("", pieces[0]); 25 26 base::SplitStringUsingSubstr(buffer, ") AppleWebKit/", &pieces); 27 ASSERT_EQ(2u, pieces.size()); 28 buffer = pieces[1]; 29 std::string os_str = pieces[0]; 30 31 base::SplitStringUsingSubstr(buffer, " (KHTML, like Gecko) ", &pieces); 32 ASSERT_EQ(2u, pieces.size()); 33 buffer = pieces[1]; 34 std::string webkit_version_str = pieces[0]; 35 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]; 40 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); 44 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); 49 50 EXPECT_EQ(0u, product_str.find("Chrome/")); 51 if (mobile_device) { 52 // "Mobile" gets tacked on to the end for mobile devices, like phones. 53 const std::string kMobileStr = " Mobile"; 54 EXPECT_EQ(kMobileStr, 55 product_str.substr(product_str.size() - kMobileStr.size())); 56 } 57 } 58 59 } // namespace 60 61 62 namespace chrome_common { 63 64 TEST(ChromeContentClientTest, Basic) { 65 #if !defined(OS_ANDROID) 66 CheckUserAgentStringOrdering(false); 67 #else 68 const char* kArguments[] = {"chrome"}; 69 CommandLine::Reset(); 70 CommandLine::Init(1, kArguments); 71 CommandLine* command_line = CommandLine::ForCurrentProcess(); 72 73 // Do it for regular devices. 74 ASSERT_FALSE(command_line->HasSwitch(switches::kUseMobileUserAgent)); 75 CheckUserAgentStringOrdering(false); 76 77 // Do it for mobile devices. 78 command_line->AppendSwitch(switches::kUseMobileUserAgent); 79 ASSERT_TRUE(command_line->HasSwitch(switches::kUseMobileUserAgent)); 80 CheckUserAgentStringOrdering(true); 81 #endif 82 } 83 84 } // namespace chrome_common 85