user_cloud_policy_manager_chromeos_unittest.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1// Copyright (c) 2012 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/policy/user_cloud_policy_manager_chromeos.h"
6
7#include "base/basictypes.h"
8#include "base/bind.h"
9#include "base/bind_helpers.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/message_loop.h"
12#include "base/prefs/pref_registry_simple.h"
13#include "base/prefs/testing_pref_service.h"
14#include "base/run_loop.h"
15#include "base/string_util.h"
16#include "base/utf_string_conversions.h"
17#include "chrome/browser/chromeos/policy/user_cloud_policy_token_forwarder.h"
18#include "chrome/browser/chromeos/profiles/profile_helper.h"
19#include "chrome/browser/policy/cloud/cloud_policy_constants.h"
20#include "chrome/browser/policy/cloud/cloud_policy_service.h"
21#include "chrome/browser/policy/cloud/mock_cloud_policy_store.h"
22#include "chrome/browser/policy/cloud/mock_device_management_service.h"
23#include "chrome/browser/policy/cloud/resource_cache.h"
24#include "chrome/browser/policy/mock_configuration_policy_provider.h"
25#include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
26#include "chrome/browser/prefs/browser_prefs.h"
27#include "chrome/browser/signin/token_service.h"
28#include "chrome/browser/signin/token_service_factory.h"
29#include "chrome/common/chrome_constants.h"
30#include "chrome/test/base/testing_browser_process.h"
31#include "chrome/test/base/testing_profile.h"
32#include "chrome/test/base/testing_profile_manager.h"
33#include "content/public/test/test_browser_thread.h"
34#include "google_apis/gaia/gaia_auth_consumer.h"
35#include "google_apis/gaia/gaia_urls.h"
36#include "net/url_request/test_url_fetcher_factory.h"
37#include "net/url_request/url_fetcher_delegate.h"
38#include "net/url_request/url_request_context_getter.h"
39#include "net/url_request/url_request_status.h"
40#include "policy/policy_constants.h"
41#include "testing/gmock/include/gmock/gmock.h"
42#include "testing/gtest/include/gtest/gtest.h"
43
44namespace em = enterprise_management;
45
46using testing::AnyNumber;
47using testing::AtLeast;
48using testing::Mock;
49using testing::_;
50
51namespace policy {
52
53namespace {
54
55const char kOAuthTokenCookie[] = "oauth_token=1234";
56
57const char kOAuth2TokenPairData[] =
58    "{"
59    "  \"refresh_token\": \"1234\","
60    "  \"access_token\": \"5678\","
61    "  \"expires_in\": 3600"
62    "}";
63
64const char kOAuth2AccessTokenData[] =
65    "{"
66    "  \"access_token\": \"5678\","
67    "  \"expires_in\": 3600"
68    "}";
69
70}  // namespace
71
72class UserCloudPolicyManagerChromeOSTest : public testing::Test {
73 protected:
74  UserCloudPolicyManagerChromeOSTest()
75      : ui_thread_(content::BrowserThread::UI, &loop_),
76        io_thread_(content::BrowserThread::IO, &loop_),
77        store_(NULL),
78        profile_(NULL),
79        signin_profile_(NULL) {}
80
81  virtual void SetUp() OVERRIDE {
82    // The initialization path that blocks on the initial policy fetch requires
83    // a signin Profile to use its URLRequestContext.
84    profile_manager_.reset(
85        new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
86    ASSERT_TRUE(profile_manager_->SetUp());
87    profile_ = profile_manager_->CreateTestingProfile(
88        chrome::kInitialProfile, UTF8ToUTF16("testing_profile"), 0);
89    signin_profile_ = profile_manager_->CreateTestingProfile("signin_profile");
90    signin_profile_->set_incognito(true);
91    // Usually the signin Profile and the main Profile are separate, but since
92    // the signin Profile is an OTR Profile then for this test it suffices to
93    // attach it to the main Profile.
94    profile_->SetOffTheRecordProfile(signin_profile_);
95    signin_profile_->SetOriginalProfile(profile_);
96    signin_profile_->CreateRequestContext();
97    ASSERT_EQ(signin_profile_, chromeos::ProfileHelper::GetSigninProfile());
98
99    chrome::RegisterLocalState(prefs_.registry());
100
101    // Set up a policy map for testing.
102    policy_map_.Set("HomepageLocation",
103                    POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
104                    base::Value::CreateStringValue("http://chromium.org"));
105    expected_bundle_.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
106        .CopyFrom(policy_map_);
107
108    // Create fake policy blobs to deliver to the client.
109    em::DeviceRegisterResponse* register_response =
110        register_blob_.mutable_register_response();
111    register_response->set_device_management_token("dmtoken123");
112
113    em::CloudPolicySettings policy_proto;
114    policy_proto.mutable_homepagelocation()->set_value("http://chromium.org");
115    ASSERT_TRUE(
116        policy_proto.SerializeToString(policy_data_.mutable_policy_value()));
117    policy_data_.set_policy_type(dm_protocol::kChromeUserPolicyType);
118    policy_data_.set_request_token("dmtoken123");
119    policy_data_.set_device_id("id987");
120    em::PolicyFetchResponse* policy_response =
121        policy_blob_.mutable_policy_response()->add_response();
122    ASSERT_TRUE(policy_data_.SerializeToString(
123        policy_response->mutable_policy_data()));
124
125    EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _))
126        .Times(AnyNumber());
127  }
128
129  virtual void TearDown() OVERRIDE {
130    if (manager_) {
131      manager_->RemoveObserver(&observer_);
132      manager_->Shutdown();
133    }
134    signin_profile_->ResetRequestContext();
135  }
136
137  void CreateManager(bool wait_for_fetch) {
138    store_ = new MockCloudPolicyStore();
139    EXPECT_CALL(*store_, Load());
140    manager_.reset(new UserCloudPolicyManagerChromeOS(
141        scoped_ptr<CloudPolicyStore>(store_),
142        scoped_ptr<ResourceCache>(),
143        wait_for_fetch));
144    manager_->Init();
145    manager_->AddObserver(&observer_);
146    manager_->Connect(&prefs_, &device_management_service_, NULL,
147                      USER_AFFILIATION_NONE);
148    Mock::VerifyAndClearExpectations(store_);
149    EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
150    EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
151
152    if (!wait_for_fetch) {
153      // Create the UserCloudPolicyTokenForwarder, which forwards the refresh
154      // token from the TokenService to the UserCloudPolicyManagerChromeOS.
155      // This service is automatically created for regular Profiles but not for
156      // testing Profiles.
157      TokenService* token_service =
158          TokenServiceFactory::GetForProfile(profile_);
159      ASSERT_TRUE(token_service);
160      token_forwarder_.reset(
161          new UserCloudPolicyTokenForwarder(manager_.get(), token_service));
162    }
163  }
164
165  // Expects a pending URLFetcher for the |expected_url|, and returns it with
166  // prepared to deliver a response to its delegate.
167  net::TestURLFetcher* PrepareOAuthFetcher(const std::string& expected_url) {
168    net::TestURLFetcher* fetcher = test_url_fetcher_factory_.GetFetcherByID(0);
169    EXPECT_TRUE(fetcher);
170    if (!fetcher)
171      return NULL;
172    EXPECT_TRUE(fetcher->delegate());
173    EXPECT_TRUE(StartsWithASCII(fetcher->GetOriginalURL().spec(),
174                                expected_url,
175                                true));
176    fetcher->set_url(fetcher->GetOriginalURL());
177    fetcher->set_response_code(200);
178    fetcher->set_status(net::URLRequestStatus());
179    return fetcher;
180  }
181
182  // Issues the OAuth2 tokens and returns the device management register job
183  // if the flow succeeded.
184  MockDeviceManagementJob* IssueOAuthToken(bool has_request_token) {
185    EXPECT_FALSE(manager_->core()->client()->is_registered());
186
187    GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
188    net::TestURLFetcher* fetcher = NULL;
189
190    if (!has_request_token) {
191      // Issue the oauth_token cookie first.
192      fetcher = PrepareOAuthFetcher(gaia_urls->client_login_to_oauth2_url());
193      if (!fetcher)
194        return NULL;
195      net::ResponseCookies cookies;
196      cookies.push_back(kOAuthTokenCookie);
197      fetcher->set_cookies(cookies);
198      fetcher->delegate()->OnURLFetchComplete(fetcher);
199
200      // Issue the refresh token.
201      fetcher = PrepareOAuthFetcher(gaia_urls->oauth2_token_url());
202      if (!fetcher)
203        return NULL;
204      fetcher->SetResponseString(kOAuth2TokenPairData);
205      fetcher->delegate()->OnURLFetchComplete(fetcher);
206    }
207
208    // Issue the access token.
209    fetcher = PrepareOAuthFetcher(gaia_urls->oauth2_token_url());
210    if (!fetcher)
211      return NULL;
212    fetcher->SetResponseString(kOAuth2AccessTokenData);
213
214    // Issuing this token triggers the callback of the OAuth2PolicyFetcher,
215    // which triggers the registration request.
216    MockDeviceManagementJob* register_request = NULL;
217    EXPECT_CALL(device_management_service_,
218                CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
219        .WillOnce(device_management_service_.CreateAsyncJob(&register_request));
220    fetcher->delegate()->OnURLFetchComplete(fetcher);
221    EXPECT_TRUE(register_request);
222    EXPECT_FALSE(manager_->core()->client()->is_registered());
223
224    Mock::VerifyAndClearExpectations(&device_management_service_);
225    EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _))
226        .Times(AnyNumber());
227
228    return register_request;
229  }
230
231  // Expects a policy fetch request to be issued after invoking |trigger_fetch|.
232  // This method replies to that fetch request and verifies that the manager
233  // handled the response.
234  void FetchPolicy(const base::Closure& trigger_fetch) {
235    MockDeviceManagementJob* policy_request = NULL;
236    EXPECT_CALL(device_management_service_,
237                CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
238        .WillOnce(device_management_service_.CreateAsyncJob(&policy_request));
239    trigger_fetch.Run();
240    ASSERT_TRUE(policy_request);
241    EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
242    EXPECT_TRUE(manager_->core()->client()->is_registered());
243
244    Mock::VerifyAndClearExpectations(&device_management_service_);
245    EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _))
246        .Times(AnyNumber());
247
248    // Send the initial policy back. This completes the initialization flow.
249    EXPECT_CALL(*store_, Store(_));
250    policy_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_);
251    Mock::VerifyAndClearExpectations(store_);
252
253    // Notifying that the store is has cached the fetched policy completes the
254    // process, and initializes the manager.
255    EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
256    store_->policy_map_.CopyFrom(policy_map_);
257    store_->NotifyStoreLoaded();
258    EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
259    Mock::VerifyAndClearExpectations(&observer_);
260    EXPECT_TRUE(manager_->policies().Equals(expected_bundle_));
261  }
262
263  // Required by the refresh scheduler that's created by the manager.
264  base::MessageLoop loop_;
265  content::TestBrowserThread ui_thread_;
266  // Required to cleanup the URLRequestContextGetter of the |signin_profile_|.
267  content::TestBrowserThread io_thread_;
268
269  // Convenience policy objects.
270  em::PolicyData policy_data_;
271  em::DeviceManagementResponse register_blob_;
272  em::DeviceManagementResponse policy_blob_;
273  PolicyMap policy_map_;
274  PolicyBundle expected_bundle_;
275
276  // Policy infrastructure.
277  net::TestURLFetcherFactory test_url_fetcher_factory_;
278  TestingPrefServiceSimple prefs_;
279  MockConfigurationPolicyObserver observer_;
280  MockDeviceManagementService device_management_service_;
281  MockCloudPolicyStore* store_;
282  scoped_ptr<UserCloudPolicyManagerChromeOS> manager_;
283  scoped_ptr<UserCloudPolicyTokenForwarder> token_forwarder_;
284
285  // Required by ProfileHelper to get the signin Profile context.
286  scoped_ptr<TestingProfileManager> profile_manager_;
287  TestingProfile* profile_;
288  TestingProfile* signin_profile_;
289
290 private:
291  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerChromeOSTest);
292};
293
294TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFirstFetch) {
295  // Tests the initialization of a manager whose Profile is waiting for the
296  // initial fetch, when the policy cache is empty.
297  ASSERT_NO_FATAL_FAILURE(CreateManager(true));
298
299  // Initialize the CloudPolicyService without any stored data.
300  EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
301  store_->NotifyStoreLoaded();
302  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
303  EXPECT_FALSE(manager_->core()->client()->is_registered());
304
305  // This starts the OAuth2 policy token fetcher using the signin Profile.
306  // The manager will then issue the registration request.
307  MockDeviceManagementJob* register_request = IssueOAuthToken(false);
308  ASSERT_TRUE(register_request);
309
310  // Reply with a valid registration response. This triggers the initial policy
311  // fetch.
312  FetchPolicy(base::Bind(&MockDeviceManagementJob::SendResponse,
313                         base::Unretained(register_request),
314                         DM_STATUS_SUCCESS, register_blob_));
315}
316
317TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingRefreshFetch) {
318  // Tests the initialization of a manager whose Profile is waiting for the
319  // initial fetch, when a previously cached policy and DMToken already exist.
320  ASSERT_NO_FATAL_FAILURE(CreateManager(true));
321
322  // Set the initially cached data and initialize the CloudPolicyService.
323  // The initial policy fetch is issued using the cached DMToken.
324  store_->policy_.reset(new em::PolicyData(policy_data_));
325  FetchPolicy(base::Bind(&MockCloudPolicyStore::NotifyStoreLoaded,
326                         base::Unretained(store_)));
327}
328
329TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchStoreError) {
330  // Tests the initialization of a manager whose Profile is waiting for the
331  // initial fetch, when the initial store load fails.
332  ASSERT_NO_FATAL_FAILURE(CreateManager(true));
333
334  // Initialize the CloudPolicyService without any stored data.
335  EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
336  store_->NotifyStoreError();
337  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
338  EXPECT_FALSE(manager_->core()->client()->is_registered());
339
340  // This starts the OAuth2 policy token fetcher using the signin Profile.
341  // The manager will then issue the registration request.
342  MockDeviceManagementJob* register_request = IssueOAuthToken(false);
343  ASSERT_TRUE(register_request);
344
345  // Reply with a valid registration response. This triggers the initial policy
346  // fetch.
347  FetchPolicy(base::Bind(&MockDeviceManagementJob::SendResponse,
348                         base::Unretained(register_request),
349                         DM_STATUS_SUCCESS, register_blob_));
350}
351
352TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchOAuthError) {
353  // Tests the initialization of a manager whose Profile is waiting for the
354  // initial fetch, when the OAuth2 token fetch fails.
355  ASSERT_NO_FATAL_FAILURE(CreateManager(true));
356
357  // Initialize the CloudPolicyService without any stored data.
358  EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
359  store_->NotifyStoreLoaded();
360  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
361  EXPECT_FALSE(manager_->core()->client()->is_registered());
362
363  // This starts the OAuth2 policy token fetcher using the signin Profile.
364  // The manager will initialize with no policy after the token fetcher fails.
365  EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
366
367  // The PolicyOAuth2TokenFetcher posts delayed retries on some errors. This
368  // data will make it fail immediately.
369  net::TestURLFetcher* fetcher = PrepareOAuthFetcher(
370      GaiaUrls::GetInstance()->client_login_to_oauth2_url());
371  ASSERT_TRUE(fetcher);
372  fetcher->set_response_code(400);
373  fetcher->SetResponseString("Error=BadAuthentication");
374  EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
375  fetcher->delegate()->OnURLFetchComplete(fetcher);
376  EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
377  EXPECT_TRUE(PolicyBundle().Equals(manager_->policies()));
378  Mock::VerifyAndClearExpectations(&observer_);
379}
380
381TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchRegisterError) {
382  // Tests the initialization of a manager whose Profile is waiting for the
383  // initial fetch, when the device management registration fails.
384  ASSERT_NO_FATAL_FAILURE(CreateManager(true));
385
386  // Initialize the CloudPolicyService without any stored data.
387  EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
388  store_->NotifyStoreError();
389  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
390  EXPECT_FALSE(manager_->core()->client()->is_registered());
391
392  // This starts the OAuth2 policy token fetcher using the signin Profile.
393  // The manager will then issue the registration request.
394  MockDeviceManagementJob* register_request = IssueOAuthToken(false);
395  ASSERT_TRUE(register_request);
396
397  // Now make it fail.
398  EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
399  EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
400  register_request->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE,
401                                 em::DeviceManagementResponse());
402  EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
403  EXPECT_TRUE(PolicyBundle().Equals(manager_->policies()));
404  Mock::VerifyAndClearExpectations(&observer_);
405}
406
407TEST_F(UserCloudPolicyManagerChromeOSTest, BlockingFetchPolicyFetchError) {
408  // Tests the initialization of a manager whose Profile is waiting for the
409  // initial fetch, when the policy fetch request fails.
410  ASSERT_NO_FATAL_FAILURE(CreateManager(true));
411
412  // Initialize the CloudPolicyService without any stored data.
413  EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
414  store_->NotifyStoreLoaded();
415  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
416  EXPECT_FALSE(manager_->core()->client()->is_registered());
417
418  // This starts the OAuth2 policy token fetcher using the signin Profile.
419  // The manager will then issue the registration request.
420  MockDeviceManagementJob* register_request = IssueOAuthToken(false);
421  ASSERT_TRUE(register_request);
422
423  // Reply with a valid registration response. This triggers the initial policy
424  // fetch.
425  MockDeviceManagementJob* policy_request = NULL;
426  EXPECT_CALL(device_management_service_,
427              CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
428      .WillOnce(device_management_service_.CreateAsyncJob(&policy_request));
429  register_request->SendResponse(DM_STATUS_SUCCESS, register_blob_);
430  Mock::VerifyAndClearExpectations(&device_management_service_);
431  ASSERT_TRUE(policy_request);
432  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
433  EXPECT_TRUE(manager_->core()->client()->is_registered());
434
435  // Make the policy fetch fail. The observer gets 2 notifications: one from the
436  // RefreshPolicies callback, and another from the OnClientError callback.
437  // A single notification suffices for this edge case, but this behavior is
438  // also correct and makes the implementation simpler.
439  EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1));
440  EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
441  policy_request->SendResponse(DM_STATUS_TEMPORARY_UNAVAILABLE,
442                               em::DeviceManagementResponse());
443  Mock::VerifyAndClearExpectations(&observer_);
444  EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
445  EXPECT_TRUE(PolicyBundle().Equals(manager_->policies()));
446}
447
448TEST_F(UserCloudPolicyManagerChromeOSTest, NonBlockingFirstFetch) {
449  // Tests the first policy fetch request by a Profile that isn't managed.
450  ASSERT_NO_FATAL_FAILURE(CreateManager(false));
451
452  // Initialize the CloudPolicyService without any stored data. Since the
453  // manager is not waiting for the initial fetch, it will become initialized
454  // once the store is ready.
455  EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
456  EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
457  EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
458  store_->NotifyStoreLoaded();
459  Mock::VerifyAndClearExpectations(&observer_);
460  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
461  EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
462  EXPECT_FALSE(manager_->core()->client()->is_registered());
463
464  // The manager is waiting for the refresh token, and hasn't started any
465  // fetchers.
466  EXPECT_FALSE(test_url_fetcher_factory_.GetFetcherByID(0));
467
468  // Set a fake refresh token at the TokenService.
469  TokenService* token_service = TokenServiceFactory::GetForProfile(profile_);
470  ASSERT_TRUE(token_service);
471  GaiaAuthConsumer::ClientOAuthResult tokens("refresh", "access", 3600);
472  EXPECT_FALSE(token_service->HasOAuthLoginToken());
473  token_service->UpdateCredentialsWithOAuth2(tokens);
474  EXPECT_TRUE(token_service->HasOAuthLoginToken());
475
476  // That should have notified the manager, which now issues the request for the
477  // policy oauth token.
478  MockDeviceManagementJob* register_request = IssueOAuthToken(true);
479  ASSERT_TRUE(register_request);
480  register_request->SendResponse(DM_STATUS_SUCCESS, register_blob_);
481
482  // The refresh scheduler takes care of the initial fetch for unmanaged users.
483  // It posts a delayed task with 0ms delay in this case, so spinning the loop
484  // issues the initial fetch.
485  base::RunLoop loop;
486  FetchPolicy(
487      base::Bind(&base::RunLoop::RunUntilIdle, base::Unretained(&loop)));
488}
489
490TEST_F(UserCloudPolicyManagerChromeOSTest, NonBlockingRefreshFetch) {
491  // Tests a non-blocking initial policy fetch for a Profile that already has
492  // a cached DMToken.
493  ASSERT_NO_FATAL_FAILURE(CreateManager(false));
494
495  // Set the initially cached data and initialize the CloudPolicyService.
496  // The initial policy fetch is issued using the cached DMToken.
497  EXPECT_FALSE(manager_->core()->service()->IsInitializationComplete());
498  EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
499  EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
500  store_->policy_.reset(new em::PolicyData(policy_data_));
501  store_->NotifyStoreLoaded();
502  Mock::VerifyAndClearExpectations(&observer_);
503  EXPECT_TRUE(manager_->core()->service()->IsInitializationComplete());
504  EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
505  EXPECT_TRUE(manager_->core()->client()->is_registered());
506
507  // The refresh scheduler takes care of the initial fetch for unmanaged users.
508  // It posts a delayed task with 0ms delay in this case, so spinning the loop
509  // issues the initial fetch.
510  base::RunLoop loop;
511  FetchPolicy(
512      base::Bind(&base::RunLoop::RunUntilIdle, base::Unretained(&loop)));
513}
514
515}  // namespace policy
516