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