Index: chrome/browser/chromeos/login/existing_user_controller_auto_login_unittest.cc |
diff --git a/chrome/browser/chromeos/login/existing_user_controller_auto_login_unittest.cc b/chrome/browser/chromeos/login/existing_user_controller_auto_login_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..97c188af5b7d563b2e048b492fdb4bd5ab8873be |
--- /dev/null |
+++ b/chrome/browser/chromeos/login/existing_user_controller_auto_login_unittest.cc |
@@ -0,0 +1,248 @@ |
+// Copyright 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 "base/memory/scoped_ptr.h" |
bartfab (slow)
2013/03/07 12:17:23
This is included by "existing_user_controller.h" a
dconnelly
2013/03/07 14:45:19
Done.
|
+#include "base/message_loop.h" |
+#include "chrome/browser/chromeos/login/existing_user_controller.h" |
+#include "chrome/browser/chromeos/login/login_display.h" |
bartfab (slow)
2013/03/07 12:17:23
This is not used.
dconnelly
2013/03/07 14:45:19
Done.
|
+#include "chrome/browser/chromeos/login/login_display_host.h" |
bartfab (slow)
2013/03/07 12:17:23
This is not used.
dconnelly
2013/03/07 14:45:19
Done.
|
+#include "chrome/browser/chromeos/login/login_utils.h" |
bartfab (slow)
2013/03/07 12:17:23
This is included by "existing_user_controller.h" a
dconnelly
2013/03/07 14:45:19
Done.
|
+#include "chrome/browser/chromeos/login/mock_login_display.h" |
+#include "chrome/browser/chromeos/login/mock_login_display_host.h" |
+#include "chrome/browser/chromeos/login/mock_login_utils.h" |
+#include "chrome/browser/chromeos/settings/cros_settings.h" |
+#include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
+#include "chrome/test/base/scoped_testing_local_state.h" |
+#include "chrome/test/base/testing_browser_process.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using testing::AnyNumber; |
bartfab (slow)
2013/03/07 12:17:23
This is not used.
dconnelly
2013/03/07 14:45:19
Done.
|
+using testing::AtLeast; |
bartfab (slow)
2013/03/07 12:17:23
This is not used.
dconnelly
2013/03/07 14:45:19
Done.
|
+using testing::Mock; |
bartfab (slow)
2013/03/07 12:17:23
This is not used.
dconnelly
2013/03/07 14:45:19
Done.
|
+using testing::Return; |
+using testing::ReturnNull; |
bartfab (slow)
2013/03/07 12:17:23
This is not used.
dconnelly
2013/03/07 14:45:19
Done.
|
+using testing::SaveArg; |
bartfab (slow)
2013/03/07 12:17:23
This is not used.
dconnelly
2013/03/07 14:45:19
Done.
|
+using testing::_; |
+ |
+namespace chromeos { |
+ |
+namespace { |
+ |
+const char kAutoLoginUsername[] = "public_session_user@localhost"; |
+const int kAutoLoginNoDelay = 0; |
bartfab (slow)
2013/03/07 12:17:23
This is not used anywhere.
dconnelly
2013/03/07 14:45:19
Done.
|
+const int kAutoLoginShortDelay = 1; |
bartfab (slow)
2013/03/07 12:17:23
Nit: Add an "Ms" suffix to make it clear that this
dconnelly
2013/03/07 14:45:19
Done.
|
+const int kAutoLoginLongDelay = 10000; |
bartfab (slow)
2013/03/07 12:17:23
Nit: Add an "Ms" suffix to make it clear that this
dconnelly
2013/03/07 14:45:19
Done.
|
+ |
+} // namespace |
+ |
+class ExistingUserControllerAutoLoginTest : public ::testing::Test { |
+ protected: |
+ ExistingUserControllerAutoLoginTest() |
+ : message_loop_(MessageLoop::TYPE_UI), |
+ ui_thread_(content::BrowserThread::UI, &message_loop_), |
+ local_state_(TestingBrowserProcess::GetGlobal()) { |
+ } |
+ |
+ virtual void SetUp() { |
+ mock_login_display_host_.reset(new MockLoginDisplayHost); |
+ mock_login_display_ = new MockLoginDisplay(); |
+ mock_login_utils_ = new MockLoginUtils(); |
+ LoginUtils::Set(mock_login_utils_); |
+ |
+ EXPECT_CALL(*mock_login_display_host_.get(), CreateLoginDisplay(_)) |
+ .Times(1) |
+ .WillOnce(Return(mock_login_display_)); |
+ |
+ existing_user_controller_.reset( |
+ new ExistingUserController(mock_login_display_host_.get())); |
+ |
+ CrosSettings::Get()->RemoveSettingsObserver( |
bartfab (slow)
2013/03/07 12:17:23
Why do you not want the ExistingUserController to
dconnelly
2013/03/07 14:45:19
Done.
|
+ kAccountsPrefDeviceLocalAccountAutoLoginId, |
bartfab (slow)
2013/03/07 12:17:23
#include "chrome/browser/chromeos/settings/cros_se
dconnelly
2013/03/07 14:45:19
Done.
|
+ existing_user_controller()); |
+ CrosSettings::Get()->RemoveSettingsObserver( |
+ kAccountsPrefDeviceLocalAccountAutoLoginDelay, |
+ existing_user_controller()); |
+ } |
+ |
+ ExistingUserController* existing_user_controller() { |
+ return ExistingUserController::current_controller(); |
+ } |
+ |
+ void SetAutoLoginSettings(const std::string& username, int delay) { |
+ CrosSettings::Get()->SetString( |
+ kAccountsPrefDeviceLocalAccountAutoLoginId, |
+ username); |
+ CrosSettings::Get()->SetInteger( |
+ kAccountsPrefDeviceLocalAccountAutoLoginDelay, |
+ delay); |
+ } |
+ |
+ // ExistingUserController private member accessors. |
+ base::OneShotTimer<ExistingUserController>* auto_login_timer() { |
+ return existing_user_controller()->auto_login_timer_.get(); |
+ } |
+ |
+ std::string auto_login_username() { |
bartfab (slow)
2013/03/07 12:17:23
Nit: const
Nit: Return const std::string&
dconnelly
2013/03/07 14:45:19
Done.
|
+ return existing_user_controller()->public_session_auto_login_username_; |
+ } |
+ void set_auto_login_username(const std::string& username) { |
+ existing_user_controller()->public_session_auto_login_username_ = username; |
+ } |
+ |
+ int auto_login_delay() { |
bartfab (slow)
2013/03/07 12:17:23
Nit: const
dconnelly
2013/03/07 14:45:19
Done.
|
+ return existing_user_controller()->public_session_auto_login_delay_; |
+ } |
+ void set_auto_login_delay(int delay) { |
+ existing_user_controller()->public_session_auto_login_delay_ = delay; |
+ } |
+ |
+ bool is_login_in_progress() { |
bartfab (slow)
2013/03/07 12:17:23
Nit: const
dconnelly
2013/03/07 14:45:19
Done.
|
+ return existing_user_controller()->is_login_in_progress_; |
+ } |
+ void set_is_login_in_progress(bool is_login_in_progress) { |
+ existing_user_controller()->is_login_in_progress_ = is_login_in_progress; |
+ } |
+ |
+ void ConfigureAutoLogin() { |
+ existing_user_controller()->ConfigurePublicSessionAutoLogin(); |
+ } |
+ |
+ private: |
+ // Owned by LoginUtilsWrapper |
bartfab (slow)
2013/03/07 12:17:23
Nit: Period at the end of the line.
dconnelly
2013/03/07 14:45:19
Done.
|
+ MockLoginUtils* mock_login_utils_; |
+ |
+ // |mock_login_display_| is owned by the ExistingUserController, which calls |
+ // CreateLoginDisplay() on the |mock_login_display_host_| to get it. |
+ MockLoginDisplay* mock_login_display_; |
+ |
+ scoped_ptr<MockLoginDisplayHost> mock_login_display_host_; |
+ scoped_ptr<ExistingUserController> existing_user_controller_; |
+ MessageLoop message_loop_; |
+ content::TestBrowserThread ui_thread_; |
+ ScopedTestingLocalState local_state_; |
+ ScopedDeviceSettingsTestHelper device_settings_test_helper_; |
+}; |
+ |
+TEST_F(ExistingUserControllerAutoLoginTest, StartAutoLoginTimer) { |
+ // Timer shouldn't start until signin screen is ready. |
+ set_auto_login_username(kAutoLoginUsername); |
+ set_auto_login_delay(kAutoLoginLongDelay); |
+ existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
+ EXPECT_FALSE(auto_login_timer()); |
+ |
+ // Timer shouldn't start if the policy isn't set. |
+ set_auto_login_username(""); |
+ existing_user_controller()->OnSigninScreenReady(); |
+ existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
+ EXPECT_FALSE(auto_login_timer()); |
+ |
+ // Timer shouldn't fire in the middle of a login attempt. |
+ set_auto_login_username(kAutoLoginUsername); |
+ set_is_login_in_progress(true); |
+ existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
+ EXPECT_FALSE(auto_login_timer()); |
+ |
+ // Otherwise start. |
+ set_is_login_in_progress(false); |
+ existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_TRUE(auto_login_timer()->IsRunning()); |
+ EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
+ kAutoLoginLongDelay); |
+} |
+ |
+TEST_F(ExistingUserControllerAutoLoginTest, StopAutoLoginTimer) { |
+ existing_user_controller()->OnSigninScreenReady(); |
+ set_auto_login_username(kAutoLoginUsername); |
+ set_auto_login_delay(kAutoLoginLongDelay); |
+ |
+ existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_TRUE(auto_login_timer()->IsRunning()); |
+ |
+ existing_user_controller()->StopPublicSessionAutoLoginTimer(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_FALSE(auto_login_timer()->IsRunning()); |
+} |
+ |
+TEST_F(ExistingUserControllerAutoLoginTest, ResetAutoLoginTimer) { |
+ existing_user_controller()->OnSigninScreenReady(); |
+ set_auto_login_username(kAutoLoginUsername); |
+ |
+ // Timer starts off not running. |
+ EXPECT_FALSE(auto_login_timer()); |
+ |
+ // When the timer isn't running, nothing should happen. |
+ existing_user_controller()->ResetPublicSessionAutoLoginTimer(); |
+ EXPECT_FALSE(auto_login_timer()); |
+ |
+ // Start the timer. |
+ set_auto_login_delay(kAutoLoginLongDelay); |
+ existing_user_controller()->StartPublicSessionAutoLoginTimer(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_TRUE(auto_login_timer()->IsRunning()); |
+ EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
+ kAutoLoginLongDelay); |
+ |
+ // User activity should restart the timer, so check to see that the |
+ // timer delay was modified. |
+ set_auto_login_delay(kAutoLoginShortDelay); |
+ existing_user_controller()->ResetPublicSessionAutoLoginTimer(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_TRUE(auto_login_timer()->IsRunning()); |
+ EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
+ kAutoLoginShortDelay); |
+} |
+ |
+TEST_F(ExistingUserControllerAutoLoginTest, ConfigureAutoLogin) { |
+ existing_user_controller()->OnSigninScreenReady(); |
+ |
+ // Timer shouldn't start when the policy is disabled. |
bartfab (slow)
2013/03/07 12:17:23
You are not really disabling the policy here, you
dconnelly
2013/03/07 14:45:19
Yes. This is redundant. It seemed more clear to do
|
+ SetAutoLoginSettings("", 0); |
bartfab (slow)
2013/03/07 12:17:23
Nit: Use kAutoLoginNoDelay
dconnelly
2013/03/07 14:45:19
Done.
|
+ ConfigureAutoLogin(); |
+ EXPECT_FALSE(auto_login_timer()); |
+ EXPECT_EQ(auto_login_delay(), 0); |
bartfab (slow)
2013/03/07 12:17:23
Nit: Use kAutoLoginNoDelay
dconnelly
2013/03/07 14:45:19
Done.
|
+ EXPECT_EQ(auto_login_username(), ""); |
+ |
+ // Timer shouldn't start when the delay alone is set. |
+ SetAutoLoginSettings("", kAutoLoginShortDelay); |
+ ConfigureAutoLogin(); |
+ EXPECT_FALSE(auto_login_timer()); |
+ EXPECT_EQ(auto_login_delay(), kAutoLoginShortDelay); |
+ EXPECT_EQ(auto_login_username(), ""); |
+ |
+ // Timer should start when the username is set. |
+ SetAutoLoginSettings(kAutoLoginUsername, kAutoLoginShortDelay); |
+ ConfigureAutoLogin(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_TRUE(auto_login_timer()->IsRunning()); |
+ EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
+ kAutoLoginShortDelay); |
+ EXPECT_EQ(auto_login_delay(), kAutoLoginShortDelay); |
+ EXPECT_EQ(auto_login_username(), kAutoLoginUsername); |
+ |
+ // Timer should restart when the delay is changed. |
+ SetAutoLoginSettings(kAutoLoginUsername, kAutoLoginLongDelay); |
+ ConfigureAutoLogin(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_TRUE(auto_login_timer()->IsRunning()); |
+ EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
+ kAutoLoginLongDelay); |
+ EXPECT_EQ(auto_login_delay(), kAutoLoginLongDelay); |
+ EXPECT_EQ(auto_login_username(), kAutoLoginUsername); |
+ |
+ // Timer should stop when the username is unset. |
+ SetAutoLoginSettings("", kAutoLoginLongDelay); |
+ ConfigureAutoLogin(); |
+ ASSERT_TRUE(auto_login_timer()); |
+ EXPECT_FALSE(auto_login_timer()->IsRunning()); |
+ EXPECT_EQ(auto_login_timer()->GetCurrentDelay().InMilliseconds(), |
+ kAutoLoginLongDelay); |
+ EXPECT_EQ(auto_login_username(), ""); |
+ EXPECT_EQ(auto_login_delay(), kAutoLoginLongDelay); |
+} |
+ |
+} // namespace chromeos |