device_cloud_policy_invalidator_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
14ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// Copyright 2014 The Chromium Authors. All rights reserved.
24ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// Use of this source code is governed by a BSD-style license that can be
34ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// found in the LICENSE file.
44ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
54ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/policy/device_cloud_policy_invalidator.h"
64ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
74ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include <string>
84ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
94ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "base/memory/ref_counted.h"
10c7a4104957aa1f6fa36e4e9cf65d36b699eb05bdBrandon Jones#include "base/message_loop/message_loop_proxy.h"
110b7eef7c469bf717f7e1b57c6273f00d88e8b1d9Geoff Lang#include "base/run_loop.h"
120b7eef7c469bf717f7e1b57c6273f00d88e8b1d9Geoff Lang#include "chrome/browser/browser_process_platform_part.h"
130b7eef7c469bf717f7e1b57c6273f00d88e8b1d9Geoff Lang#include "chrome/browser/chrome_notification_types.h"
140b7eef7c469bf717f7e1b57c6273f00d88e8b1d9Geoff Lang#include "chrome/browser/chromeos/login/users/fake_user_manager.h"
150b7eef7c469bf717f7e1b57c6273f00d88e8b1d9Geoff Lang#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
164ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
174ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
18e261b44af7631c31ddca8601490e7445af5a3341Jamie Madill#include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h"
19486d9e9b6b4ed31f66d2624b8e822020fe40a1f7shannon.woods@transgaming.com#include "chrome/browser/chromeos/policy/device_policy_builder.h"
204ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
214ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/settings/cros_settings.h"
224ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
234ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/settings/device_settings_service.h"
244ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
2512e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "chrome/browser/invalidation/fake_invalidation_service.h"
264ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
2712e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "chrome/browser/policy/cloud/cloud_policy_invalidator.h"
2812e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "chrome/browser/profiles/profile.h"
294ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "chrome/test/base/testing_browser_process.h"
3012e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "chrome/test/base/testing_profile_manager.h"
3112e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "chromeos/cryptohome/system_salt_getter.h"
3212e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "chromeos/dbus/dbus_thread_manager.h"
334ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "components/invalidation/invalidation_service.h"
3412e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "components/invalidation/invalidator_state.h"
354ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "components/invalidation/profile_invalidation_provider.h"
36ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang#include "components/invalidation/ticl_invalidation_service.h"
374ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "components/keyed_service/core/keyed_service.h"
384ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "components/ownership/mock_owner_key_util.h"
3912e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com#include "components/policy/core/common/cloud/cloud_policy_client.h"
404ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "components/policy/core/common/cloud/cloud_policy_constants.h"
414ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "components/policy/core/common/cloud/cloud_policy_core.h"
424ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
434ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "content/public/browser/browser_context.h"
444ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "content/public/browser/notification_details.h"
454ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "content/public/browser/notification_service.h"
464ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "content/public/test/test_browser_thread_bundle.h"
474ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "net/url_request/url_request_context_getter.h"
484ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "net/url_request/url_request_test_util.h"
494ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "policy/proto/device_management_backend.pb.h"
504ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "testing/gmock/include/gmock/gmock.h"
514ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com#include "testing/gtest/include/gtest/gtest.h"
524ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5391fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Langnamespace policy {
5491fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
55cead8ad71769f28cbf8df829f60952999d3df602shannonwoods@chromium.orgnamespace {
564ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
574ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comconst char kAffiliatedUserID1[] = "test_1@example.com";
584ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comconst char kAffiliatedUserID2[] = "test_2@example.com";
594ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comconst char kUnaffiliatedUserID[] = "test_2@other_domain.test";
604ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
614ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comKeyedService* BuildProfileInvalidationProvider(
624ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com    content::BrowserContext* context) {
634ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  scoped_ptr<invalidation::FakeInvalidationService> invalidation_service(
6457f9b6d56bec5e2040909b9dc0c68ddb7ac4a978Geoff Lang      new invalidation::FakeInvalidationService);
6557f9b6d56bec5e2040909b9dc0c68ddb7ac4a978Geoff Lang  invalidation_service->SetInvalidatorState(
664ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::TRANSIENT_INVALIDATION_ERROR);
674ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  return new invalidation::ProfileInvalidationProvider(
684ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      invalidation_service.PassAs<invalidation::InvalidationService>());
6991fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang}
7091fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
714ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}  // namespace
72cead8ad71769f28cbf8df829f60952999d3df602shannonwoods@chromium.org
73cead8ad71769f28cbf8df829f60952999d3df602shannonwoods@chromium.orgclass DeviceCloudPolicyInvalidatorTest : public testing::Test {
74cead8ad71769f28cbf8df829f60952999d3df602shannonwoods@chromium.org public:
754ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  DeviceCloudPolicyInvalidatorTest();
764ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  virtual ~DeviceCloudPolicyInvalidatorTest();
774ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
784ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // testing::Test:
798b400b1e8d84c5b93dd151807504a3e4b90d1b21apatrick@chromium.org  virtual void SetUp() OVERRIDE;
804ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  virtual void TearDown() OVERRIDE;
814ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
824ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Ownership is not passed. The Profile is owned by the global ProfileManager.
834ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  Profile *LogInAndReturnProfile(const std::string& user_id);
844ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
854ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::TiclInvalidationService* GetDeviceInvalidationService();
864ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  bool HasDeviceInvalidationServiceObserver() const;
87779aa2618c1333c034768f3a4ca7826a14753b60shannon.woods@transgaming.com
884ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::FakeInvalidationService* GetProfileInvalidationService(
894ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      Profile* profile);
9012e02218fe8a0a8ff962463a4f185d276421195bdaniel@transgaming.com  int GetProfileInvalidationServiceObserverCount() const;
914ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
925dd4502f2fdc5a832bb23997348b82c16d2537c3daniel@transgaming.com  const invalidation::InvalidationService* GetInvalidationService() const;
934ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyInvalidator* GetCloudPolicyInvalidator() const;
944ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
954ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  void ConnectDeviceInvalidationService();
964ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
974ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com protected:
984ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  DevicePolicyBuilder device_policy_;
9991fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
1004ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com private:
1014ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  content::TestBrowserThreadBundle thread_bundle_;
10291fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  scoped_refptr<net::URLRequestContextGetter> system_request_context_;
1034ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  TestingProfileManager profile_manager_;
1044ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  chromeos::FakeUserManager* fake_user_manager_;
1054ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  chromeos::ScopedUserManagerEnabler user_manager_enabler_;
1064ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ScopedStubEnterpriseInstallAttributes install_attributes_;
1074ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  scoped_ptr<chromeos::ScopedTestDeviceSettingsService>
10891fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang      test_device_settings_service_;
10991fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_;
110803be0a97a07688190ea977251959467e7f94daeshannonwoods@chromium.org  chromeos::DeviceSettingsTestHelper device_settings_test_helper_;
11191fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
11291fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  scoped_ptr<DeviceCloudPolicyInvalidator> invalidator_;
11391fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang};
1144ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
11591fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff LangDeviceCloudPolicyInvalidatorTest::DeviceCloudPolicyInvalidatorTest()
1164ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com    : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
1174ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      system_request_context_(new net::TestURLRequestContextGetter(
1184ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com          base::MessageLoopProxy::current())),
1194ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      profile_manager_(TestingBrowserProcess::GetGlobal()),
1204ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      fake_user_manager_(new chromeos::FakeUserManager),
1214ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      user_manager_enabler_(fake_user_manager_),
1224ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      install_attributes_("example.com",
1234ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com                          "user@example.com",
1244ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com                          "device_id",
1254ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com                          DEVICE_MODE_ENTERPRISE) {
126005df41f8900641ed1df60700c8e2eca659a33cbGeoff Lang}
1274ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
1284760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.comDeviceCloudPolicyInvalidatorTest::~DeviceCloudPolicyInvalidatorTest() {
1294760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com}
1304760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com
13156074f3318a3854b8bfad1498ef719c73212290fshannon.woods%transgaming.com@gtempaccount.comvoid DeviceCloudPolicyInvalidatorTest::SetUp() {
13256074f3318a3854b8bfad1498ef719c73212290fshannon.woods%transgaming.com@gtempaccount.com  chromeos::SystemSaltGetter::Initialize();
13356074f3318a3854b8bfad1498ef719c73212290fshannon.woods%transgaming.com@gtempaccount.com  chromeos::DBusThreadManager::Initialize();
1344ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  chromeos::DeviceOAuth2TokenServiceFactory::Initialize();
1354ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(
1364760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com      system_request_context_.get());
1374ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_manager_.SetUp());
1384ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
1394ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  test_device_settings_service_.reset(new
1404ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      chromeos::ScopedTestDeviceSettingsService);
1414ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings);
1424ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util(
1434ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      new ownership::MockOwnerKeyUtil);
1444760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com  owner_key_util->SetPublicKeyFromPrivateKey(
1454ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      *device_policy_.GetSigningKey());
146803be0a97a07688190ea977251959467e7f94daeshannonwoods@chromium.org  chromeos::DeviceSettingsService::Get()->SetSessionManager(
1474ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      &device_settings_test_helper_,
14891fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang      owner_key_util);
14991fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
15091fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  device_policy_.policy_data().set_invalidation_source(123);
15191fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  device_policy_.policy_data().set_invalidation_name("invalidation");
15291fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  device_policy_.Build();
1534ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
154ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang  device_settings_test_helper_.Flush();
15591fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
1564ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  scoped_ptr<MockCloudPolicyClient> policy_client(new MockCloudPolicyClient);
1574ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_CALL(*policy_client, SetupRegistration("token", "device-id"));
1584ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyCore* core = TestingBrowserProcess::GetGlobal()->platform_part()->
1594ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()->
1604ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com          core();
1614ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  core->Connect(policy_client.PassAs<CloudPolicyClient>());
1624ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  core->StartRefreshScheduler();
1634ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
1644ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::ProfileInvalidationProviderFactory::GetInstance()->
1654ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      RegisterTestingFactory(BuildProfileInvalidationProvider);
1664ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
1674ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidator_.reset(new DeviceCloudPolicyInvalidator);
1684ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
1694ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
1704ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comvoid DeviceCloudPolicyInvalidatorTest::TearDown() {
1714ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidator_.reset();
1724ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  base::RunLoop().RunUntilIdle();
1734ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
1744ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::ProfileInvalidationProviderFactory::GetInstance()->
1754ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      RegisterTestingFactory(NULL);
1764ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  chromeos::DeviceSettingsService::Get()->UnsetSessionManager();
1774ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL);
1784ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  chromeos::DeviceOAuth2TokenServiceFactory::Shutdown();
1794ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  chromeos::DBusThreadManager::Shutdown();
180803be0a97a07688190ea977251959467e7f94daeshannonwoods@chromium.org  chromeos::SystemSaltGetter::Shutdown();
1814ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
182204677af271502bb1f91d4abd0cf8ab4ab5b49d0daniel@transgaming.com
1834ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comProfile *DeviceCloudPolicyInvalidatorTest::LogInAndReturnProfile(
1844ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com    const std::string& user_id) {
1854ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  fake_user_manager_->AddUser(user_id);
1864ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  Profile* profile = profile_manager_.CreateTestingProfile(user_id);
1874ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  content::NotificationService::current()->Notify(
1884ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
1894ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      content::NotificationService::AllSources(),
1904ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      content::Details<Profile>(profile));
191779aa2618c1333c034768f3a4ca7826a14753b60shannon.woods@transgaming.com  return profile;
1924ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
1934ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
1944ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.cominvalidation::TiclInvalidationService*
195ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff LangDeviceCloudPolicyInvalidatorTest::GetDeviceInvalidationService() {
19634256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang  return invalidator_->device_invalidation_service_.get();
19791fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang}
19891fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
19934256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Langbool DeviceCloudPolicyInvalidatorTest::HasDeviceInvalidationServiceObserver(
20034256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang    ) const {
20134256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang  return invalidator_->device_invalidation_service_observer_.get();
20234256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang}
20334256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang
20434256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Langinvalidation::FakeInvalidationService*
20534256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff LangDeviceCloudPolicyInvalidatorTest::GetProfileInvalidationService(
20634256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang    Profile* profile) {
20734256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang  invalidation::ProfileInvalidationProvider* invalidation_provider =
20834256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang      static_cast<invalidation::ProfileInvalidationProvider*>(
20934256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang          invalidation::ProfileInvalidationProviderFactory::GetInstance()->
21091fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang              GetServiceForBrowserContext(profile, false));
21191fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  if (!invalidation_provider)
21234256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang    return NULL;
21334256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang  return static_cast<invalidation::FakeInvalidationService*>(
21434256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang      invalidation_provider->GetInvalidationService());
21534256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang}
2164ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2174ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comint DeviceCloudPolicyInvalidatorTest::
2184ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com        GetProfileInvalidationServiceObserverCount() const {
2194ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  return invalidator_->profile_invalidation_service_observers_.size();
2204ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
2214ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2224ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comconst invalidation::InvalidationService*
2234ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comDeviceCloudPolicyInvalidatorTest::GetInvalidationService() const {
2244ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  return invalidator_->invalidation_service_;
2254ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
2264ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2274ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comCloudPolicyInvalidator*
2284ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comDeviceCloudPolicyInvalidatorTest::GetCloudPolicyInvalidator() const {
2294ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  return invalidator_->invalidator_.get();
2304ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
2314ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2324ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comvoid DeviceCloudPolicyInvalidatorTest::ConnectDeviceInvalidationService() {
2334ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  const int per_profile_invalidation_service_observer_count =
2344ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      GetProfileInvalidationServiceObserverCount();
2354ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2364ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a device-global invalidation service has been created.
2374ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(GetDeviceInvalidationService());
2384ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
2394ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2404ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that no new per-profile invalidation service observers have been
2414ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // created.
2424ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(per_profile_invalidation_service_observer_count,
2434ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com            GetProfileInvalidationServiceObserverCount());
2444ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2459cd1915c96800fc998ec25b88b0e3081f0aa6109Geoff Lang  // Verify that no invalidator exists yet
2464ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
2474ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
2484ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2494ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the device-global invalidation service has connected.
2504ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  GetDeviceInvalidationService()->OnInvalidatorStateChange(
2514ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::INVALIDATIONS_ENABLED);
2524ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  base::RunLoop().RunUntilIdle();
2534ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2544ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidation service still exists.
2554ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
2564ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
2574ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2584ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the device-global invalidation service
25934256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang  // has been created.
26034256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang  EXPECT_TRUE(GetCloudPolicyInvalidator());
26134256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
26234256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang}
26391fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
26434256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang// Verifies that a DeviceCloudPolicyInvalidator backed by a device-global
26534256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang// invalidation service is created/destroyed as the service
2664ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// connects/disconnects.
2674ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comTEST_F(DeviceCloudPolicyInvalidatorTest, UseDeviceInvalidationService) {
2684ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the device-global invalidation service
2694ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // is created when the service connects.
2704ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ConnectDeviceInvalidationService();
2714ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(GetDeviceInvalidationService());
2724ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
2732f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill
2744ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the device-global invalidation service has disconnected.
2752f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill  GetDeviceInvalidationService()->OnInvalidatorStateChange(
27634da397e13a101d6157619bfbd6c2f33efbd1a31daniel@transgaming.com      syncer::INVALIDATION_CREDENTIALS_REJECTED);
2774ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  base::RunLoop().RunUntilIdle();
2784ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2792f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill  // Verify that the device-global invalidation service still exists.
2804ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
2812f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
28234da397e13a101d6157619bfbd6c2f33efbd1a31daniel@transgaming.com
2834ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the invalidator has been destroyed.
2844ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
2854ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
2864ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
2874ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
2884ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// Verifies that when the per-profile invalidation service for an affiliated
2894ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// user connect/disconnects, a DeviceCloudPolicyInvalidator backed by it is
2904ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// created/destroyed.
2914ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comTEST_F(DeviceCloudPolicyInvalidatorTest,
2924ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com       UseAffiliatedProfileInvalidationService) {
2934ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Log in as an affiliated user.
2944ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
2954ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile);
296ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang
2974ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a device-global invalidation service has been created.
2984ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
2994ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
3004ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3014ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service has been created.
3024ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::FakeInvalidationService* profile_invalidation_service =
3034ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      GetProfileInvalidationService(profile);
3042f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill  ASSERT_TRUE(profile_invalidation_service);
3054ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
3064ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3072f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill  // Verify that no invalidator exists yet.
308169d11186cb1fdef8920668169682cf84749606dJamie Madill  EXPECT_FALSE(GetCloudPolicyInvalidator());
3094ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
3104ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3112f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill  // Indicate that the per-profile invalidation service has connected.
3124ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service->SetInvalidatorState(
3134ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::INVALIDATIONS_ENABLED);
3142f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill
315169d11186cb1fdef8920668169682cf84749606dJamie Madill  // Verify that the device-global invalidator has been destroyed.
3164ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetDeviceInvalidationService());
3174ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
3182f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill
3192058d64a7c8c3cf5f79c3ab3fad055cd984c8497shannon.woods%transgaming.com@gtempaccount.com  // Verify that a per-profile invalidation service still exists.
3202058d64a7c8c3cf5f79c3ab3fad055cd984c8497shannon.woods%transgaming.com@gtempaccount.com  profile_invalidation_service = GetProfileInvalidationService(profile);
3212058d64a7c8c3cf5f79c3ab3fad055cd984c8497shannon.woods%transgaming.com@gtempaccount.com  ASSERT_TRUE(profile_invalidation_service);
3222058d64a7c8c3cf5f79c3ab3fad055cd984c8497shannon.woods%transgaming.com@gtempaccount.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
3232058d64a7c8c3cf5f79c3ab3fad055cd984c8497shannon.woods%transgaming.com@gtempaccount.com
3242058d64a7c8c3cf5f79c3ab3fad055cd984c8497shannon.woods%transgaming.com@gtempaccount.com  // Verify that an invalidator backed by the per-profile invalidation service
3252f06dbfb3f4bd815a3fe5b52638b091c1e356a04Jamie Madill  // has been created.
3266c86bd5f6358dfe721133bae6e599c4c5fdaf04cshannon.woods%transgaming.com@gtempaccount.com  EXPECT_TRUE(GetCloudPolicyInvalidator());
3276c86bd5f6358dfe721133bae6e599c4c5fdaf04cshannon.woods%transgaming.com@gtempaccount.com  EXPECT_EQ(profile_invalidation_service, GetInvalidationService());
3286c86bd5f6358dfe721133bae6e599c4c5fdaf04cshannon.woods%transgaming.com@gtempaccount.com
3296c86bd5f6358dfe721133bae6e599c4c5fdaf04cshannon.woods%transgaming.com@gtempaccount.com  // Indicate that the per-profile invalidation service has disconnected.
3306c86bd5f6358dfe721133bae6e599c4c5fdaf04cshannon.woods%transgaming.com@gtempaccount.com  profile_invalidation_service->SetInvalidatorState(
3316c86bd5f6358dfe721133bae6e599c4c5fdaf04cshannon.woods%transgaming.com@gtempaccount.com      syncer::INVALIDATION_CREDENTIALS_REJECTED);
332169d11186cb1fdef8920668169682cf84749606dJamie Madill
3334ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a device-global invalidation service has been created.
334c41e42d49626e1527c9e049b48d7c9fb30b0b9d6Geoff Lang  EXPECT_TRUE(GetDeviceInvalidationService());
335c41e42d49626e1527c9e049b48d7c9fb30b0b9d6Geoff Lang  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
3364ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3374ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service still exists.
3384ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service = GetProfileInvalidationService(profile);
3394ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(profile_invalidation_service);
3404ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
3414ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3424ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the invalidator has been destroyed.
3434ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
3444ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
3454ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
3464ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3474ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// Verifies that even if the per-profile invalidation service for an
3484ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// unaffiliated user connects, no DeviceCloudPolicyInvalidator backed by it is
3494ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// created.
3504ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comTEST_F(DeviceCloudPolicyInvalidatorTest,
351204677af271502bb1f91d4abd0cf8ab4ab5b49d0daniel@transgaming.com       DoNotUseUnaffiliatedProfileInvalidationService) {
3524ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Log in as an unaffiliated user.
3534ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID);
3544ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile);
3554ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3564ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a device-global invalidation service has been created.
3574ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
3584ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
3594ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3604ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service has been created.
3614ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::FakeInvalidationService* profile_invalidation_service =
3624ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      GetProfileInvalidationService(profile);
3634ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_invalidation_service);
3644ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount());
3654ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
366ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang  // Verify that no invalidator exists yet.
3674ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
3684ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
3694ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3704ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the per-profile invalidation service has connected.
3719f0b42a6ab87ae2b2f065cf022497ac6deabf3fdJamie Madill  profile_invalidation_service->SetInvalidatorState(
3724ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::INVALIDATIONS_ENABLED);
3739cd1915c96800fc998ec25b88b0e3081f0aa6109Geoff Lang
3744ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidator still exists.
3754ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
3764ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
3774ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
378ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang  // Verify that a per-profile invalidation service still exists.
3794ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service = GetProfileInvalidationService(profile);
3804ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(profile_invalidation_service);
3814ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount());
3824ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
3834ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that no invalidator has been created.
3844760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
385557aab057cafe276182e798a50ce8c23b410f6f4shannonwoods@chromium.org  EXPECT_FALSE(GetInvalidationService());
3864ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
3874760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com
3884760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com// Verifies that when the per-profile invalidation service for an affiliated
3894760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com// user connects, a DeviceCloudPolicyInvalidator backed by it replaces the
3905d601382b51c29d1670b58c01360416bd929842dGeoff Lang// current DeviceCloudPolicyInvalidator backed by a device-global invalidation
3915d601382b51c29d1670b58c01360416bd929842dGeoff Lang// service. Also verifies that the device-global invalidation service is
392557aab057cafe276182e798a50ce8c23b410f6f4shannonwoods@chromium.org// destroyed at this point and the highest handled invalidation version is
39391fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang// preserved when switching invalidation services.
39491fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff LangTEST_F(DeviceCloudPolicyInvalidatorTest,
395557aab057cafe276182e798a50ce8c23b410f6f4shannonwoods@chromium.org       SwitchToAffiliatedProfileInvalidationService) {
3964ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyStore* store = static_cast<CloudPolicyStore*>(
3974ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      TestingBrowserProcess::GetGlobal()->platform_part()->
3984ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com          browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()->
3994ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com              device_store());
4004ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(store);
4014ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4024ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the device-global invalidation service
4034ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // is created when the service connects.
4044ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ConnectDeviceInvalidationService();
4054ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyInvalidator* invalidator = GetCloudPolicyInvalidator();
4064ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(invalidator);
4074ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
4084ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
40991fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  // Verify that the invalidator's highest handled invalidation version starts
41091fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  // out as zero.
41191fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  EXPECT_EQ(0, invalidator->highest_handled_invalidation_version());
4124ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4134ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Handle an invalidation with version 1. Verify that the invalidator's
4144ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // highest handled invalidation version is updated accordingly.
4154ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  store->Store(device_policy_.policy(), 1);
4164760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com  invalidator->OnStoreLoaded(store);
4174ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, invalidator->highest_handled_invalidation_version());
4184ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4194760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com  // Log in as an affiliated user.
4204760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com  Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
4214760c563c3d41b97c1677454c9e700595c25a04bshannon.woods%transgaming.com@gtempaccount.com  ASSERT_TRUE(profile);
4225d601382b51c29d1670b58c01360416bd929842dGeoff Lang
4235d601382b51c29d1670b58c01360416bd929842dGeoff Lang  // Verify that the device-global invalidation service still exists.
4245d601382b51c29d1670b58c01360416bd929842dGeoff Lang  EXPECT_TRUE(GetDeviceInvalidationService());
425b8cabd593cf162be44fb1c0f3c8fbe72c1f45b75shannonwoods@chromium.org  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
42691fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang
42791fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  // Verify that a per-profile invalidation service has been created.
42891fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  invalidation::FakeInvalidationService* profile_invalidation_service =
42991fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang      GetProfileInvalidationService(profile);
430b8cabd593cf162be44fb1c0f3c8fbe72c1f45b75shannonwoods@chromium.org  ASSERT_TRUE(profile_invalidation_service);
43191fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
432b8cabd593cf162be44fb1c0f3c8fbe72c1f45b75shannonwoods@chromium.org
433b8cabd593cf162be44fb1c0f3c8fbe72c1f45b75shannonwoods@chromium.org  // Verify that an invalidator backed by the device-global invalidation service
434b8cabd593cf162be44fb1c0f3c8fbe72c1f45b75shannonwoods@chromium.org  // still exists.
4354ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetCloudPolicyInvalidator());
4364ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
4374ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4384ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the per-profile invalidation service has connected.
4394ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service->SetInvalidatorState(
4404ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::INVALIDATIONS_ENABLED);
4414ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4424ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidator has been destroyed.
4434ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetDeviceInvalidationService());
4444ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
4454ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
44691fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  // Verify that a per-profile invalidation service still exists.
44791fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  profile_invalidation_service = GetProfileInvalidationService(profile);
44891fa9ce47649057c5b15113ea31c5ed4b7bd8fceGeoff Lang  EXPECT_TRUE(profile_invalidation_service);
4494ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
4504ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4514ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the per-profile invalidation service
4524ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // has been created.
4534ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidator = GetCloudPolicyInvalidator();
454e5dcce75bb08208387723a4e0494ca19073e05a7shannon.woods%transgaming.com@gtempaccount.com  ASSERT_TRUE(invalidator);
4554ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(profile_invalidation_service, GetInvalidationService());
456e5dcce75bb08208387723a4e0494ca19073e05a7shannon.woods%transgaming.com@gtempaccount.com
457e5dcce75bb08208387723a4e0494ca19073e05a7shannon.woods%transgaming.com@gtempaccount.com  // Verify that the invalidator's highest handled invalidation version starts
458e5dcce75bb08208387723a4e0494ca19073e05a7shannon.woods%transgaming.com@gtempaccount.com  // out as one.
4594ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, invalidator->highest_handled_invalidation_version());
4604ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
4613c7fa226aeb81469b93142b54e339f13a46ab835Jamie Madill
4624ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// Verifies that when the per-profile invalidation service for an unaffiliated
4634ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// user connects, the current DeviceCloudPolicyInvalidator backed by a
4644ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// device-global invalidation service is not destroyed and replaced.
4659f0b42a6ab87ae2b2f065cf022497ac6deabf3fdJamie MadillTEST_F(DeviceCloudPolicyInvalidatorTest,
4664ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com       DoNotSwitchToUnaffiliatedProfileInvalidationService) {
4679f0b42a6ab87ae2b2f065cf022497ac6deabf3fdJamie Madill  // Verify that an invalidator backed by the device-global invalidation service
4684ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // is created when the service connects.
4694ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ConnectDeviceInvalidationService();
4704ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyInvalidator* invalidator = GetCloudPolicyInvalidator();
4714ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(invalidator);
4724ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
4734ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4744ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the invalidator's highest handled invalidation version starts
4754ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // out as zero.
476779aa2618c1333c034768f3a4ca7826a14753b60shannon.woods@transgaming.com  EXPECT_EQ(0, invalidator->highest_handled_invalidation_version());
4774ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4784ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Log in as an unaffiliated user.
479204677af271502bb1f91d4abd0cf8ab4ab5b49d0daniel@transgaming.com  Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID);
4804ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile);
4814ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4824ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidation service still exists.
4834ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
4849f0b42a6ab87ae2b2f065cf022497ac6deabf3fdJamie Madill  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
4854ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4864ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service has been created.
4874ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::FakeInvalidationService* profile_invalidation_service =
4884ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      GetProfileInvalidationService(profile);
4894ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_invalidation_service);
490ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang  EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount());
491779aa2618c1333c034768f3a4ca7826a14753b60shannon.woods@transgaming.com
4924ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the device-global invalidation service
4934ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // still exists.
4944ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetCloudPolicyInvalidator());
4954ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
4964ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
4974ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the per-profile invalidation service has connected.
4984ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service->SetInvalidatorState(
499ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang      syncer::INVALIDATIONS_ENABLED);
500ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang
501779aa2618c1333c034768f3a4ca7826a14753b60shannon.woods@transgaming.com  // Verify that the device-global invalidator still exists.
5024ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
5034ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
5044ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5054ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service still exists.
5064ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service = GetProfileInvalidationService(profile);
5074ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(profile_invalidation_service);
5084ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(0, GetProfileInvalidationServiceObserverCount());
5094ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5104ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the device-global invalidation service
5114ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // still exists.
5124ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetCloudPolicyInvalidator());
513ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
514ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang}
515779aa2618c1333c034768f3a4ca7826a14753b60shannon.woods@transgaming.com
5164ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// Verifies that when the per-profile invalidation service backing the current
5174ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// DeviceCloudPolicyInvalidator disconnects and no other connected invalidation
5184ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// service is available for use, a device-global invalidation service is
5194ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// created. Also verifies that when this service connects, a
5209f0b42a6ab87ae2b2f065cf022497ac6deabf3fdJamie Madill// DeviceCloudPolicyInvalidator backed by it is created and the highest handled
5214ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// invalidation version is preserved when switching invalidation services.
5224ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comTEST_F(DeviceCloudPolicyInvalidatorTest, SwitchToDeviceInvalidationService) {
5234ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyStore* store = static_cast<CloudPolicyStore*>(
5244ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      TestingBrowserProcess::GetGlobal()->platform_part()->
525ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang          browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()->
526ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang              device_store());
527779aa2618c1333c034768f3a4ca7826a14753b60shannon.woods@transgaming.com  ASSERT_TRUE(store);
5284ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5294ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Log in as an affiliated user.
5304ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
5314ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile);
5324ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5334ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a device-global invalidation service has been created.
5344ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
5354ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
5364ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5374ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service has been created.
5384ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::FakeInvalidationService* profile_invalidation_service =
5394ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      GetProfileInvalidationService(profile);
5404ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_invalidation_service);
5414ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
5424ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5434ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that no invalidator exists yet.
5444ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
5454ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
5464ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5474ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the per-profile invalidation service has connected.
5484ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service->SetInvalidatorState(
5494ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::INVALIDATIONS_ENABLED);
5504ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5514ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidator has been destroyed.
5524ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetDeviceInvalidationService());
5534ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
5544ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5554ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service still exists.
5564ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service = GetProfileInvalidationService(profile);
5574ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_invalidation_service);
5584ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
5594ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5604ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the per-profile invalidation service
5614ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // has been created.
5624ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyInvalidator* invalidator = GetCloudPolicyInvalidator();
5634ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(invalidator);
5644ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(profile_invalidation_service, GetInvalidationService());
5654ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5664ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the invalidator's highest handled invalidation version starts
5674ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // out as zero.
5684ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(0, invalidator->highest_handled_invalidation_version());
5694ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5704ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Handle an invalidation with version 1. Verify that the invalidator's
5714ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // highest handled invalidation version is updated accordingly.
5724ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  store->Store(device_policy_.policy(), 1);
5734ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidator->OnStoreLoaded(store);
5744ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, invalidator->highest_handled_invalidation_version());
5754ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5764ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the per-profile invalidation service has disconnected.
5774ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service->SetInvalidatorState(
5784ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::INVALIDATION_CREDENTIALS_REJECTED);
5794ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5804ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a device-global invalidation service has been created.
5814ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
5824ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
5834ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5844ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service still exists.
5854ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_invalidation_service = GetProfileInvalidationService(profile);
5864ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(profile_invalidation_service);
5874ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
5884ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5894ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the invalidator has been destroyed.
5904ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
5914ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
5924ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
5934ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the device-global invalidation service
5944ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // is created when the service connects.
5954ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ConnectDeviceInvalidationService();
5964ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidator = GetCloudPolicyInvalidator();
5974ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(invalidator);
5984ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(GetDeviceInvalidationService(), GetInvalidationService());
5994ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6004ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the invalidator's highest handled invalidation version starts
6014ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // out as one.
6024ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, invalidator->highest_handled_invalidation_version());
6034ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com}
6044ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6054ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// Verifies that when the per-profile invalidation service backing the current
6064ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// DeviceCloudPolicyInvalidator disconnects and another connected per-profile
6074ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// invalidation service is available for use, a DeviceCloudPolicyInvalidator
6084ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// backed by that service is created. Also verifies that the highest handled
6094ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com// invalidation version is preserved when switching invalidation services.
6104ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.comTEST_F(DeviceCloudPolicyInvalidatorTest,
6114ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com       SwitchBetweenAffiliatedProfileInvalidationServices) {
6124ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyStore* store = static_cast<CloudPolicyStore*>(
6134ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      TestingBrowserProcess::GetGlobal()->platform_part()->
6144ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com          browser_policy_connector_chromeos()->GetDeviceCloudPolicyManager()->
6154ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com              device_store());
6164ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(store);
6174ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6184ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a device-global invalidation service has been created.
6194ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
6204ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
6214ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6224ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that no invalidator exists yet.
6234ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
6244ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
6254ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6264ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Log in as a first affiliated user.
6274ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  Profile* profile_1 = LogInAndReturnProfile(kAffiliatedUserID1);
6284ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_1);
6294ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6304ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidation service still exists.
6314ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetDeviceInvalidationService());
6324ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(HasDeviceInvalidationServiceObserver());
6334ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6344ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service has been created for the
6354ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // first user.
6364ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::FakeInvalidationService* profile_1_invalidation_service =
6374ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      GetProfileInvalidationService(profile_1);
6384ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_1_invalidation_service);
6394ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
6404ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6414ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that no invalidator has been created.
6424ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetCloudPolicyInvalidator());
6434ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetInvalidationService());
6444ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6454ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the first user's per-profile invalidation service has
6464ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // connected.
6474ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_1_invalidation_service->SetInvalidatorState(
6484ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      syncer::INVALIDATIONS_ENABLED);
6494ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6504ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidator has been destroyed.
6514ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetDeviceInvalidationService());
6524ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
6534ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6544ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service still exists for the first
6554ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // user.
6564ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
6574ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(profile_1_invalidation_service);
6584ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, GetProfileInvalidationServiceObserverCount());
6594ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6604ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the first user's per-profile
6614ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // invalidation service has been created.
6624ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  CloudPolicyInvalidator* invalidator = GetCloudPolicyInvalidator();
6634ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(invalidator);
6644ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService());
6654ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6664ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the invalidator's highest handled invalidation version starts
6674ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // out as zero.
6684ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(0, invalidator->highest_handled_invalidation_version());
6694ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6704ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Handle an invalidation with version 1. Verify that the invalidator's
6714ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // highest handled invalidation version is updated accordingly.
6724ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  store->Store(device_policy_.policy(), 1);
6734ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidator->OnStoreLoaded(store);
6744ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(1, invalidator->highest_handled_invalidation_version());
6754ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6764ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Log in as a second affiliated user.
6774ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  Profile* profile_2 = LogInAndReturnProfile(kAffiliatedUserID2);
6784ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_2);
6794ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6804ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidator still does not exist.
6814ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetDeviceInvalidationService());
6824ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
6834ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6844ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that a per-profile invalidation service still exists for the first
6854ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // user and one has been created for the second user.
6864ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
6874ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(profile_1_invalidation_service);
6884ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  invalidation::FakeInvalidationService* profile_2_invalidation_service =
6894ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com      GetProfileInvalidationService(profile_2);
6904ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  ASSERT_TRUE(profile_2_invalidation_service);
6914ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
6924ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6934ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that an invalidator backed by the first user's per-profile
6944ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // invalidation service still exists.
6954ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_TRUE(GetCloudPolicyInvalidator());
6964ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService());
6974ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
6984ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Indicate that the second user's per-profile invalidation service has
6994ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // connected.
700ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang  profile_2_invalidation_service->SetInvalidatorState(
701ea2286352f5eba47dfaa3e9ab418bbacfd1ba8a1Geoff Lang      syncer::INVALIDATIONS_ENABLED);
7024ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com
7034ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  // Verify that the device-global invalidator still does not exist.
7044ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(GetDeviceInvalidationService());
7054ba2406781174627e817ca547a6e9f6b1a196ff0daniel@transgaming.com  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
70634256ed8593f6d7fff6b7536979c95afc28ae2d3Geoff Lang
707  // Verify that per-profile invalidation services still exist for both users.
708  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
709  ASSERT_TRUE(profile_1_invalidation_service);
710  profile_2_invalidation_service = GetProfileInvalidationService(profile_2);
711  EXPECT_TRUE(profile_2_invalidation_service);
712  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
713
714  // Verify that an invalidator backed by the first user's per-profile
715  // invalidation service still exists.
716  EXPECT_TRUE(GetCloudPolicyInvalidator());
717  EXPECT_EQ(profile_1_invalidation_service, GetInvalidationService());
718
719  // Indicate that the first user's per-profile invalidation service has
720  // disconnected.
721  profile_1_invalidation_service->SetInvalidatorState(
722      syncer::INVALIDATION_CREDENTIALS_REJECTED);
723
724  // Verify that the device-global invalidator still does not exist.
725  EXPECT_FALSE(GetDeviceInvalidationService());
726  EXPECT_FALSE(HasDeviceInvalidationServiceObserver());
727
728  // Verify that per-profile invalidation services still exist for both users.
729  profile_1_invalidation_service = GetProfileInvalidationService(profile_1);
730  EXPECT_TRUE(profile_1_invalidation_service);
731  profile_2_invalidation_service = GetProfileInvalidationService(profile_2);
732  ASSERT_TRUE(profile_2_invalidation_service);
733  EXPECT_EQ(2, GetProfileInvalidationServiceObserverCount());
734
735  // Verify that an invalidator backed by the second user's per-profile
736  // invalidation service has been created.
737  invalidator = GetCloudPolicyInvalidator();
738  ASSERT_TRUE(invalidator);
739  EXPECT_EQ(profile_2_invalidation_service, GetInvalidationService());
740
741  // Verify that the invalidator's highest handled invalidation version starts
742  // out as one.
743  EXPECT_EQ(1, invalidator->highest_handled_invalidation_version());
744}
745
746}  // namespace policy
747