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