user_agent_unittest.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2017 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. #import "ios/web/common/user_agent.h"
  5. #include "base/strings/stringprintf.h"
  6. #include "base/system/sys_info.h"
  7. #include "base/test/scoped_feature_list.h"
  8. #include "ios/web/common/features.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "testing/platform_test.h"
  11. #import "third_party/ocmock/OCMock/OCMock.h"
  12. #include "ui/base/device_form_factor.h"
  13. #if !defined(__has_feature) || !__has_feature(objc_arc)
  14. #error "This file requires ARC support."
  15. #endif
  16. namespace {
  17. const char kDesktopUserAgentWithProduct[] =
  18. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
  19. "AppleWebKit/605.1.15 (KHTML, like Gecko) desktop_product_name "
  20. "Version/11.1.1 "
  21. "Safari/605.1.15";
  22. const char kDesktopUserAgent[] =
  23. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) "
  24. "AppleWebKit/605.1.15 (KHTML, like Gecko) "
  25. "Version/11.1.1 "
  26. "Safari/605.1.15";
  27. } // namespace
  28. namespace web {
  29. using UserAgentTest = PlatformTest;
  30. // Tests conversions between UserAgentType values and their descriptions
  31. TEST_F(UserAgentTest, UserAgentTypeDescription) {
  32. const std::string kMobileDescription("MOBILE");
  33. const std::string kDesktopDescription("DESKTOP");
  34. const std::string kAutomaticDescription("AUTOMATIC");
  35. const std::string kNoneDescription("NONE");
  36. const std::string kInvalidDescription(
  37. "not returned by GetUserAgentTypeDescription()");
  38. EXPECT_EQ(kMobileDescription,
  39. GetUserAgentTypeDescription(UserAgentType::MOBILE));
  40. EXPECT_EQ(kDesktopDescription,
  41. GetUserAgentTypeDescription(UserAgentType::DESKTOP));
  42. EXPECT_EQ(kNoneDescription, GetUserAgentTypeDescription(UserAgentType::NONE));
  43. EXPECT_EQ(UserAgentType::MOBILE,
  44. GetUserAgentTypeWithDescription(kMobileDescription));
  45. EXPECT_EQ(UserAgentType::DESKTOP,
  46. GetUserAgentTypeWithDescription(kDesktopDescription));
  47. EXPECT_EQ(UserAgentType::NONE,
  48. GetUserAgentTypeWithDescription(kNoneDescription));
  49. EXPECT_EQ(UserAgentType::NONE,
  50. GetUserAgentTypeWithDescription(kInvalidDescription));
  51. EXPECT_EQ(kAutomaticDescription,
  52. GetUserAgentTypeDescription(UserAgentType::AUTOMATIC));
  53. EXPECT_EQ(UserAgentType::AUTOMATIC,
  54. GetUserAgentTypeWithDescription(kAutomaticDescription));
  55. }
  56. // Tests the mobile user agent returned for a specific product.
  57. TEST_F(UserAgentTest, MobileUserAgentForProduct) {
  58. std::string product = "my_product_name";
  59. std::string platform;
  60. std::string cpu;
  61. if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) {
  62. platform = "iPad";
  63. cpu = "OS";
  64. } else {
  65. platform = "iPhone";
  66. cpu = "iPhone OS";
  67. }
  68. std::string os_version;
  69. int32_t os_major_version = 0;
  70. int32_t os_minor_version = 0;
  71. int32_t os_bugfix_version = 0;
  72. base::SysInfo::OperatingSystemVersionNumbers(
  73. &os_major_version, &os_minor_version, &os_bugfix_version);
  74. base::StringAppendF(&os_version, "%d_%d", os_major_version, os_minor_version);
  75. std::string expected_user_agent;
  76. base::StringAppendF(
  77. &expected_user_agent,
  78. "Mozilla/5.0 (%s; CPU %s %s like Mac OS X) AppleWebKit/605.1.15 (KHTML, "
  79. "like Gecko) %s Mobile/15E148 Safari/604.1",
  80. platform.c_str(), cpu.c_str(), os_version.c_str(), product.c_str());
  81. std::string result = BuildMobileUserAgent(product);
  82. EXPECT_EQ(expected_user_agent, result);
  83. }
  84. // Tests the desktop user agent, checking that the product isn't taken into
  85. // account when it is empty.
  86. TEST_F(UserAgentTest, DesktopUserAgentForProduct) {
  87. EXPECT_EQ(kDesktopUserAgent, BuildDesktopUserAgent(""));
  88. }
  89. // Tests the desktop user agent for a specific product name.
  90. TEST_F(UserAgentTest, DesktopUserAgentWithProduct) {
  91. EXPECT_EQ(kDesktopUserAgentWithProduct,
  92. BuildDesktopUserAgent("desktop_product_name"));
  93. }
  94. } // namespace web