Commit 5c9f94e9 authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

flags: add expiration browser test

This change adds a browser test (actually an interactive ui test) that ensures
that flags that expire actually end up hidden in the UI. To avoid depending on
the real flag metadata, this change also adds
flags::testing::SetFlagExpiredPredicate to allow tests to control which flags
are considered to have expired.

Bug: 1010678
Change-Id: If6275735105fa552779429f7978d7bf9e76f3687
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1876469Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709011}
parent 4e4b503b
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "chrome/browser/about_flags.h" #include "chrome/browser/about_flags.h"
...@@ -22,7 +23,10 @@ ...@@ -22,7 +23,10 @@
namespace { namespace {
const char kSwitchName[] = "flag-system-test-switch"; const char kSwitchName[] = "flag-system-test-switch";
const char kFlagName[] = "flag-system-test-flag"; const char kFlagName[] = "flag-system-test-flag-1";
const char kExpiredFlagName[] = "flag-system-test-flag-2";
const char kExpiredFlagSwitchName[] = "flag-system-test-expired-switch";
// Command line switch containing an invalid origin. // Command line switch containing an invalid origin.
const char kUnsanitizedCommandLine[] = const char kUnsanitizedCommandLine[] =
...@@ -101,6 +105,17 @@ bool IsDropdownEnabled(content::WebContents* contents, ...@@ -101,6 +105,17 @@ bool IsDropdownEnabled(content::WebContents* contents,
return result; return result;
} }
bool IsFlagPresent(content::WebContents* contents, const char* experiment_id) {
bool result = false;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
contents,
base::StringPrintf("var k = document.getElementById('%s');"
"window.domAutomationController.send(k != null);",
experiment_id),
&result));
return result;
}
void WaitForExperimentalFeatures(content::WebContents* contents) { void WaitForExperimentalFeatures(content::WebContents* contents) {
bool unused; bool unused;
ASSERT_TRUE(content::ExecuteScriptAndExtractBool( ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
...@@ -120,8 +135,14 @@ class AboutFlagsBrowserTest : public InProcessBrowserTest, ...@@ -120,8 +135,14 @@ class AboutFlagsBrowserTest : public InProcessBrowserTest,
public: public:
AboutFlagsBrowserTest() { AboutFlagsBrowserTest() {
about_flags::testing::SetFeatureEntries( about_flags::testing::SetFeatureEntries(
{{"flag-system-test-flag", "name", "description", -1, {{kFlagName, "name-1", "description-1", -1,
ORIGIN_LIST_VALUE_TYPE(kSwitchName, "")}}); ORIGIN_LIST_VALUE_TYPE(kSwitchName, "")},
{kExpiredFlagName, "name-2", "description-2", -1,
SINGLE_VALUE_TYPE(kExpiredFlagSwitchName)}});
flags::testing::SetFlagExpiredPredicate(
base::BindLambdaForTesting([&](const std::string& name) -> bool {
return expiration_enabled_ && name == kExpiredFlagName;
}));
feature_list_.InitWithFeatures({flags::kUnexpireFlagsM76}, {}); feature_list_.InitWithFeatures({flags::kUnexpireFlagsM76}, {});
} }
...@@ -130,6 +151,8 @@ class AboutFlagsBrowserTest : public InProcessBrowserTest, ...@@ -130,6 +151,8 @@ class AboutFlagsBrowserTest : public InProcessBrowserTest,
} }
protected: protected:
void set_expiration_enabled(bool enabled) { expiration_enabled_ = enabled; }
bool has_initial_command_line() const { return GetParam(); } bool has_initial_command_line() const { return GetParam(); }
std::string GetInitialCommandLine() const { std::string GetInitialCommandLine() const {
...@@ -151,6 +174,8 @@ class AboutFlagsBrowserTest : public InProcessBrowserTest, ...@@ -151,6 +174,8 @@ class AboutFlagsBrowserTest : public InProcessBrowserTest,
browser()->tab_strip_model()->GetActiveWebContents()); browser()->tab_strip_model()->GetActiveWebContents());
} }
bool expiration_enabled_ = true;
base::test::ScopedFeatureList feature_list_; base::test::ScopedFeatureList feature_list_;
}; };
...@@ -275,4 +300,19 @@ IN_PROC_BROWSER_TEST_P(AboutFlagsBrowserTest, DISABLED_OriginFlagEnabled) { ...@@ -275,4 +300,19 @@ IN_PROC_BROWSER_TEST_P(AboutFlagsBrowserTest, DISABLED_OriginFlagEnabled) {
base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kSwitchName)); base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(kSwitchName));
} }
IN_PROC_BROWSER_TEST_P(AboutFlagsBrowserTest, ExpiryHidesFlag) {
NavigateToFlagsPage();
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(IsFlagPresent(contents, kFlagName));
EXPECT_FALSE(IsFlagPresent(contents, kExpiredFlagName));
set_expiration_enabled(false);
NavigateToFlagsPage();
contents = browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(IsFlagPresent(contents, kFlagName));
EXPECT_TRUE(IsFlagPresent(contents, kExpiredFlagName));
}
} // namespace } // namespace
...@@ -4,10 +4,36 @@ ...@@ -4,10 +4,36 @@
#include "chrome/browser/unexpire_flags.h" #include "chrome/browser/unexpire_flags.h"
#include "base/no_destructor.h"
#include "chrome/browser/expired_flags_list.h" #include "chrome/browser/expired_flags_list.h"
namespace flags { namespace flags {
namespace {
class FlagPredicateSingleton {
public:
FlagPredicateSingleton() = default;
~FlagPredicateSingleton() = default;
static const testing::FlagPredicate& GetPredicate() {
return GetInstance()->predicate_;
}
static void SetPredicate(testing::FlagPredicate predicate) {
GetInstance()->predicate_ = predicate;
}
private:
static FlagPredicateSingleton* GetInstance() {
static base::NoDestructor<FlagPredicateSingleton> instance;
return instance.get();
}
testing::FlagPredicate predicate_;
};
} // namespace
const base::Feature kUnexpireFlagsM76{"TemporaryUnexpireFlagsM76", const base::Feature kUnexpireFlagsM76{"TemporaryUnexpireFlagsM76",
base::FEATURE_DISABLED_BY_DEFAULT}; base::FEATURE_DISABLED_BY_DEFAULT};
...@@ -35,6 +61,9 @@ bool ExpiryEnabledForMstone(int mstone) { ...@@ -35,6 +61,9 @@ bool ExpiryEnabledForMstone(int mstone) {
} }
bool IsFlagExpired(const char* internal_name) { bool IsFlagExpired(const char* internal_name) {
if (FlagPredicateSingleton::GetPredicate())
return FlagPredicateSingleton::GetPredicate().Run(internal_name);
for (int i = 0; kExpiredFlags[i].name; ++i) { for (int i = 0; kExpiredFlags[i].name; ++i) {
const ExpiredFlag* f = &kExpiredFlags[i]; const ExpiredFlag* f = &kExpiredFlags[i];
if (!strcmp(f->name, internal_name) && ExpiryEnabledForMstone(f->mstone)) if (!strcmp(f->name, internal_name) && ExpiryEnabledForMstone(f->mstone))
...@@ -43,4 +72,12 @@ bool IsFlagExpired(const char* internal_name) { ...@@ -43,4 +72,12 @@ bool IsFlagExpired(const char* internal_name) {
return false; return false;
} }
namespace testing {
void SetFlagExpiredPredicate(FlagPredicate predicate) {
FlagPredicateSingleton::SetPredicate(predicate);
}
} // namespace testing
} // namespace flags } // namespace flags
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_UNEXPIRE_FLAGS_H_ #ifndef CHROME_BROWSER_UNEXPIRE_FLAGS_H_
#define CHROME_BROWSER_UNEXPIRE_FLAGS_H_ #define CHROME_BROWSER_UNEXPIRE_FLAGS_H_
#include "base/callback.h"
#include "base/feature_list.h" #include "base/feature_list.h"
namespace flags { namespace flags {
...@@ -13,6 +14,14 @@ extern const base::Feature kUnexpireFlagsM76; ...@@ -13,6 +14,14 @@ extern const base::Feature kUnexpireFlagsM76;
bool IsFlagExpired(const char* internal_name); bool IsFlagExpired(const char* internal_name);
namespace testing {
using FlagPredicate = base::RepeatingCallback<bool(const std::string&)>;
void SetFlagExpiredPredicate(FlagPredicate predicate);
} // namespace testing
} // namespace flags } // namespace flags
#endif // CHROME_BROWSER_UNEXPIRE_FLAGS_H_ #endif // CHROME_BROWSER_UNEXPIRE_FLAGS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment