1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/raw_ptr.h"
#include "chrome/browser/about_flags.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/toolbar/chrome_labs/chrome_labs_model.h"
#include "chrome/browser/ui/toolbar/chrome_labs/chrome_labs_prefs.h"
#include "chrome/browser/ui/toolbar/chrome_labs/chrome_labs_utils.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/toolbar/chrome_labs/chrome_labs_bubble_view.h"
#include "chrome/browser/ui/views/toolbar/chrome_labs/chrome_labs_coordinator.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/unexpire_flags.h"
#include "components/webui/flags/feature_entry_macros.h"
#include "ui/events/event_utils.h"
#include "ui/views/controls/dot_indicator.h"
#include "ui/views/test/button_test_api.h"
#include "ui/views/test/widget_test.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash.h"
#include "chrome/browser/ash/ownership/owner_settings_service_ash_factory.h"
#include "chrome/common/pref_names.h"
#endif
#if BUILDFLAG(GOOGLE_CHROME_BRANDING) && !BUILDFLAG(IS_CHROMEOS)
#include "chrome/test/base/scoped_channel_override.h"
#endif
#if !BUILDFLAG(IS_CHROMEOS) || !BUILDFLAG(GOOGLE_CHROME_BRANDING)
namespace {
const char kFirstTestFeatureId[] = "feature-1";
BASE_FEATURE(kTestFeature1, "FeatureName1", base::FEATURE_ENABLED_BY_DEFAULT);
const char kSecondTestFeatureId[] = "feature-2";
BASE_FEATURE(kTestFeature2, "FeatureName2", base::FEATURE_DISABLED_BY_DEFAULT);
const char kExpiredFlagTestFeatureId[] = "expired-feature";
BASE_FEATURE(kTestFeatureExpired, "Expired", base::FEATURE_DISABLED_BY_DEFAULT);
} // namespace
class ChromeLabsButtonTest : public TestWithBrowserView {
public:
ChromeLabsButtonTest()
:
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
channel_override_(chrome::ScopedChannelOverride(
chrome::ScopedChannelOverride::Channel::kDev)),
#endif
scoped_feature_entries_({{kFirstTestFeatureId, "", "",
flags_ui::FlagsState::GetCurrentPlatform(),
FEATURE_VALUE_TYPE(kTestFeature1)}})
{
ForceChromeLabsActivationForTesting();
}
void SetUp() override {
std::vector<LabInfo> test_feature_info = {
{kFirstTestFeatureId, u"", u"", "", version_info::Channel::STABLE}};
scoped_chrome_labs_model_data_.SetModelDataForTesting(test_feature_info);
TestWithBrowserView::SetUp();
profile()->GetPrefs()->SetBoolean(
chrome_labs_prefs::kBrowserLabsEnabledEnterprisePolicy, true);
browser_view()
->toolbar()
->pinned_toolbar_actions_container()
->ShowActionEphemerallyInToolbar(kActionShowChromeLabs, true);
}
private:
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
chrome::ScopedChannelOverride channel_override_;
#endif
about_flags::testing::ScopedFeatureEntries scoped_feature_entries_;
ScopedChromeLabsModelDataForTesting scoped_chrome_labs_model_data_;
};
TEST_F(ChromeLabsButtonTest, ShowAndHideChromeLabsBubbleOnPress) {
views::Button* labs_button = browser_view()->toolbar()->GetChromeLabsButton();
ChromeLabsCoordinator* coordinator =
browser_view()->browser()->GetFeatures().chrome_labs_coordinator();
#if BUILDFLAG(IS_CHROMEOS)
ash::OwnerSettingsServiceAsh* service_ =
ash::OwnerSettingsServiceAshFactory::GetForBrowserContext(GetProfile());
coordinator->SetShouldCircumventDeviceCheckForTesting(true);
#endif
EXPECT_FALSE(coordinator->BubbleExists());
ui::MouseEvent e(ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), 0, 0);
views::test::ButtonTestApi test_api(labs_button);
test_api.NotifyClick(e);
#if BUILDFLAG(IS_CHROMEOS)
service_->RunPendingIsOwnerCallbacksForTesting(/*is_owner=*/false);
#endif
EXPECT_TRUE(coordinator->BubbleExists());
views::test::WidgetDestroyedWaiter destroyed_waiter(
coordinator->GetChromeLabsBubbleView()->GetWidget());
test_api.NotifyClick(e);
destroyed_waiter.Wait();
EXPECT_FALSE(coordinator->BubbleExists());
}
TEST_F(ChromeLabsButtonTest, ShouldButtonShowTest) {
// There are experiments available so the button should not be nullptr.
EXPECT_NE(browser_view()->toolbar()->GetChromeLabsButton(), nullptr);
// Enterprise policy is initially set to true.
EXPECT_TRUE(browser_view()->toolbar()->GetChromeLabsButton()->GetVisible());
// Default enterprise policy value should show the Chrome Labs button.
profile()->GetPrefs()->ClearPref(
chrome_labs_prefs::kBrowserLabsEnabledEnterprisePolicy);
EXPECT_TRUE(browser_view()->toolbar()->GetChromeLabsButton()->GetVisible());
profile()->GetPrefs()->SetBoolean(
chrome_labs_prefs::kBrowserLabsEnabledEnterprisePolicy, false);
EXPECT_FALSE(browser_view()->toolbar()->GetChromeLabsButton()->GetVisible());
}
TEST_F(ChromeLabsButtonTest, ShouldButtonShowEphemerallyTest) {
// Reset the value set during setup to ensure the button doesn't artificially
// show ephemerally during this test.
browser_view()
->toolbar()
->pinned_toolbar_actions_container()
->ShowActionEphemerallyInToolbar(kActionShowChromeLabs, false);
EXPECT_EQ(browser_view()->toolbar()->GetChromeLabsButton(), nullptr);
ChromeLabsCoordinator* coordinator =
browser_view()->browser()->GetFeatures().chrome_labs_coordinator();
coordinator->Show();
// Showing the bubble when the button was not previously showing should cause
// it to show.
EXPECT_TRUE(coordinator->BubbleExists());
EXPECT_NE(browser_view()->toolbar()->GetChromeLabsButton(), nullptr);
EXPECT_TRUE(browser_view()->toolbar()->GetChromeLabsButton()->GetVisible());
views::test::WidgetDestroyedWaiter destroyed_waiter(
coordinator->GetChromeLabsBubbleView()->GetWidget());
coordinator->Hide();
destroyed_waiter.Wait();
// Hiding the bubble should cause the ephemeral button to hide.
EXPECT_EQ(browser_view()->toolbar()->GetChromeLabsButton(), nullptr);
}
TEST_F(ChromeLabsButtonTest, DotIndicatorTest) {
views::Button* labs_button = browser_view()->toolbar()->GetChromeLabsButton();
ChromeLabsCoordinator* coordinator =
browser_view()->browser()->GetFeatures().chrome_labs_coordinator();
coordinator->MaybeInstallDotIndicator();
views::DotIndicator* dot_indicator = coordinator->GetDotIndicator();
EXPECT_TRUE(dot_indicator->GetVisible());
ui::MouseEvent e(ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), 0, 0);
views::test::ButtonTestApi test_api(labs_button);
test_api.NotifyClick(e);
EXPECT_FALSE(dot_indicator->GetVisible());
}
#if BUILDFLAG(IS_CHROMEOS)
class ChromeLabsButtonTestSafeMode : public ChromeLabsButtonTest {
public:
ChromeLabsButtonTestSafeMode() : ChromeLabsButtonTest() {}
void SetUp() override {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
ash::switches::kSafeMode);
ChromeLabsButtonTest::SetUp();
}
void TearDown() override {
base::CommandLine::ForCurrentProcess()->RemoveSwitch(
ash::switches::kSafeMode);
ChromeLabsButtonTest::TearDown();
}
};
TEST_F(ChromeLabsButtonTestSafeMode, ButtonShouldNotShowTest) {
EXPECT_FALSE(browser_view()->toolbar()->GetChromeLabsButton()->GetVisible());
}
class ChromeLabsButtonTestSecondaryUser : public ChromeLabsButtonTest {
public:
ChromeLabsButtonTestSecondaryUser() : ChromeLabsButtonTest() {}
void LogIn(std::string_view email, const GaiaId& gaia_id) override {
// Fake primary user log-in, so that the created profile will be interpreted
// as secondary user's profile.
ChromeLabsButtonTest::LogIn("primary-user@domain.com", GaiaId("fakegaia1"));
ChromeLabsButtonTest::LogIn(email, gaia_id);
}
};
TEST_F(ChromeLabsButtonTestSecondaryUser, ButtonShouldNotShowTest) {
EXPECT_FALSE(browser_view()->toolbar()->GetChromeLabsButton()->GetVisible());
}
#endif
class ChromeLabsButtonNoExperimentsAvailableTest : public TestWithBrowserView {
public:
ChromeLabsButtonNoExperimentsAvailableTest()
:
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
channel_override_(chrome::ScopedChannelOverride(
chrome::ScopedChannelOverride::Channel::kDev)),
#endif
scoped_feature_entries_({{kSecondTestFeatureId, "", "", 0,
FEATURE_VALUE_TYPE(kTestFeature2)}}) {
ForceChromeLabsActivationForTesting();
}
void SetUp() override {
std::vector<LabInfo> test_feature_info = {
{kSecondTestFeatureId, u"", u"", "", version_info::Channel::STABLE}};
scoped_chrome_labs_model_data_.SetModelDataForTesting(test_feature_info);
TestWithBrowserView::SetUp();
profile()->GetPrefs()->SetBoolean(
chrome_labs_prefs::kBrowserLabsEnabledEnterprisePolicy, true);
}
private:
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
chrome::ScopedChannelOverride channel_override_;
#endif
about_flags::testing::ScopedFeatureEntries scoped_feature_entries_;
ScopedChromeLabsModelDataForTesting scoped_chrome_labs_model_data_;
};
TEST_F(ChromeLabsButtonNoExperimentsAvailableTest, ButtonShouldNotShowTest) {
EXPECT_EQ(browser_view()->toolbar()->GetChromeLabsButton(), nullptr);
}
class ChromeLabsButtonOnlyExpiredFeaturesAvailableTest
: public TestWithBrowserView {
public:
ChromeLabsButtonOnlyExpiredFeaturesAvailableTest()
:
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
channel_override_(chrome::ScopedChannelOverride(
chrome::ScopedChannelOverride::Channel::kDev)),
#endif
scoped_feature_entries_({{kExpiredFlagTestFeatureId, "", "",
flags_ui::FlagsState::GetCurrentPlatform(),
FEATURE_VALUE_TYPE(kTestFeatureExpired)}}) {
flags::testing::SetFlagExpiration(kExpiredFlagTestFeatureId, 0);
ForceChromeLabsActivationForTesting();
}
void SetUp() override {
std::vector<LabInfo> test_feature_info = {{kExpiredFlagTestFeatureId, u"",
u"", "",
version_info::Channel::STABLE}};
scoped_chrome_labs_model_data_.SetModelDataForTesting(test_feature_info);
TestWithBrowserView::SetUp();
profile()->GetPrefs()->SetBoolean(
chrome_labs_prefs::kBrowserLabsEnabledEnterprisePolicy, true);
}
private:
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
chrome::ScopedChannelOverride channel_override_;
#endif
about_flags::testing::ScopedFeatureEntries scoped_feature_entries_;
ScopedChromeLabsModelDataForTesting scoped_chrome_labs_model_data_;
};
TEST_F(ChromeLabsButtonOnlyExpiredFeaturesAvailableTest,
ButtonShouldNotShowTest) {
EXPECT_EQ(browser_view()->toolbar()->GetChromeLabsButton(), nullptr);
}
#endif // !BUILDFLAG(IS_CHROMEOS) || !BUILDFLAG(GOOGLE_CHROME_BRANDING)
|