Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 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 | |
| 5 | #include "chrome/common/channel_info.h" |
| 6 | |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 7 | #include "base/debug/debugging_buildflags.h" |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 8 | #include "build/branding_buildflags.h" |
| 9 | #include "build/build_config.h" |
Roman Arora | 8889207 | 2021-10-06 21:40:38 | [diff] [blame] | 10 | #include "build/chromeos_buildflags.h" |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 11 | #include "components/version_info/channel.h" |
| 12 | #include "testing/gmock/include/gmock/gmock.h" |
| 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 16 | #include "chrome/test/base/scoped_channel_override.h" |
| 17 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 18 | |
| 19 | namespace chrome { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | // A bucket of test parameters for ChannelInfoTest. |
| 24 | struct Param { |
| 25 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 26 | Param(ScopedChannelOverride::Channel channel_override, |
| 27 | const char* name_without_es, |
| 28 | const char* name_with_es, |
| 29 | version_info::Channel channel, |
| 30 | bool is_extended_stable, |
| 31 | const char* posix_data_dir_suffix) |
| 32 | : channel_override(channel_override), |
| 33 | channel_name_without_es(name_without_es), |
| 34 | channel_name_with_es(name_with_es), |
| 35 | channel(channel), |
| 36 | is_extended_stable(is_extended_stable), |
| 37 | posix_data_dir_suffix(posix_data_dir_suffix) {} |
| 38 | #else |
| 39 | Param(const char* name_without_es, |
| 40 | const char* name_with_es, |
| 41 | version_info::Channel channel, |
| 42 | bool is_extended_stable, |
| 43 | const char* posix_data_dir_suffix) |
| 44 | : channel_name_without_es(name_without_es), |
| 45 | channel_name_with_es(name_with_es), |
| 46 | channel(channel), |
| 47 | is_extended_stable(is_extended_stable), |
| 48 | posix_data_dir_suffix(posix_data_dir_suffix) {} |
| 49 | #endif |
| 50 | |
| 51 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 52 | // Value to use in the test to override the default channel in branded builds. |
| 53 | const ScopedChannelOverride::Channel channel_override; |
| 54 | #endif |
| 55 | |
| 56 | // Expected channel name when extended stable should not be surfaced. |
| 57 | const char* const channel_name_without_es; |
| 58 | |
| 59 | // Expected channel name when extended stable should be surfaced. |
| 60 | const char* const channel_name_with_es; |
| 61 | |
| 62 | // Expected channel value. |
| 63 | const version_info::Channel channel; |
| 64 | |
| 65 | // Expected extended stable channel value. |
| 66 | const bool is_extended_stable; |
| 67 | |
| 68 | // Suffix for User Data dir (only used for non-Mac Posix). |
| 69 | const char* const posix_data_dir_suffix; |
| 70 | }; |
| 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | // A value-parameterized test to facilitate testing the various channel info |
| 75 | // functions. Branded builds evaluate all tests once for each supported channel. |
| 76 | class ChannelInfoTest : public ::testing::TestWithParam<Param> { |
| 77 | protected: |
| 78 | ChannelInfoTest() = default; |
| 79 | |
| 80 | private: |
| 81 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 82 | ScopedChannelOverride scoped_channel_override_{GetParam().channel_override}; |
| 83 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 84 | }; |
| 85 | |
| 86 | TEST_P(ChannelInfoTest, GetVersionStringWithout) { |
| 87 | const std::string channel_name = GetParam().channel_name_without_es; |
| 88 | if (!channel_name.empty()) { |
| 89 | EXPECT_THAT(GetVersionString(WithExtendedStable(false)), |
| 90 | ::testing::EndsWith(channel_name)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | TEST_P(ChannelInfoTest, GetVersionStringWith) { |
| 95 | const std::string channel_name = GetParam().channel_name_with_es; |
| 96 | if (!channel_name.empty()) { |
| 97 | EXPECT_THAT(GetVersionString(WithExtendedStable(true)), |
| 98 | ::testing::EndsWith(channel_name)); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | TEST_P(ChannelInfoTest, GetChannelNameWithout) { |
| 103 | EXPECT_EQ(GetChannelName(WithExtendedStable(false)), |
| 104 | GetParam().channel_name_without_es); |
| 105 | } |
| 106 | |
| 107 | TEST_P(ChannelInfoTest, GetChannelNameWith) { |
| 108 | EXPECT_EQ(GetChannelName(WithExtendedStable(true)), |
| 109 | GetParam().channel_name_with_es); |
| 110 | } |
| 111 | |
| 112 | TEST_P(ChannelInfoTest, GetChannel) { |
| 113 | EXPECT_EQ(GetChannel(), GetParam().channel); |
| 114 | } |
| 115 | |
| 116 | TEST_P(ChannelInfoTest, IsExtendedStableChannel) { |
| 117 | EXPECT_EQ(IsExtendedStableChannel(), GetParam().is_extended_stable); |
| 118 | } |
| 119 | |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame] | 120 | #if BUILDFLAG(IS_WIN) |
| 121 | #elif BUILDFLAG(IS_MAC) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 122 | |
| 123 | TEST_P(ChannelInfoTest, GetChannelByName) { |
| 124 | EXPECT_EQ(GetChannelByName(GetParam().channel_name_with_es), |
| 125 | GetParam().channel); |
| 126 | } |
| 127 | |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame] | 128 | #elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_CHROMEOS_LACROS) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 129 | |
| 130 | TEST_P(ChannelInfoTest, GetChannelSuffixForDataDir) { |
| 131 | EXPECT_EQ(GetChannelSuffixForDataDir(), GetParam().posix_data_dir_suffix); |
| 132 | } |
| 133 | |
| 134 | #endif |
| 135 | |
| 136 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 137 | // Instantiate the test suite once per supported channel in branded builds. |
| 138 | INSTANTIATE_TEST_SUITE_P( |
| 139 | Stable, |
| 140 | ChannelInfoTest, |
| 141 | ::testing::Values(Param(ScopedChannelOverride::Channel::kStable, |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 142 | #if BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Peter Boström | 2586612 | 2022-06-02 10:25:33 | [diff] [blame] | 143 | "-dcheck", |
| 144 | "-dcheck", |
| 145 | #else |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 146 | "", |
| 147 | "", |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 148 | #endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 149 | version_info::Channel::STABLE, |
| 150 | /*is_extended_stable=*/false, |
| 151 | /*posix_data_dir_suffix=*/""))); |
| 152 | INSTANTIATE_TEST_SUITE_P( |
| 153 | ExtendedStable, |
| 154 | ChannelInfoTest, |
| 155 | ::testing::Values(Param(ScopedChannelOverride::Channel::kExtendedStable, |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 156 | #if BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Peter Boström | 2586612 | 2022-06-02 10:25:33 | [diff] [blame] | 157 | "-dcheck", |
| 158 | "extended-dcheck", |
| 159 | #else |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 160 | "", |
| 161 | "extended", |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 162 | #endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 163 | version_info::Channel::STABLE, |
| 164 | /*is_extended_stable=*/true, |
| 165 | /*posix_data_dir_suffix=*/""))); |
| 166 | INSTANTIATE_TEST_SUITE_P( |
| 167 | Beta, |
| 168 | ChannelInfoTest, |
| 169 | ::testing::Values(Param(ScopedChannelOverride::Channel::kBeta, |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 170 | #if BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Peter Boström | 2586612 | 2022-06-02 10:25:33 | [diff] [blame] | 171 | "beta-dcheck", |
| 172 | "beta-dcheck", |
| 173 | #else |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 174 | "beta", |
| 175 | "beta", |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 176 | #endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 177 | version_info::Channel::BETA, |
| 178 | /*is_extended_stable=*/false, |
| 179 | /*posix_data_dir_suffix=*/"-beta"))); |
| 180 | INSTANTIATE_TEST_SUITE_P( |
| 181 | Dev, |
| 182 | ChannelInfoTest, |
| 183 | ::testing::Values(Param(ScopedChannelOverride::Channel::kDev, |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 184 | #if BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Peter Boström | 2586612 | 2022-06-02 10:25:33 | [diff] [blame] | 185 | "dev-dcheck", |
| 186 | "dev-dcheck", |
| 187 | #else |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 188 | "dev", |
| 189 | "dev", |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 190 | #endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 191 | version_info::Channel::DEV, |
| 192 | /*is_extended_stable=*/false, |
| 193 | /*posix_data_dir_suffix=*/"-unstable"))); |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame] | 194 | #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || \ |
| 195 | BUILDFLAG(IS_FUCHSIA) // No canary channel on Linux. |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 196 | INSTANTIATE_TEST_SUITE_P( |
| 197 | Canary, |
| 198 | ChannelInfoTest, |
| 199 | ::testing::Values(Param(ScopedChannelOverride::Channel::kCanary, |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 200 | #if BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Peter Boström | 2586612 | 2022-06-02 10:25:33 | [diff] [blame] | 201 | "canary-dcheck", |
| 202 | "canary-dcheck", |
| 203 | #else |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 204 | "canary", |
| 205 | "canary", |
Peter Boström | 3f79857 | 2022-07-26 23:44:36 | [diff] [blame] | 206 | #endif // BUILDFLAG(DCHECK_IS_CONFIGURABLE) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 207 | version_info::Channel::CANARY, |
| 208 | /*is_extended_stable=*/false, |
| 209 | /*posix_data_dir_suffix=*/""))); |
Xiaohan Wang | 4d5c504 | 2022-01-18 21:54:37 | [diff] [blame] | 210 | #endif // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) |
Greg Thompson | 32772c1f | 2021-06-29 21:18:48 | [diff] [blame] | 211 | #else // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 212 | INSTANTIATE_TEST_SUITE_P( |
| 213 | Chromium, |
| 214 | ChannelInfoTest, |
| 215 | ::testing::Values(Param("", |
| 216 | "", |
| 217 | version_info::Channel::UNKNOWN, |
| 218 | /*is_extended_stable=*/false, |
| 219 | /*posix_data_dir_suffix=*/""))); |
| 220 | #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 221 | |
| 222 | } // namespace chrome |