This source file includes following definitions.
- OnUnblockOnProfileCreation
- GetUnloadedProfilePath
- GetTrackedPrefHistogramCount
- SetUpCommandLine
- SetUpInProcessBrowserTestFixture
- SetUpOnMainThread
- IsUnloadedProfileSeedingAllowed
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
- IN_PROC_BROWSER_TEST_P
#include <set>
#include <vector>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/statistics_recorder.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string16.h"
#include "base/threading/sequenced_worker_pool.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/chrome_pref_service_factory.h"
#include "chrome/browser/prefs/pref_hash_store.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profiles_state.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
void OnUnblockOnProfileCreation(const base::Closure& callback,
Profile* profile,
Profile::CreateStatus status) {
switch (status) {
case Profile::CREATE_STATUS_CREATED:
break;
case Profile::CREATE_STATUS_INITIALIZED:
callback.Run();
break;
default:
ADD_FAILURE() << "Unexpected Profile::CreateStatus: " << status;
callback.Run();
break;
}
}
base::FilePath GetUnloadedProfilePath() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
const ProfileInfoCache& cache = profile_manager->GetProfileInfoCache();
const std::vector<Profile*> loaded_profiles =
profile_manager->GetLoadedProfiles();
std::set<base::FilePath> profile_paths;
for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i)
profile_paths.insert(cache.GetPathOfProfileAtIndex(i));
for (size_t i = 0; i < loaded_profiles.size(); ++i)
EXPECT_EQ(1U, profile_paths.erase(loaded_profiles[i]->GetPath()));
if (profile_paths.size())
return *profile_paths.begin();
return base::FilePath();
}
int GetTrackedPrefHistogramCount(const char* histogram_name, bool expect_zero) {
const base::HistogramBase* histogram =
base::StatisticsRecorder::FindHistogram(histogram_name);
if (!histogram)
return 0;
scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
int sum = 0;
for (int i = 0; i < 100; ++i) {
int count_for_id = samples->GetCount(i);
sum += count_for_id;
if (expect_zero)
EXPECT_EQ(0, count_for_id) << "Faulty reporting_id: " << i;
}
return sum;
}
}
class PrefHashBrowserTest : public InProcessBrowserTest,
public testing::WithParamInterface<std::string> {
public:
PrefHashBrowserTest()
: is_unloaded_profile_seeding_allowed_(
IsUnloadedProfileSeedingAllowed()) {}
virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
InProcessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(
switches::kForceFieldTrials,
std::string(chrome_prefs::internals::kSettingsEnforcementTrialName) +
"/" + GetParam() + "/");
}
virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
chrome_prefs::DisableDelaysAndDomainCheckForTesting();
}
virtual void SetUpOnMainThread() OVERRIDE {
content::BrowserThread::GetBlockingPool()->FlushForTesting();
content::RunAllPendingInMessageLoop();
}
protected:
const bool is_unloaded_profile_seeding_allowed_;
private:
bool IsUnloadedProfileSeedingAllowed() const {
const bool allowed_from_param =
GetParam() !=
chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways &&
GetParam() != chrome_prefs::internals::
kSettingsEnforcementGroupEnforceAlwaysWithExtensions;
const bool blocked_by_configuration =
#if defined(OFFICIAL_BUILD)
true;
#else
false;
#endif
return allowed_from_param && !blocked_by_configuration;
}
};
#if defined(OS_CHROMEOS)
#define MAYBE_PRE_PRE_InitializeUnloadedProfiles DISABLED_PRE_PRE_InitializeUnloadedProfiles
#define MAYBE_PRE_InitializeUnloadedProfiles DISABLED_PRE_InitializeUnloadedProfiles
#define MAYBE_InitializeUnloadedProfiles DISABLED_InitializeUnloadedProfiles
#else
#define MAYBE_PRE_PRE_InitializeUnloadedProfiles PRE_PRE_InitializeUnloadedProfiles
#define MAYBE_PRE_InitializeUnloadedProfiles PRE_InitializeUnloadedProfiles
#define MAYBE_InitializeUnloadedProfiles InitializeUnloadedProfiles
#endif
IN_PROC_BROWSER_TEST_P(PrefHashBrowserTest,
MAYBE_PRE_PRE_InitializeUnloadedProfiles) {
if (!profiles::IsMultipleProfilesEnabled())
return;
ProfileManager* profile_manager = g_browser_process->profile_manager();
const base::FilePath new_path =
profile_manager->GenerateNextProfileDirectoryPath();
const scoped_refptr<content::MessageLoopRunner> runner(
new content::MessageLoopRunner);
profile_manager->CreateProfileAsync(
new_path,
base::Bind(&OnUnblockOnProfileCreation, runner->QuitClosure()),
base::string16(),
base::string16(),
std::string());
runner->Run();
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom",
true));
}
IN_PROC_BROWSER_TEST_P(PrefHashBrowserTest,
MAYBE_PRE_InitializeUnloadedProfiles) {
if (!profiles::IsMultipleProfilesEnabled())
return;
const base::DictionaryValue* hashes =
g_browser_process->local_state()->GetDictionary(
prefs::kProfilePreferenceHashes);
EXPECT_EQ(4U, hashes->size());
const base::FilePath unloaded_profile_path = GetUnloadedProfilePath();
chrome_prefs::ResetPrefHashStore(unloaded_profile_path);
EXPECT_EQ(3U, hashes->size());
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom",
true));
}
IN_PROC_BROWSER_TEST_P(PrefHashBrowserTest,
MAYBE_InitializeUnloadedProfiles) {
if (!profiles::IsMultipleProfilesEnabled())
return;
const base::DictionaryValue* hashes =
g_browser_process->local_state()->GetDictionary(
prefs::kProfilePreferenceHashes);
if (is_unloaded_profile_seeding_allowed_) {
EXPECT_EQ(4U, hashes->size());
EXPECT_EQ(
1, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom",
false));
} else {
EXPECT_EQ(3U, hashes->size());
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferencesAlternateStoreVersionUpdatedFrom",
true));
}
ProfileManager* profile_manager = g_browser_process->profile_manager();
ASSERT_EQ(1U, profile_manager->GetLoadedProfiles().size());
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceChanged", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceCleared", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceInitialized", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceTrustedInitialized", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceMigrated", true));
int initial_unchanged_count =
GetTrackedPrefHistogramCount("Settings.TrackedPreferenceUnchanged",
false);
EXPECT_GT(initial_unchanged_count, 0);
if (is_unloaded_profile_seeding_allowed_) {
profile_manager->GetProfile(GetUnloadedProfilePath());
ASSERT_EQ(2U, profile_manager->GetLoadedProfiles().size());
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceChanged", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceCleared", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceInitialized", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceTrustedInitialized", true));
EXPECT_EQ(
0, GetTrackedPrefHistogramCount(
"Settings.TrackedPreferenceMigrated", true));
EXPECT_EQ(
initial_unchanged_count * 2,
GetTrackedPrefHistogramCount("Settings.TrackedPreferenceUnchanged",
false));
}
}
INSTANTIATE_TEST_CASE_P(
PrefHashBrowserTestInstance,
PrefHashBrowserTest,
testing::Values(
chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement,
chrome_prefs::internals::kSettingsEnforcementGroupEnforceOnload,
chrome_prefs::internals::kSettingsEnforcementGroupEnforceAlways,
chrome_prefs::internals::
kSettingsEnforcementGroupEnforceAlwaysWithExtensions));