Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / login_manager_test.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/login/login_manager_test.h"
6
7 #include "base/prefs/scoped_user_pref_update.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/chromeos/login/existing_user_controller.h"
11 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
12 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
13 #include "chromeos/chromeos_switches.h"
14 #include "chromeos/login/auth/key.h"
15 #include "chromeos/login/auth/user_context.h"
16 #include "components/user_manager/user.h"
17 #include "components/user_manager/user_manager.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/test/browser_test_utils.h"
21 #include "content/public/test/test_utils.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace chromeos {
25
26 LoginManagerTest::LoginManagerTest(bool should_launch_browser)
27     : should_launch_browser_(should_launch_browser),
28       web_contents_(NULL) {
29   set_exit_when_last_browser_closes(false);
30 }
31
32 void LoginManagerTest::TearDownOnMainThread() {
33   MixinBasedBrowserTest::TearDownOnMainThread();
34   if (LoginDisplayHostImpl::default_host())
35     LoginDisplayHostImpl::default_host()->Finalize();
36   base::MessageLoop::current()->RunUntilIdle();
37 }
38
39 void LoginManagerTest::SetUpCommandLine(CommandLine* command_line) {
40   command_line->AppendSwitch(chromeos::switches::kLoginManager);
41   command_line->AppendSwitch(chromeos::switches::kForceLoginManagerInTests);
42   MixinBasedBrowserTest::SetUpCommandLine(command_line);
43 }
44
45 void LoginManagerTest::SetUpInProcessBrowserTestFixture() {
46   MixinBasedBrowserTest::SetUpInProcessBrowserTestFixture();
47   mock_login_utils_ = new testing::NiceMock<MockLoginUtils>();
48   mock_login_utils_->DelegateToFake();
49   mock_login_utils_->GetFakeLoginUtils()->set_should_launch_browser(
50       should_launch_browser_);
51   LoginUtils::Set(mock_login_utils_);
52 }
53
54 void LoginManagerTest::SetUpOnMainThread() {
55   MixinBasedBrowserTest::SetUpOnMainThread();
56   content::WindowedNotificationObserver(
57       chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
58       content::NotificationService::AllSources()).Wait();
59   InitializeWebContents();
60 }
61
62 void LoginManagerTest::RegisterUser(const std::string& user_id) {
63   ListPrefUpdate users_pref(g_browser_process->local_state(), "LoggedInUsers");
64   users_pref->AppendIfNotPresent(new base::StringValue(user_id));
65 }
66
67 void LoginManagerTest::SetExpectedCredentials(const UserContext& user_context) {
68   login_utils().GetFakeLoginUtils()->SetExpectedCredentials(user_context);
69 }
70
71 bool LoginManagerTest::TryToLogin(const UserContext& user_context) {
72   if (!AddUserToSession(user_context))
73     return false;
74   if (const user_manager::User* active_user =
75           user_manager::UserManager::Get()->GetActiveUser())
76     return active_user->email() == user_context.GetUserID();
77   return false;
78 }
79
80 bool LoginManagerTest::AddUserToSession(const UserContext& user_context) {
81   ExistingUserController* controller =
82       ExistingUserController::current_controller();
83   if (!controller) {
84     ADD_FAILURE();
85     return false;
86   }
87   content::WindowedNotificationObserver observer(
88       chrome::NOTIFICATION_SESSION_STARTED,
89       content::NotificationService::AllSources());
90   controller->Login(user_context, SigninSpecifics());
91   observer.Wait();
92   const user_manager::UserList& logged_users =
93       user_manager::UserManager::Get()->GetLoggedInUsers();
94   for (user_manager::UserList::const_iterator it = logged_users.begin();
95        it != logged_users.end();
96        ++it) {
97     if ((*it)->email() == user_context.GetUserID())
98       return true;
99   }
100   return false;
101 }
102
103 void LoginManagerTest::LoginUser(const std::string& user_id) {
104   UserContext user_context(user_id);
105   user_context.SetKey(Key("password"));
106   SetExpectedCredentials(user_context);
107   EXPECT_TRUE(TryToLogin(user_context));
108 }
109
110 void LoginManagerTest::AddUser(const std::string& user_id) {
111   UserContext user_context(user_id);
112   user_context.SetKey(Key("password"));
113   SetExpectedCredentials(user_context);
114   EXPECT_TRUE(AddUserToSession(user_context));
115 }
116
117 void LoginManagerTest::JSExpect(const std::string& expression) {
118   js_checker_.ExpectTrue(expression);
119 }
120
121 void LoginManagerTest::InitializeWebContents() {
122   LoginDisplayHost* host = LoginDisplayHostImpl::default_host();
123   EXPECT_TRUE(host != NULL);
124
125   content::WebContents* web_contents =
126       host->GetWebUILoginView()->GetWebContents();
127   EXPECT_TRUE(web_contents != NULL);
128   set_web_contents(web_contents);
129   js_checker_.set_web_contents(web_contents);
130 }
131
132 }  // namespace chromeos