This source file includes following definitions.
- profile_manager_
- SetUp
- RegisterUserProfilePrefs
- RegisterLoginProfilePrefs
- SetRecommendedValues
- SetUserSettings
- CreateLoginProfile
- CreateUserProfile
- NotifyOfSessionStart
- NotifyOfUserActivity
- VerifyPrefFollowsUser
- VerifyPrefsFollowUser
- VerifyPrefFollowsRecommendation
- VerifyPrefsFollowRecommendations
- VerifyNotListeningForNotifications
- VerifyTimerIsStopped
- VerifyTimerIsRunning
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/chromeos/policy/recommendation_restorer.h"
#include "ash/magnifier/magnifier_constants.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_notifier_impl.h"
#include "base/prefs/testing_pref_store.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_simple_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/policy/recommendation_restorer_factory.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
namespace {
const int kRestoreDelayInMs = 60 * 1000;
}
class RecommendationRestorerTest : public testing::Test {
protected:
RecommendationRestorerTest();
virtual void SetUp() OVERRIDE;
void RegisterUserProfilePrefs();
void RegisterLoginProfilePrefs();
void SetRecommendedValues();
void SetUserSettings();
void CreateLoginProfile();
void CreateUserProfile();
void NotifyOfSessionStart();
void NotifyOfUserActivity();
void VerifyPrefFollowsUser(const char* pref_name,
const base::Value& expected_value) const;
void VerifyPrefsFollowUser() const;
void VerifyPrefFollowsRecommendation(const char* pref_name,
const base::Value& expected_value) const;
void VerifyPrefsFollowRecommendations() const;
void VerifyNotListeningForNotifications() const;
void VerifyTimerIsStopped() const;
void VerifyTimerIsRunning() const;
TestingPrefStore* recommended_prefs_;
TestingPrefServiceSyncable* prefs_;
RecommendationRestorer* restorer_;
scoped_refptr<base::TestSimpleTaskRunner> runner_;
base::ThreadTaskRunnerHandle runner_handler_;
private:
scoped_ptr<PrefServiceSyncable> prefs_owner_;
TestingProfileManager profile_manager_;
DISALLOW_COPY_AND_ASSIGN(RecommendationRestorerTest);
};
RecommendationRestorerTest::RecommendationRestorerTest()
: recommended_prefs_(new TestingPrefStore),
prefs_(new TestingPrefServiceSyncable(
new TestingPrefStore,
new TestingPrefStore,
recommended_prefs_,
new user_prefs::PrefRegistrySyncable,
new PrefNotifierImpl)),
restorer_(NULL),
runner_(new base::TestSimpleTaskRunner),
runner_handler_(runner_),
prefs_owner_(prefs_),
profile_manager_(TestingBrowserProcess::GetGlobal()) {
}
void RecommendationRestorerTest::SetUp() {
testing::Test::SetUp();
ASSERT_TRUE(profile_manager_.SetUp());
}
void RecommendationRestorerTest::RegisterUserProfilePrefs() {
chrome::RegisterUserProfilePrefs(prefs_->registry());
}
void RecommendationRestorerTest::RegisterLoginProfilePrefs() {
chrome::RegisterLoginProfilePrefs(prefs_->registry());
}
void RecommendationRestorerTest::SetRecommendedValues() {
recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
recommended_prefs_->SetBoolean(prefs::kVirtualKeyboardEnabled, false);
}
void RecommendationRestorerTest::SetUserSettings() {
prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
prefs_->SetBoolean(prefs::kVirtualKeyboardEnabled, true);
}
void RecommendationRestorerTest::CreateLoginProfile() {
ASSERT_FALSE(restorer_);
TestingProfile* profile = profile_manager_.CreateTestingProfile(
chrome::kInitialProfile, prefs_owner_.Pass(),
base::UTF8ToUTF16(chrome::kInitialProfile), 0, std::string(),
TestingProfile::TestingFactories());
restorer_ = RecommendationRestorerFactory::GetForProfile(profile);
EXPECT_TRUE(restorer_);
}
void RecommendationRestorerTest::CreateUserProfile() {
ASSERT_FALSE(restorer_);
TestingProfile* profile = profile_manager_.CreateTestingProfile(
"user", prefs_owner_.Pass(), base::UTF8ToUTF16("user"), 0, std::string(),
TestingProfile::TestingFactories());
restorer_ = RecommendationRestorerFactory::GetForProfile(profile);
EXPECT_TRUE(restorer_);
}
void RecommendationRestorerTest::NotifyOfSessionStart() {
ASSERT_TRUE(restorer_);
restorer_->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
content::Source<RecommendationRestorerTest>(this),
content::NotificationService::NoDetails());
}
void RecommendationRestorerTest::NotifyOfUserActivity() {
ASSERT_TRUE(restorer_);
restorer_->OnUserActivity(NULL);
}
void RecommendationRestorerTest::VerifyPrefFollowsUser(
const char* pref_name,
const base::Value& expected_value) const {
const PrefServiceSyncable::Preference* pref =
prefs_->FindPreference(pref_name);
ASSERT_TRUE(pref);
EXPECT_TRUE(pref->HasUserSetting());
const base::Value* value = pref->GetValue();
ASSERT_TRUE(value);
EXPECT_TRUE(expected_value.Equals(value));
}
void RecommendationRestorerTest::VerifyPrefsFollowUser() const {
VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
base::FundamentalValue(ash::MAGNIFIER_FULL));
VerifyPrefFollowsUser(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(true));
}
void RecommendationRestorerTest::VerifyPrefFollowsRecommendation(
const char* pref_name,
const base::Value& expected_value) const {
const PrefServiceSyncable::Preference* pref =
prefs_->FindPreference(pref_name);
ASSERT_TRUE(pref);
EXPECT_TRUE(pref->IsRecommended());
EXPECT_FALSE(pref->HasUserSetting());
const base::Value* value = pref->GetValue();
ASSERT_TRUE(value);
EXPECT_TRUE(expected_value.Equals(value));
}
void RecommendationRestorerTest::VerifyPrefsFollowRecommendations() const {
VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
base::FundamentalValue(0));
VerifyPrefFollowsRecommendation(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(false));
}
void RecommendationRestorerTest::VerifyNotListeningForNotifications() const {
ASSERT_TRUE(restorer_);
EXPECT_TRUE(restorer_->pref_change_registrar_.IsEmpty());
EXPECT_TRUE(restorer_->notification_registrar_.IsEmpty());
}
void RecommendationRestorerTest::VerifyTimerIsStopped() const {
ASSERT_TRUE(restorer_);
EXPECT_FALSE(restorer_->restore_timer_.IsRunning());
}
void RecommendationRestorerTest::VerifyTimerIsRunning() const {
ASSERT_TRUE(restorer_);
EXPECT_TRUE(restorer_->restore_timer_.IsRunning());
EXPECT_EQ(base::TimeDelta::FromMilliseconds(kRestoreDelayInMs),
restorer_->restore_timer_.GetCurrentDelay());
}
TEST_F(RecommendationRestorerTest, CreateForUserProfile) {
RegisterUserProfilePrefs();
SetRecommendedValues();
SetUserSettings();
CreateUserProfile();
VerifyNotListeningForNotifications();
VerifyPrefsFollowUser();
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, NoRecommendations) {
RegisterLoginProfilePrefs();
SetUserSettings();
CreateLoginProfile();
VerifyPrefsFollowUser();
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, RestoreOnStartup) {
RegisterLoginProfilePrefs();
SetRecommendedValues();
SetUserSettings();
CreateLoginProfile();
VerifyPrefsFollowRecommendations();
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, RestoreOnRecommendationChangeOnLoginScreen) {
RegisterLoginProfilePrefs();
SetUserSettings();
CreateLoginProfile();
VerifyTimerIsStopped();
recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
base::FundamentalValue(ash::MAGNIFIER_FULL));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
base::FundamentalValue(0));
VerifyTimerIsStopped();
recommended_prefs_->SetBoolean(prefs::kVirtualKeyboardEnabled, false);
VerifyPrefFollowsUser(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, RestoreOnRecommendationChangeInUserSession) {
RegisterLoginProfilePrefs();
SetUserSettings();
CreateLoginProfile();
NotifyOfSessionStart();
VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
base::FundamentalValue(true));
recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
VerifyTimerIsStopped();
VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(true));
recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
VerifyTimerIsStopped();
VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
base::FundamentalValue(true));
recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
VerifyTimerIsStopped();
VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
base::FundamentalValue(ash::MAGNIFIER_FULL));
recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
VerifyTimerIsStopped();
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
base::FundamentalValue(0));
VerifyPrefFollowsUser(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(true));
recommended_prefs_->SetBoolean(prefs::kVirtualKeyboardEnabled, false);
VerifyTimerIsStopped();
VerifyPrefFollowsRecommendation(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(false));
}
TEST_F(RecommendationRestorerTest, DoNothingOnUserChange) {
RegisterLoginProfilePrefs();
CreateLoginProfile();
prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
base::FundamentalValue(true));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(true));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
base::FundamentalValue(true));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(true));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
base::FundamentalValue(ash::MAGNIFIER_FULL));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kVirtualKeyboardEnabled, true);
VerifyPrefFollowsUser(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(true));
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, RestoreOnUserChange) {
RegisterLoginProfilePrefs();
SetRecommendedValues();
CreateLoginProfile();
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(true));
VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
base::FundamentalValue(ash::MAGNIFIER_FULL));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
base::FundamentalValue(false));
VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
base::FundamentalValue(0));
VerifyTimerIsStopped();
prefs_->SetBoolean(prefs::kVirtualKeyboardEnabled, true);
VerifyPrefFollowsUser(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kVirtualKeyboardEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, RestoreOnSessionStart) {
RegisterLoginProfilePrefs();
SetRecommendedValues();
CreateLoginProfile();
SetUserSettings();
NotifyOfSessionStart();
VerifyPrefsFollowRecommendations();
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, DoNothingOnSessionStart) {
RegisterLoginProfilePrefs();
CreateLoginProfile();
SetUserSettings();
NotifyOfSessionStart();
VerifyPrefsFollowUser();
VerifyTimerIsStopped();
}
TEST_F(RecommendationRestorerTest, UserActivityResetsTimer) {
RegisterLoginProfilePrefs();
recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
CreateLoginProfile();
prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
VerifyTimerIsRunning();
NotifyOfUserActivity();
runner_->RunPendingTasks();
VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
base::FundamentalValue(true));
VerifyTimerIsRunning();
runner_->RunUntilIdle();
VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
base::FundamentalValue(false));
VerifyTimerIsStopped();
}
}