channel_info_unittest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2021 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. #include "chrome/common/channel_info.h"
  5. #include "build/branding_buildflags.h"
  6. #include "build/build_config.h"
  7. #include "build/chromeos_buildflags.h"
  8. #include "components/version_info/channel.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  12. #include "chrome/test/base/scoped_channel_override.h"
  13. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  14. namespace chrome {
  15. namespace {
  16. // A bucket of test parameters for ChannelInfoTest.
  17. struct Param {
  18. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  19. Param(ScopedChannelOverride::Channel channel_override,
  20. const char* name_without_es,
  21. const char* name_with_es,
  22. version_info::Channel channel,
  23. bool is_extended_stable,
  24. const char* posix_data_dir_suffix)
  25. : channel_override(channel_override),
  26. channel_name_without_es(name_without_es),
  27. channel_name_with_es(name_with_es),
  28. channel(channel),
  29. is_extended_stable(is_extended_stable),
  30. posix_data_dir_suffix(posix_data_dir_suffix) {}
  31. #else
  32. Param(const char* name_without_es,
  33. const char* name_with_es,
  34. version_info::Channel channel,
  35. bool is_extended_stable,
  36. const char* posix_data_dir_suffix)
  37. : channel_name_without_es(name_without_es),
  38. channel_name_with_es(name_with_es),
  39. channel(channel),
  40. is_extended_stable(is_extended_stable),
  41. posix_data_dir_suffix(posix_data_dir_suffix) {}
  42. #endif
  43. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  44. // Value to use in the test to override the default channel in branded builds.
  45. const ScopedChannelOverride::Channel channel_override;
  46. #endif
  47. // Expected channel name when extended stable should not be surfaced.
  48. const char* const channel_name_without_es;
  49. // Expected channel name when extended stable should be surfaced.
  50. const char* const channel_name_with_es;
  51. // Expected channel value.
  52. const version_info::Channel channel;
  53. // Expected extended stable channel value.
  54. const bool is_extended_stable;
  55. // Suffix for User Data dir (only used for non-Mac Posix).
  56. const char* const posix_data_dir_suffix;
  57. };
  58. } // namespace
  59. // A value-parameterized test to facilitate testing the various channel info
  60. // functions. Branded builds evaluate all tests once for each supported channel.
  61. class ChannelInfoTest : public ::testing::TestWithParam<Param> {
  62. protected:
  63. ChannelInfoTest() = default;
  64. private:
  65. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  66. ScopedChannelOverride scoped_channel_override_{GetParam().channel_override};
  67. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  68. };
  69. TEST_P(ChannelInfoTest, GetVersionStringWithout) {
  70. const std::string channel_name = GetParam().channel_name_without_es;
  71. if (!channel_name.empty()) {
  72. EXPECT_THAT(GetVersionString(WithExtendedStable(false)),
  73. ::testing::EndsWith(channel_name));
  74. }
  75. }
  76. TEST_P(ChannelInfoTest, GetVersionStringWith) {
  77. const std::string channel_name = GetParam().channel_name_with_es;
  78. if (!channel_name.empty()) {
  79. EXPECT_THAT(GetVersionString(WithExtendedStable(true)),
  80. ::testing::EndsWith(channel_name));
  81. }
  82. }
  83. TEST_P(ChannelInfoTest, GetChannelNameWithout) {
  84. EXPECT_EQ(GetChannelName(WithExtendedStable(false)),
  85. GetParam().channel_name_without_es);
  86. }
  87. TEST_P(ChannelInfoTest, GetChannelNameWith) {
  88. EXPECT_EQ(GetChannelName(WithExtendedStable(true)),
  89. GetParam().channel_name_with_es);
  90. }
  91. TEST_P(ChannelInfoTest, GetChannel) {
  92. EXPECT_EQ(GetChannel(), GetParam().channel);
  93. }
  94. TEST_P(ChannelInfoTest, IsExtendedStableChannel) {
  95. EXPECT_EQ(IsExtendedStableChannel(), GetParam().is_extended_stable);
  96. }
  97. #if BUILDFLAG(IS_WIN)
  98. #elif BUILDFLAG(IS_MAC)
  99. TEST_P(ChannelInfoTest, GetChannelByName) {
  100. EXPECT_EQ(GetChannelByName(GetParam().channel_name_with_es),
  101. GetParam().channel);
  102. }
  103. #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_CHROMEOS_LACROS)
  104. TEST_P(ChannelInfoTest, GetChannelSuffixForDataDir) {
  105. EXPECT_EQ(GetChannelSuffixForDataDir(), GetParam().posix_data_dir_suffix);
  106. }
  107. #endif
  108. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  109. // Instantiate the test suite once per supported channel in branded builds.
  110. INSTANTIATE_TEST_SUITE_P(
  111. Stable,
  112. ChannelInfoTest,
  113. ::testing::Values(Param(ScopedChannelOverride::Channel::kStable,
  114. "",
  115. "",
  116. version_info::Channel::STABLE,
  117. /*is_extended_stable=*/false,
  118. /*posix_data_dir_suffix=*/"")));
  119. INSTANTIATE_TEST_SUITE_P(
  120. ExtendedStable,
  121. ChannelInfoTest,
  122. ::testing::Values(Param(ScopedChannelOverride::Channel::kExtendedStable,
  123. "",
  124. "extended",
  125. version_info::Channel::STABLE,
  126. /*is_extended_stable=*/true,
  127. /*posix_data_dir_suffix=*/"")));
  128. INSTANTIATE_TEST_SUITE_P(
  129. Beta,
  130. ChannelInfoTest,
  131. ::testing::Values(Param(ScopedChannelOverride::Channel::kBeta,
  132. "beta",
  133. "beta",
  134. version_info::Channel::BETA,
  135. /*is_extended_stable=*/false,
  136. /*posix_data_dir_suffix=*/"-beta")));
  137. INSTANTIATE_TEST_SUITE_P(
  138. Dev,
  139. ChannelInfoTest,
  140. ::testing::Values(Param(ScopedChannelOverride::Channel::kDev,
  141. "dev",
  142. "dev",
  143. version_info::Channel::DEV,
  144. /*is_extended_stable=*/false,
  145. /*posix_data_dir_suffix=*/"-unstable")));
  146. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || \
  147. BUILDFLAG(IS_FUCHSIA) // No canary channel on Linux.
  148. INSTANTIATE_TEST_SUITE_P(
  149. Canary,
  150. ChannelInfoTest,
  151. ::testing::Values(Param(ScopedChannelOverride::Channel::kCanary,
  152. "canary",
  153. "canary",
  154. version_info::Channel::CANARY,
  155. /*is_extended_stable=*/false,
  156. /*posix_data_dir_suffix=*/"")));
  157. #endif // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  158. #else // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  159. INSTANTIATE_TEST_SUITE_P(
  160. Chromium,
  161. ChannelInfoTest,
  162. ::testing::Values(Param("",
  163. "",
  164. version_info::Channel::UNKNOWN,
  165. /*is_extended_stable=*/false,
  166. /*posix_data_dir_suffix=*/"")));
  167. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  168. } // namespace chrome