OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/login/login_utils.h" | 5 #include "chrome/browser/chromeos/login/login_utils.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "chrome/browser/policy/browser_policy_connector.h" | 23 #include "chrome/browser/policy/browser_policy_connector.h" |
24 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 24 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
25 #include "chrome/browser/profiles/profile_manager.h" | 25 #include "chrome/browser/profiles/profile_manager.h" |
26 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
27 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/net/gaia/gaia_urls.h" | 28 #include "chrome/common/net/gaia/gaia_urls.h" |
29 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
30 #include "chrome/common/net/gaia/gaia_auth_consumer.h" | 30 #include "chrome/common/net/gaia/gaia_auth_consumer.h" |
31 #include "chrome/test/base/testing_browser_process.h" | 31 #include "chrome/test/base/testing_browser_process.h" |
32 #include "chrome/test/base/testing_pref_service.h" | 32 #include "chrome/test/base/testing_pref_service.h" |
33 #include "content/public/browser/browser_thread.h" | |
34 #include "content/public/common/url_fetcher_delegate.h" | 33 #include "content/public/common/url_fetcher_delegate.h" |
35 #include "content/test/test_browser_thread.h" | 34 #include "content/test/test_browser_thread.h" |
36 #include "content/test/test_url_fetcher_factory.h" | 35 #include "content/test/test_url_fetcher_factory.h" |
37 #include "net/url_request/url_request.h" | 36 #include "net/url_request/url_request.h" |
38 #include "net/url_request/url_request_status.h" | 37 #include "net/url_request/url_request_status.h" |
39 #include "testing/gmock/include/gmock/gmock.h" | 38 #include "testing/gmock/include/gmock/gmock.h" |
40 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
41 | 40 |
42 namespace chromeos { | 41 namespace chromeos { |
43 | 42 |
44 namespace { | 43 namespace { |
45 | 44 |
46 namespace em = enterprise_management; | 45 namespace em = enterprise_management; |
47 | 46 |
48 using ::testing::DoAll; | 47 using ::testing::DoAll; |
49 using ::testing::Return; | 48 using ::testing::Return; |
50 using ::testing::SetArgPointee; | 49 using ::testing::SetArgPointee; |
51 using ::testing::_; | 50 using ::testing::_; |
52 using content::BrowserThread; | |
53 | 51 |
54 const char kTrue[] = "true"; | 52 const char kTrue[] = "true"; |
55 const char kDomain[] = "domain.com"; | 53 const char kDomain[] = "domain.com"; |
56 const char kUsername[] = "user@domain.com"; | 54 const char kUsername[] = "user@domain.com"; |
57 const char kUsernameOtherDomain[] = "user@other.com"; | 55 const char kUsernameOtherDomain[] = "user@other.com"; |
58 const char kAttributeOwned[] = "enterprise.owned"; | 56 const char kAttributeOwned[] = "enterprise.owned"; |
59 const char kAttributeOwner[] = "enterprise.user"; | 57 const char kAttributeOwner[] = "enterprise.user"; |
60 | 58 |
61 const char kOAuthTokenCookie[] = "oauth_token=1234"; | 59 const char kOAuthTokenCookie[] = "oauth_token=1234"; |
62 const char kOAuthGetAccessTokenData[] = | 60 const char kOAuthGetAccessTokenData[] = |
63 "oauth_token=1234&oauth_token_secret=1234"; | 61 "oauth_token=1234&oauth_token_secret=1234"; |
64 const char kOAuthServiceTokenData[] = | 62 const char kOAuthServiceTokenData[] = |
65 "wrap_access_token=1234&wrap_access_token_expires_in=123456789"; | 63 "wrap_access_token=1234&wrap_access_token_expires_in=123456789"; |
66 | 64 |
67 const char kDMServer[] = "http://server/device_management"; | 65 const char kDMServer[] = "http://server/device_management"; |
68 const char kDMRegisterRequest[] = | 66 const char kDMRegisterRequest[] = |
69 "http://server/device_management?request=register"; | 67 "http://server/device_management?request=register"; |
70 const char kDMPolicyRequest[] = | 68 const char kDMPolicyRequest[] = |
71 "http://server/device_management?request=policy"; | 69 "http://server/device_management?request=policy"; |
72 | 70 |
73 const char kDMToken[] = "1234"; | 71 const char kDMToken[] = "1234"; |
74 | 72 |
75 ACTION_P(MockSessionManagerClientPolicyCallback, policy) { | 73 ACTION_P(MockSessionManagerClientPolicyCallback, policy) { |
76 arg0.Run(policy); | 74 arg0.Run(policy); |
77 } | 75 } |
78 | 76 |
| 77 // Subclass IOThread to expose set_message_loop. |
| 78 class TestIOThread : public IOThread { |
| 79 public: |
| 80 explicit TestIOThread(PrefService* local_state) |
| 81 : IOThread(local_state, NULL, NULL) {} |
| 82 |
| 83 using IOThread::set_message_loop; |
| 84 }; |
| 85 |
79 template<typename TESTBASE> | 86 template<typename TESTBASE> |
80 class LoginUtilsTestBase : public TESTBASE, | 87 class LoginUtilsTestBase : public TESTBASE, |
81 public LoginUtils::Delegate, | 88 public LoginUtils::Delegate, |
82 public LoginStatusConsumer { | 89 public LoginStatusConsumer { |
83 public: | 90 public: |
84 // Initialization here is important. The UI thread gets the test's | |
85 // message loop, as does the file thread (which never actually gets | |
86 // started - so this is a way to fake multiple threads on a single | |
87 // test thread). The IO thread does not get the message loop set, | |
88 // and is never started. This is necessary so that we skip various | |
89 // bits of initialization that get posted to the IO thread. We do | |
90 // however, at one point in the test, temporarily set the message | |
91 // loop for the IO thread. | |
92 LoginUtilsTestBase() | 91 LoginUtilsTestBase() |
93 : loop_(MessageLoop::TYPE_IO), | 92 : loop_(MessageLoop::TYPE_IO), |
94 browser_process_( | 93 browser_process_( |
95 static_cast<TestingBrowserProcess*>(g_browser_process)), | 94 static_cast<TestingBrowserProcess*>(g_browser_process)), |
96 local_state_(browser_process_), | 95 local_state_(browser_process_), |
97 ui_thread_(content::BrowserThread::UI, &loop_), | 96 ui_thread_(content::BrowserThread::UI, &loop_), |
98 file_thread_(content::BrowserThread::FILE, &loop_), | 97 file_thread_(content::BrowserThread::FILE, &loop_), |
99 io_thread_(content::BrowserThread::IO), | 98 io_thread_(local_state_.Get()), |
100 io_thread_state_(local_state_.Get(), NULL, NULL), | |
101 prepared_profile_(NULL) {} | 99 prepared_profile_(NULL) {} |
102 | 100 |
103 virtual void SetUp() OVERRIDE { | 101 virtual void SetUp() OVERRIDE { |
104 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 102 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
105 // BrowserPolicyConnector makes the UserPolicyCache read relative to this | 103 // BrowserPolicyConnector makes the UserPolicyCache read relative to this |
106 // path. Make sure it's in a clean state. | 104 // path. Make sure it's in a clean state. |
107 PathService::Override(chrome::DIR_USER_DATA, scoped_temp_dir_.path()); | 105 PathService::Override(chrome::DIR_USER_DATA, scoped_temp_dir_.path()); |
108 | 106 |
109 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 107 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
110 command_line->AppendSwitch(switches::kEnableDevicePolicy); | 108 command_line->AppendSwitch(switches::kEnableDevicePolicy); |
111 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, kDMServer); | 109 command_line->AppendSwitchASCII(switches::kDeviceManagementUrl, kDMServer); |
112 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); | 110 command_line->AppendSwitchASCII(switches::kLoginProfile, "user"); |
113 | 111 |
114 local_state_.Get()->RegisterStringPref(prefs::kApplicationLocale, ""); | 112 local_state_.Get()->RegisterStringPref(prefs::kApplicationLocale, ""); |
115 | 113 |
116 browser_process_->SetIOThread(&io_thread_state_); | 114 browser_process_->SetIOThread(&io_thread_); |
117 | 115 |
118 DBusThreadManager::InitializeForTesting(&dbus_thread_manager_); | 116 DBusThreadManager::InitializeForTesting(&dbus_thread_manager_); |
119 | 117 |
120 MockSessionManagerClient* session_managed_client = | 118 MockSessionManagerClient* session_managed_client = |
121 dbus_thread_manager_.mock_session_manager_client(); | 119 dbus_thread_manager_.mock_session_manager_client(); |
122 EXPECT_CALL(*session_managed_client, RetrievePolicy(_)) | 120 EXPECT_CALL(*session_managed_client, RetrievePolicy(_)) |
123 .WillRepeatedly(MockSessionManagerClientPolicyCallback("")); | 121 .WillRepeatedly(MockSessionManagerClientPolicyCallback("")); |
124 | 122 |
125 CrosLibrary::TestApi* test_api = CrosLibrary::Get()->GetTestApi(); | 123 CrosLibrary::TestApi* test_api = CrosLibrary::Get()->GetTestApi(); |
126 ASSERT_TRUE(test_api); | 124 ASSERT_TRUE(test_api); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 162 } |
165 | 163 |
166 virtual void TearDown() OVERRIDE { | 164 virtual void TearDown() OVERRIDE { |
167 loop_.RunAllPending(); | 165 loop_.RunAllPending(); |
168 { | 166 { |
169 // chrome_browser_net::Predictor usually skips its shutdown routines on | 167 // chrome_browser_net::Predictor usually skips its shutdown routines on |
170 // unit_tests, but does the full thing when | 168 // unit_tests, but does the full thing when |
171 // g_browser_process->profile_manager() is valid during initialization. | 169 // g_browser_process->profile_manager() is valid during initialization. |
172 // Run a task on a temporary BrowserThread::IO that allows skipping | 170 // Run a task on a temporary BrowserThread::IO that allows skipping |
173 // these routines. | 171 // these routines. |
174 // | 172 io_thread_.set_message_loop(&loop_); |
175 // It is important to not have a fake message loop on the IO | |
176 // thread for the whole test, see comment on LoginUtilsTestBase | |
177 // constructor for details. | |
178 io_thread_.DeprecatedSetMessageLoop(&loop_); | |
179 loop_.PostTask(FROM_HERE, | 173 loop_.PostTask(FROM_HERE, |
180 base::Bind(&LoginUtilsTestBase::TearDownOnIO, | 174 base::Bind(&LoginUtilsTestBase::TearDownOnIO, |
181 base::Unretained(this))); | 175 base::Unretained(this))); |
182 loop_.RunAllPending(); | 176 loop_.RunAllPending(); |
183 io_thread_.DeprecatedSetMessageLoop(NULL); | 177 io_thread_.set_message_loop(NULL); |
184 } | 178 } |
185 | 179 |
186 // These trigger some tasks that have to run while BrowserThread::UI | 180 // These trigger some tasks that have to run while BrowserThread::UI |
187 // exists. | 181 // exists. |
188 connector_ = NULL; | 182 connector_ = NULL; |
189 browser_process_->SetBrowserPolicyConnector(NULL); | 183 browser_process_->SetBrowserPolicyConnector(NULL); |
190 browser_process_->SetProfileManager(NULL); | 184 browser_process_->SetProfileManager(NULL); |
191 loop_.RunAllPending(); | 185 loop_.RunAllPending(); |
192 } | 186 } |
193 | 187 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 | 298 |
305 protected: | 299 protected: |
306 ScopedStubCrosEnabler stub_cros_enabler_; | 300 ScopedStubCrosEnabler stub_cros_enabler_; |
307 | 301 |
308 MessageLoop loop_; | 302 MessageLoop loop_; |
309 TestingBrowserProcess* browser_process_; | 303 TestingBrowserProcess* browser_process_; |
310 ScopedTestingLocalState local_state_; | 304 ScopedTestingLocalState local_state_; |
311 | 305 |
312 content::TestBrowserThread ui_thread_; | 306 content::TestBrowserThread ui_thread_; |
313 content::TestBrowserThread file_thread_; | 307 content::TestBrowserThread file_thread_; |
314 content::TestBrowserThread io_thread_; | 308 TestIOThread io_thread_; |
315 IOThread io_thread_state_; | |
316 | 309 |
317 MockDBusThreadManager dbus_thread_manager_; | 310 MockDBusThreadManager dbus_thread_manager_; |
318 TestURLFetcherFactory test_url_fetcher_factory_; | 311 TestURLFetcherFactory test_url_fetcher_factory_; |
319 | 312 |
320 policy::BrowserPolicyConnector* connector_; | 313 policy::BrowserPolicyConnector* connector_; |
321 MockCryptohomeLibrary* cryptohome_; | 314 MockCryptohomeLibrary* cryptohome_; |
322 Profile* prepared_profile_; | 315 Profile* prepared_profile_; |
323 | 316 |
324 private: | 317 private: |
325 ScopedTempDir scoped_temp_dir_; | 318 ScopedTempDir scoped_temp_dir_; |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 449 } |
457 | 450 |
458 INSTANTIATE_TEST_CASE_P( | 451 INSTANTIATE_TEST_CASE_P( |
459 LoginUtilsBlockingLoginTestInstance, | 452 LoginUtilsBlockingLoginTestInstance, |
460 LoginUtilsBlockingLoginTest, | 453 LoginUtilsBlockingLoginTest, |
461 testing::Values(0, 1, 2, 3, 4, 5)); | 454 testing::Values(0, 1, 2, 3, 4, 5)); |
462 | 455 |
463 } // namespace | 456 } // namespace |
464 | 457 |
465 } | 458 } |
OLD | NEW |