Chromium Code Reviews| Index: chrome/browser/chromeos/power/power_prefs_unittest.cc |
| diff --git a/chrome/browser/chromeos/power/power_prefs_unittest.cc b/chrome/browser/chromeos/power/power_prefs_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..57c93dc394928ece4930d9e11223c9b2c7dc481c |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/power/power_prefs_unittest.cc |
| @@ -0,0 +1,229 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/power/power_prefs.h" |
| + |
| +#include <string> |
| + |
| +#include "base/command_line.h" |
| +#include "base/files/file_path.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/prefs/pref_change_registrar.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "chrome/browser/extensions/extension_special_storage_policy.h" |
| +#include "chrome/browser/prefs/browser_prefs.h" |
| +#include "chrome/browser/prefs/pref_service_syncable.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/common/chrome_constants.h" |
| +#include "chrome/common/chrome_notification_types.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 "chromeos/chromeos_switches.h" |
| +#include "chromeos/dbus/fake_power_manager_client.h" |
| +#include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h" |
| +#include "chromeos/dbus/power_manager/policy.pb.h" |
| +#include "chromeos/dbus/power_policy_controller.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 chromeos { |
| + |
| +class PowerPrefsTest : public testing::Test { |
| + protected: |
| + PowerPrefsTest(); |
| + |
| + // testing::Test: |
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + void VerifyProfileIsBeingUsed(Profile* profile); |
| + void VerifyNoProfileIsBeingUsed(); |
| + |
| + TestingProfileManager profile_manager_; |
| + MockDBusThreadManagerWithoutGMock mock_dbus_thread_manager_; |
| + PowerPolicyController* power_policy_controller_; // Not owned. |
| + FakePowerManagerClient* fake_power_manager_client_; // Not owned. |
| + |
| + scoped_ptr<PowerPrefs> power_prefs_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(PowerPrefsTest); |
| +}; |
| + |
| +PowerPrefsTest::PowerPrefsTest() |
| + : profile_manager_(TestingBrowserProcess::GetGlobal()), |
| + power_policy_controller_( |
| + mock_dbus_thread_manager_.GetPowerPolicyController()), |
| + fake_power_manager_client_( |
| + mock_dbus_thread_manager_.fake_power_manager_client()) { |
| +} |
| + |
| +void PowerPrefsTest::SetUp() { |
| + testing::Test::SetUp(); |
| + ASSERT_TRUE(profile_manager_.SetUp()); |
| + |
| + power_prefs_.reset(new PowerPrefs(power_policy_controller_)); |
| + VerifyNoProfileIsBeingUsed(); |
| +} |
| + |
| +void PowerPrefsTest::TearDown() { |
| + power_prefs_.reset(); |
| + testing::Test::TearDown(); |
| +} |
| + |
| +void PowerPrefsTest::VerifyProfileIsBeingUsed(Profile* profile) { |
|
Daniel Erat
2013/06/28 17:05:09
to make test failures have meaningful line numbers
bartfab (slow)
2013/07/01 12:32:59
Done.
|
| + EXPECT_EQ(profile, power_prefs_->profile_); |
| + const PrefService* prefs = profile->GetPrefs(); |
| + ASSERT_TRUE(prefs); |
| + ASSERT_TRUE(power_prefs_->pref_change_registrar_); |
| + EXPECT_EQ(prefs, power_prefs_->pref_change_registrar_->prefs()); |
| + |
| + power_manager::PowerManagementPolicy expected_policy; |
| + expected_policy.mutable_ac_delays()->set_screen_dim_ms( |
| + prefs->GetInteger(prefs::kPowerAcScreenDimDelayMs)); |
| + expected_policy.mutable_ac_delays()->set_screen_off_ms( |
| + prefs->GetInteger(prefs::kPowerAcScreenOffDelayMs)); |
| + expected_policy.mutable_ac_delays()->set_screen_lock_ms( |
| + prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs)); |
| + expected_policy.mutable_ac_delays()->set_idle_warning_ms( |
| + prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs)); |
| + expected_policy.mutable_ac_delays()->set_idle_ms( |
| + prefs->GetInteger(prefs::kPowerAcIdleDelayMs)); |
| + expected_policy.mutable_battery_delays()->set_screen_dim_ms( |
| + prefs->GetInteger(prefs::kPowerBatteryScreenDimDelayMs)); |
| + expected_policy.mutable_battery_delays()->set_screen_off_ms( |
| + prefs->GetInteger(prefs::kPowerBatteryScreenOffDelayMs)); |
| + expected_policy.mutable_battery_delays()->set_screen_lock_ms( |
| + prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs)); |
| + expected_policy.mutable_battery_delays()->set_idle_warning_ms( |
| + prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs)); |
| + expected_policy.mutable_battery_delays()->set_idle_ms( |
| + prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs)); |
| + expected_policy.set_ac_idle_action( |
| + static_cast<power_manager::PowerManagementPolicy_Action>( |
| + prefs->GetInteger(prefs::kPowerAcIdleAction))); |
| + expected_policy.set_battery_idle_action( |
| + static_cast<power_manager::PowerManagementPolicy_Action>( |
| + prefs->GetInteger(prefs::kPowerBatteryIdleAction))); |
| + expected_policy.set_lid_closed_action( |
| + static_cast<power_manager::PowerManagementPolicy_Action>( |
| + prefs->GetInteger(prefs::kPowerLidClosedAction))); |
| + expected_policy.set_use_audio_activity( |
| + prefs->GetBoolean(prefs::kPowerUseAudioActivity)); |
| + expected_policy.set_use_video_activity( |
| + prefs->GetBoolean(prefs::kPowerUseVideoActivity)); |
| + expected_policy.set_presentation_screen_dim_delay_factor( |
| + prefs->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor)); |
| + expected_policy.set_user_activity_screen_dim_delay_factor( |
| + prefs->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor)); |
| + expected_policy.set_reason("Prefs"); |
| + EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| + PowerPolicyController::GetPolicyDebugString( |
| + fake_power_manager_client_->get_policy())); |
| + EXPECT_EQ(prefs->GetBoolean(prefs::kPowerAllowScreenWakeLocks), |
| + power_policy_controller_->honor_screen_wake_locks_); |
| +} |
| + |
| +void PowerPrefsTest::VerifyNoProfileIsBeingUsed() { |
| + EXPECT_FALSE(power_prefs_->profile_); |
| + EXPECT_FALSE(power_prefs_->pref_change_registrar_); |
| + |
| + power_manager::PowerManagementPolicy empty_policy; |
| + EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(empty_policy), |
| + PowerPolicyController::GetPolicyDebugString( |
| + fake_power_manager_client_->get_policy())); |
| +} |
| + |
| +TEST_F(PowerPrefsTest, LoginScreen) { |
| + // Set up login profile. |
| + scoped_ptr<TestingPrefServiceSyncable> login_profile_prefs( |
| + new TestingPrefServiceSyncable); |
| + chrome::RegisterUserPrefs(login_profile_prefs->registry(), true); |
| + scoped_ptr<TestingProfile> login_profile_owner(new TestingProfile( |
| + profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile), |
| + NULL, |
| + scoped_refptr<ExtensionSpecialStoragePolicy>(), |
| + scoped_ptr<PrefServiceSyncable>(login_profile_prefs.release()))); |
| + TestingProfile* login_profile = login_profile_owner.get(); |
| + TestingProfile* login_profile_parent = profile_manager_.CreateTestingProfile( |
| + chrome::kInitialProfile); |
| + login_profile_parent->SetOffTheRecordProfile(login_profile_owner.release()); |
| + login_profile->SetOriginalProfile(login_profile_parent); |
| + login_profile->set_incognito(true); |
| + |
| + // Inform power_prefs_ that the login screen is being shown. |
| + power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
| + content::Source<PowerPrefsTest>(this), |
| + content::NotificationService::NoDetails()); |
| + |
| + // Verify that the login profile's power prefs are being used. |
|
Daniel Erat
2013/06/28 17:05:09
i'd probably delete the rest of the comments in th
bartfab (slow)
2013/07/01 12:32:59
I snipped some of the comments. I think the bit ab
|
| + VerifyProfileIsBeingUsed(login_profile); |
| + |
| + // Set up another profile. |
| + TestingProfile* other_profile = |
| + profile_manager_.CreateTestingProfile("other"); |
| + |
| + // Inform power_prefs_ that the other profile has been destroyed. |
| + power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| + content::Source<Profile>(other_profile), |
| + content::NotificationService::NoDetails()); |
| + |
| + // Verify that the login profile's power prefs are still being used. |
| + VerifyProfileIsBeingUsed(login_profile); |
| + |
| + // Inform power_prefs_ that the login profile has been destroyed. |
| + power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| + content::Source<Profile>(login_profile), |
| + content::NotificationService::NoDetails()); |
| + |
| + // Verify that no profile's power prefs are being used. |
| + VerifyNoProfileIsBeingUsed(); |
| +} |
| + |
| + |
| +TEST_F(PowerPrefsTest, UserSession) { |
| + // Set up user profile. |
| + TestingProfile* user_profile = profile_manager_.CreateTestingProfile("user"); |
| + CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kLoginProfile, |
| + "user"); |
| + profile_manager_.SetLoggedIn(true); |
| + ProfileManager::AllowGetDefaultProfile(); |
| + |
| + // Inform power_prefs_ that a session has started. |
| + power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED, |
| + content::Source<PowerPrefsTest>(this), |
| + content::NotificationService::NoDetails()); |
| + |
| + // Verify that the user profile's prefs are being used. |
| + VerifyProfileIsBeingUsed(user_profile); |
| + |
| + // Set up another profile. |
| + TestingProfile* other_profile = |
| + profile_manager_.CreateTestingProfile("other"); |
| + |
| + // Inform power_prefs_ that the other profile has been destroyed. |
| + power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| + content::Source<Profile>(other_profile), |
| + content::NotificationService::NoDetails()); |
| + |
| + // Verify that the user profile's power prefs are still being used. |
| + VerifyProfileIsBeingUsed(user_profile); |
| + |
| + // Inform power_prefs_ that the session has ended and the user profile has |
| + // been destroyed. |
| + power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| + content::Source<Profile>(user_profile), |
| + content::NotificationService::NoDetails()); |
| + |
| + // Verify that no profile's power prefs are being used. |
| + VerifyNoProfileIsBeingUsed(); |
| +} |
| + |
| +} // namespace chromeos |