Commit fd5e6f0a authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Add content::IsPreTest().

Checks if a test has the PRE_ prefix. Use it for tests that wants to
know, so they do not have to have their own version.

Change-Id: I4c0dbf38ab5c3ab5b088b5564a91acc61f275656
Reviewed-on: https://chromium-review.googlesource.com/786771
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#519753}
parent d4214b50
......@@ -35,6 +35,7 @@
#include "chrome/test/base/testing_profile.h"
#include "components/search_engines/default_search_manager.h"
#include "components/search_engines/template_url_data.h"
#include "content/public/test/test_launcher.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension.h"
#include "services/preferences/public/cpp/tracked/tracked_preference_histogram_names.h"
......@@ -218,7 +219,7 @@ class PrefHashBrowserTestBase
bool SetUpUserDataDirectory() override {
// Do the normal setup in the PRE test and attack preferences in the main
// test.
if (IsPRETest())
if (content::IsPreTest())
return ExtensionBrowserTest::SetUpUserDataDirectory();
#if defined(OS_CHROMEOS)
......@@ -299,7 +300,7 @@ class PrefHashBrowserTestBase
// Keys should be unique, but to avoid flakes in the long run make sure an
// identical test key wasn't left behind by a previous test.
if (IsPRETest()) {
if (content::IsPreTest()) {
base::win::RegKey key;
if (key.Open(HKEY_CURRENT_USER,
registry_key_for_external_validation_.c_str(),
......@@ -314,7 +315,7 @@ class PrefHashBrowserTestBase
void TearDown() override {
#if defined(OS_WIN)
// When done, delete the Registry key to avoid polluting the registry.
if (!IsPRETest()) {
if (!content::IsPreTest()) {
base::string16 registry_key = GetRegistryPathForTestProfile();
base::win::RegKey key;
if (key.Open(HKEY_CURRENT_USER, registry_key.c_str(),
......@@ -343,7 +344,7 @@ class PrefHashBrowserTestBase
num_tracked_prefs_file =
num_tracked_prefs_file.AppendASCII(kNumTrackedPrefFilename);
if (IsPRETest()) {
if (content::IsPreTest()) {
num_tracked_prefs_ = GetTrackedPrefHistogramCount(
user_prefs::tracked::kTrackedPrefHistogramNullInitialized, ALLOW_ANY);
EXPECT_EQ(protection_level_ > PROTECTION_DISABLED_ON_PLATFORM,
......@@ -416,13 +417,6 @@ class PrefHashBrowserTestBase
const SettingsProtectionLevel protection_level_;
private:
// Returns true if this is the PRE_ phase of the test.
bool IsPRETest() {
return base::StartsWith(
testing::UnitTest::GetInstance()->current_test_info()->name(), "PRE_",
base::CompareCase::SENSITIVE);
}
SettingsProtectionLevel GetProtectionLevelFromTrialGroup(
const std::string& trial_group) {
if (!ProfilePrefStoreManager::kPlatformSupportsPreferenceTracking)
......@@ -438,27 +432,24 @@ class PrefHashBrowserTestBase
return PROTECTION_ENABLED_ALL;
#else
return PROTECTION_DISABLED_FOR_GROUP;
#endif
#endif // defined(OS_WIN) || defined(OS_MACOSX)
#else // defined(OFFICIAL_BUILD)
using namespace chrome_prefs::internals;
if (trial_group == kSettingsEnforcementGroupNoEnforcement) {
if (trial_group == kSettingsEnforcementGroupNoEnforcement)
return PROTECTION_DISABLED_FOR_GROUP;
} else if (trial_group == kSettingsEnforcementGroupEnforceAlways) {
if (trial_group == kSettingsEnforcementGroupEnforceAlways)
return PROTECTION_ENABLED_BASIC;
} else if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE) {
if (trial_group == kSettingsEnforcementGroupEnforceAlwaysWithDSE)
return PROTECTION_ENABLED_DSE;
} else if (trial_group ==
kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) {
if (trial_group ==
kSettingsEnforcementGroupEnforceAlwaysWithExtensionsAndDSE) {
return PROTECTION_ENABLED_EXTENSIONS;
} else {
ADD_FAILURE();
return static_cast<SettingsProtectionLevel>(-1);
}
ADD_FAILURE();
return static_cast<SettingsProtectionLevel>(-1);
#endif // defined(OFFICIAL_BUILD)
}
int num_tracked_prefs_;
......
......@@ -25,6 +25,7 @@
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/testing_browser_process.h"
#include "content/public/test/test_launcher.h"
namespace {
......@@ -187,11 +188,8 @@ if (testing::UnitTest::GetInstance()->current_test_info()->name() == \
// If control goes here, it means SetUpUserDataDirectory is not handled.
// This is okay for PRE_ tests, but not acceptable for main tests.
if (base::StartsWith(
testing::UnitTest::GetInstance()->current_test_info()->name(),
"PRE_", base::CompareCase::SENSITIVE)) {
if (content::IsPreTest())
return true;
}
ADD_FAILURE() << "SetUpUserDataDirectory is not handled by the test.";
return false;
......
......@@ -16,6 +16,7 @@
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_launcher.h"
#include "content/shell/browser/shell.h"
namespace content {
......@@ -123,9 +124,7 @@ class DOMStorageMigrationBrowserTest : public DOMStorageBrowserTest {
void SetUpCommandLine(base::CommandLine* command_line) override {
ContentBrowserTest::SetUpCommandLine(command_line);
// Only enable mojo local storage if this is not a PRE_ test.
const testing::TestInfo* test =
testing::UnitTest::GetInstance()->current_test_info();
if (base::StartsWith(test->name(), "PRE_", base::CompareCase::SENSITIVE))
if (IsPreTest())
command_line->AppendSwitch(switches::kDisableMojoLocalStorage);
}
};
......
......@@ -10,6 +10,7 @@
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/bind_helpers.h"
......@@ -255,6 +256,11 @@ bool WrapperTestLauncherDelegate::GetTests(
return true;
}
bool IsPreTestName(const std::string& test_name) {
return base::StartsWith(test_name, kPreTestPrefix,
base::CompareCase::SENSITIVE);
}
bool WrapperTestLauncherDelegate::ShouldRunTest(
const std::string& test_case_name,
const std::string& test_name) {
......@@ -266,8 +272,7 @@ bool WrapperTestLauncherDelegate::ShouldRunTest(
return false;
}
if (base::StartsWith(test_name, kPreTestPrefix,
base::CompareCase::SENSITIVE)) {
if (IsPreTestName(test_name)) {
// We will actually run PRE_ tests, but to ensure they run on the same shard
// as dependent tests, handle all these details internally.
return false;
......@@ -295,8 +300,8 @@ size_t WrapperTestLauncherDelegate::RunTests(
size_t additional_tests_to_run_count = 0;
// Compute dependencies of tests to be run.
for (size_t i = 0; i < test_names.size(); i++) {
std::string full_name(test_names[i]);
for (const std::string& test_name : test_names) {
std::string full_name(test_name);
std::string pre_test_name(GetPreTestName(full_name));
while (base::ContainsKey(all_test_names_, pre_test_name)) {
......@@ -313,9 +318,8 @@ size_t WrapperTestLauncherDelegate::RunTests(
}
}
for (size_t i = 0; i < test_names.size(); i++) {
std::string full_name(test_names[i]);
for (const std::string& test_name : test_names) {
std::string full_name(test_name);
// Make sure no PRE_ tests were requested explicitly.
DCHECK_EQ(full_name, RemoveAnyPrePrefixes(full_name));
......@@ -349,31 +353,25 @@ size_t WrapperTestLauncherDelegate::RetryTests(
// In the face of PRE_ tests, we need to retry the entire chain of tests,
// from the very first one.
for (size_t i = 0; i < test_names.size(); i++) {
std::string test_name(test_names[i]);
while (base::ContainsKey(reverse_dependent_test_map_, test_name)) {
test_name = reverse_dependent_test_map_[test_name];
test_names_set.insert(test_name);
for (const std::string& test_name : test_names) {
std::string name(test_name);
while (base::ContainsKey(reverse_dependent_test_map_, name)) {
name = reverse_dependent_test_map_[name];
test_names_set.insert(name);
}
}
// Discard user data directories from any previous runs. Start with
// fresh state.
for (UserDataDirMap::const_iterator i = user_data_dir_map_.begin();
i != user_data_dir_map_.end();
++i) {
for (const auto& it : user_data_dir_map_) {
// Delete temporary directories now to avoid using too much space in /tmp.
if (!base::DeleteFile(i->second, true)) {
LOG(WARNING) << "Failed to delete " << i->second.value();
if (!base::DeleteFile(it.second, true)) {
LOG(WARNING) << "Failed to delete " << it.second.value();
}
}
user_data_dir_map_.clear();
for (std::set<std::string>::const_iterator i = test_names_set.begin();
i != test_names_set.end();
++i) {
std::string full_name(*i);
for (const std::string& full_name : test_names_set) {
// Make sure PRE_ tests and tests that depend on them share the same
// data directory - based it on the test name without prefixes.
std::string test_name_no_pre(RemoveAnyPrePrefixes(full_name));
......@@ -676,4 +674,9 @@ ContentMainParams* GetContentMainParams() {
return g_params;
}
bool IsPreTest() {
auto* test = testing::UnitTest::GetInstance();
return IsPreTestName(test->current_test_info()->name());
}
} // namespace content
......@@ -85,6 +85,10 @@ int LaunchTests(TestLauncherDelegate* launcher_delegate,
TestLauncherDelegate* GetCurrentTestLauncherDelegate();
ContentMainParams* GetContentMainParams();
// Returns true if the currently running test has a prefix that indicates it
// should run before a test of the same name without the prefix.
bool IsPreTest();
} // namespace content
#endif // CONTENT_PUBLIC_TEST_TEST_LAUNCHER_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