Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(637)

Unified Diff: chrome/browser/chromeos/policy/affiliated_invalidation_service_provider_unittest.cc

Issue 828953004: Add AffiliatedInvalidationServiceProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Trimmed unit tests to only check externally visible behavior only. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/policy/affiliated_invalidation_service_provider_unittest.cc
diff --git a/chrome/browser/chromeos/policy/affiliated_invalidation_service_provider_unittest.cc b/chrome/browser/chromeos/policy/affiliated_invalidation_service_provider_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..4c6ce84e59d5291fabc03ff04f143855593d55f7
--- /dev/null
+++ b/chrome/browser/chromeos/policy/affiliated_invalidation_service_provider_unittest.cc
@@ -0,0 +1,651 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/policy/affiliated_invalidation_service_provider.h"
+
+#include <string>
+
+#include "chrome/browser/chrome_notification_types.h"
+#include "chrome/browser/chromeos/login/users/fake_user_manager.h"
+#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
+#include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
+#include "chrome/browser/chromeos/settings/cros_settings.h"
+#include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h"
+#include "chrome/browser/chromeos/settings/device_settings_service.h"
+#include "chrome/browser/invalidation/fake_invalidation_service.h"
+#include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/test/base/testing_browser_process.h"
+#include "chrome/test/base/testing_profile_manager.h"
+#include "chromeos/cryptohome/system_salt_getter.h"
+#include "chromeos/dbus/dbus_thread_manager.h"
+#include "components/invalidation/invalidation_service.h"
+#include "components/invalidation/invalidator_state.h"
+#include "components/invalidation/profile_invalidation_provider.h"
+#include "components/invalidation/ticl_invalidation_service.h"
+#include "components/keyed_service/core/keyed_service.h"
+#include "components/policy/core/common/cloud/cloud_policy_constants.h"
+#include "content/public/browser/browser_context.h"
+#include "content/public/browser/notification_details.h"
+#include "content/public/browser/notification_service.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::_;
+using testing::Mock;
+using testing::Sequence;
+
+namespace policy {
+
+namespace {
+
+const char kAffiliatedUserID1[] = "test_1@example.com";
+const char kAffiliatedUserID2[] = "test_2@example.com";
+const char kUnaffiliatedUserID[] = "test@other_domain.test";
+
+KeyedService* BuildProfileInvalidationProvider(
+ content::BrowserContext* context) {
+ scoped_ptr<invalidation::FakeInvalidationService> invalidation_service(
+ new invalidation::FakeInvalidationService);
+ invalidation_service->SetInvalidatorState(
+ syncer::TRANSIENT_INVALIDATION_ERROR);
+ return new invalidation::ProfileInvalidationProvider(
+ invalidation_service.Pass());
+}
+
+} // namespace
+
+class MockConsumer : public AffiliatedInvalidationServiceProvider::Consumer {
+ public:
+ MockConsumer();
+ ~MockConsumer() override;
+
+ MOCK_METHOD1(OnInvalidationServiceSet,
+ void(invalidation::InvalidationService*));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockConsumer);
+};
+
+class AffiliatedInvalidationServiceProviderTest : public testing::Test {
+ public:
+ AffiliatedInvalidationServiceProviderTest();
+
+ // testing::Test:
+ virtual void SetUp() override;
+ virtual void TearDown() override;
+
+ // Ownership is not passed. The Profile is owned by the global ProfileManager.
+ Profile* LogInAndReturnProfile(const std::string& user_id);
+
+ invalidation::FakeInvalidationService* GetProfileInvalidationService(
+ Profile* profile);
+
+ protected:
+ scoped_ptr<AffiliatedInvalidationServiceProvider> provider_;
+
+ private:
+ content::TestBrowserThreadBundle thread_bundle_;
+ chromeos::FakeUserManager* fake_user_manager_;
+ chromeos::ScopedUserManagerEnabler user_manager_enabler_;
+ ScopedStubEnterpriseInstallAttributes install_attributes_;
+ scoped_ptr<chromeos::ScopedTestDeviceSettingsService>
+ test_device_settings_service_;
+ scoped_ptr<chromeos::ScopedTestCrosSettings> test_cros_settings_;
+ TestingProfileManager profile_manager_;
+};
+
+MockConsumer::MockConsumer() {
+}
+
+MockConsumer::~MockConsumer() {
+}
+
+AffiliatedInvalidationServiceProviderTest::
+AffiliatedInvalidationServiceProviderTest()
+ : fake_user_manager_(new chromeos::FakeUserManager),
+ user_manager_enabler_(fake_user_manager_),
+ install_attributes_("example.com",
+ "user@example.com",
+ "device_id",
+ DEVICE_MODE_ENTERPRISE),
+ profile_manager_(TestingBrowserProcess::GetGlobal()) {
+}
+
+void AffiliatedInvalidationServiceProviderTest::SetUp() {
+ chromeos::SystemSaltGetter::Initialize();
+ chromeos::DBusThreadManager::Initialize();
+ chromeos::DeviceOAuth2TokenServiceFactory::Initialize();
+ ASSERT_TRUE(profile_manager_.SetUp());
+
+ test_device_settings_service_.reset(new
+ chromeos::ScopedTestDeviceSettingsService);
+ test_cros_settings_.reset(new chromeos::ScopedTestCrosSettings);
+
+ invalidation::ProfileInvalidationProviderFactory::GetInstance()->
+ RegisterTestingFactory(BuildProfileInvalidationProvider);
+
+ provider_.reset(new AffiliatedInvalidationServiceProvider);
+}
+
+void AffiliatedInvalidationServiceProviderTest::TearDown() {
+ provider_.reset();
+
+ invalidation::ProfileInvalidationProviderFactory::GetInstance()->
+ RegisterTestingFactory(nullptr);
+ chromeos::DeviceOAuth2TokenServiceFactory::Shutdown();
+ chromeos::DBusThreadManager::Shutdown();
+ chromeos::SystemSaltGetter::Shutdown();
+}
+
+Profile* AffiliatedInvalidationServiceProviderTest::LogInAndReturnProfile(
+ const std::string& user_id) {
+ fake_user_manager_->AddUser(user_id);
+ Profile* profile = profile_manager_.CreateTestingProfile(user_id);
+ content::NotificationService::current()->Notify(
+ chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
+ content::NotificationService::AllSources(),
+ content::Details<Profile>(profile));
+ return profile;
+}
+
+invalidation::FakeInvalidationService*
+AffiliatedInvalidationServiceProviderTest::GetProfileInvalidationService(
+ Profile* profile) {
+ invalidation::ProfileInvalidationProvider* invalidation_provider =
+ static_cast<invalidation::ProfileInvalidationProvider*>(
+ invalidation::ProfileInvalidationProviderFactory::GetInstance()->
+ GetServiceForBrowserContext(profile, false));
+ if (!invalidation_provider)
+ return nullptr;
+ return static_cast<invalidation::FakeInvalidationService*>(
+ invalidation_provider->GetInvalidationService());
+}
+
+// No consumers are registered with the AffiliatedInvalidationServiceProvider.
+// Verifies that no device-global invalidation service is created, whether an
+// affiliated user is logged in or not.
+TEST_F(AffiliatedInvalidationServiceProviderTest, NoConsumers) {
+ // Verify that no device-global invalidation service has been created.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Log in as an affiliated user.
+ EXPECT_TRUE(LogInAndReturnProfile(kAffiliatedUserID1));
+
+ // Verify that no device-global invalidation service has been created.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider.
+// Verifies that when no per-profile invalidation service belonging to an
+// affiliated user is available, a device-global invalidation service is
+// created. Further verifies that when the device-global invalidation service
+// connects, it is made available to the consumer.
+TEST_F(AffiliatedInvalidationServiceProviderTest,
+ UseDeviceInvalidationService) {
+ // Register a consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ invalidation::TiclInvalidationService* device_invalidation_service =
+ provider_->GetDeviceInvalidationServiceForTest();
+ ASSERT_TRUE(device_invalidation_service);
+
+ // Indicate that the device-global invalidation service has connected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
Mattias Nissler (ping if slow) 2015/01/16 13:24:29 Are these expectations actually necessary, i.e. do
bartfab (slow) 2015/01/20 13:22:47 Regular Mock just prints a warning that there was
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service))
+ .Times(1);
+ device_invalidation_service->OnInvalidatorStateChange(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Indicate that the device-global invalidation service has disconnected.
+ // Verify that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)).Times(1);
+ device_invalidation_service->OnInvalidatorStateChange(
+ syncer::INVALIDATION_CREDENTIALS_REJECTED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Unregister the consumer.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->UnregisterConsumer(&consumer);
Mattias Nissler (ping if slow) 2015/01/16 13:24:29 Let's put a Mock::VerifyAndClearExpectations here
bartfab (slow) 2015/01/20 13:22:47 Done.
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider.
+// Verifies that when a per-profile invalidation service belonging to an
+// affiliated user connects, it is made available to the consumer.
+TEST_F(AffiliatedInvalidationServiceProviderTest,
+ UseAffiliatedProfileInvalidationService) {
+ // Register a consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Log in as an affiliated user.
+ Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
+ EXPECT_TRUE(profile);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a per-profile invalidation service has been created.
+ invalidation::FakeInvalidationService* profile_invalidation_service =
+ GetProfileInvalidationService(profile);
+ ASSERT_TRUE(profile_invalidation_service);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the per-profile invalidation service has connected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service))
+ .Times(1);
+ profile_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that the device-global invalidation service has been destroyed.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the per-profile invalidation service has disconnected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)).Times(1);
+ profile_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATION_CREDENTIALS_REJECTED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Unregister the consumer.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->UnregisterConsumer(&consumer);
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider.
+// Verifies that when a per-profile invalidation service belonging to an
+// unaffiliated user connects, it is ignored.
+TEST_F(AffiliatedInvalidationServiceProviderTest,
+ DoNotUseUnaffiliatedProfileInvalidationService) {
+ // Register a consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Log in as an unaffiliated user.
+ Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID);
+ EXPECT_TRUE(profile);
+
+ // Verify that a per-profile invalidation service has been created.
+ invalidation::FakeInvalidationService* profile_invalidation_service =
+ GetProfileInvalidationService(profile);
+ ASSERT_TRUE(profile_invalidation_service);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the per-profile invalidation service has connected. Verify
+ // that the consumer is not called back.
+ profile_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATIONS_ENABLED);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Unregister the consumer.
+ provider_->UnregisterConsumer(&consumer);
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider. A
+// device-global invalidation service exists, is connected and is made available
+// to the consumer. Verifies that when a per-profile invalidation service
+// belonging to an affiliated user connects, it is made available to the
+// consumer instead and the device-global invalidation service is destroyed.
+TEST_F(AffiliatedInvalidationServiceProviderTest,
+ SwitchToAffiliatedProfileInvalidationService) {
+ // Register a consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ invalidation::TiclInvalidationService* device_invalidation_service =
+ provider_->GetDeviceInvalidationServiceForTest();
+ ASSERT_TRUE(device_invalidation_service);
+
+ // Indicate that the device-global invalidation service has connected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service))
+ .Times(1);
+ device_invalidation_service->OnInvalidatorStateChange(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Log in as an affiliated user.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
+ EXPECT_TRUE(profile);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a per-profile invalidation service has been created.
+ invalidation::FakeInvalidationService* profile_invalidation_service =
+ GetProfileInvalidationService(profile);
+ ASSERT_TRUE(profile_invalidation_service);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the per-profile invalidation service has connected. Verify
+ // that the consumer is informed that the device-global invalidation service
+ // is no longer to be used and the per-profile invalidation service is to be
+ // used instead.
+ Sequence sequence;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr))
+ .Times(1)
+ .InSequence(sequence);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service))
+ .Times(1)
+ .InSequence(sequence);
+ profile_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that the device-global invalidation service has been destroyed.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Unregister the consumer.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->UnregisterConsumer(&consumer);
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider. A
+// device-global invalidation service exists, is connected and is made available
+// to the consumer. Verifies that when a per-profile invalidation service
Mattias Nissler (ping if slow) 2015/01/16 13:24:29 This and all tests below have a common setup seque
bartfab (slow) 2015/01/20 13:22:47 Done.
+// belonging to an unaffiliated user connects, it is ignored and the
+// device-global invalidation service continues to be made available to the
+// consumer.
+TEST_F(AffiliatedInvalidationServiceProviderTest,
+ DoNotSwitchToUnaffiliatedProfileInvalidationService) {
+ // Register a consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ invalidation::TiclInvalidationService* device_invalidation_service =
+ provider_->GetDeviceInvalidationServiceForTest();
+ ASSERT_TRUE(device_invalidation_service);
+
+ // Indicate that the device-global invalidation service has connected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service))
+ .Times(1);
+ device_invalidation_service->OnInvalidatorStateChange(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Log in as an unaffiliated user.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ Profile* profile = LogInAndReturnProfile(kUnaffiliatedUserID);
+ EXPECT_TRUE(profile);
+
+ // Verify that a per-profile invalidation service has been created.
+ invalidation::FakeInvalidationService* profile_invalidation_service =
+ GetProfileInvalidationService(profile);
+ ASSERT_TRUE(profile_invalidation_service);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the per-profile invalidation service has connected. Verify
+ // that the consumer is not called back.
+ profile_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATIONS_ENABLED);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Unregister the consumer.
+ provider_->UnregisterConsumer(&consumer);
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider. A
+// per-profile invalidation service belonging to an affiliated user exists, is
+// connected and is made available to the consumer. Verifies that when the
+// per-profile invalidation service disconnects, a device-global invalidation
+// service is created. Further verifies that when the device-global invalidation
+// service connects, it is made available to the consumer.
+TEST_F(AffiliatedInvalidationServiceProviderTest,
+ SwitchToDeviceInvalidationService) {
+ // Register a consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Log in as an affiliated user.
+ Profile* profile = LogInAndReturnProfile(kAffiliatedUserID1);
+ EXPECT_TRUE(profile);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a per-profile invalidation service has been created.
+ invalidation::FakeInvalidationService* profile_invalidation_service =
+ GetProfileInvalidationService(profile);
+ ASSERT_TRUE(profile_invalidation_service);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the per-profile invalidation service has connected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(profile_invalidation_service))
+ .Times(1);
+ profile_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that the device-global invalidation service has been destroyed.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the per-profile invalidation service has disconnected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr)).Times(1);
+ profile_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATION_CREDENTIALS_REJECTED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ invalidation::TiclInvalidationService* device_invalidation_service =
+ provider_->GetDeviceInvalidationServiceForTest();
+ ASSERT_TRUE(device_invalidation_service);
+
+ // Indicate that the device-global invalidation service has connected. Verify
+ // that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(device_invalidation_service))
+ .Times(1);
+ device_invalidation_service->OnInvalidatorStateChange(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Unregister the consumer.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->UnregisterConsumer(&consumer);
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider. A
+// per-profile invalidation service belonging to a first affiliated user exists,
+// is connected and is made available to the consumer. A per-profile
+// invalidation service belonging to a second affiliated user also exists and is
+// connected. Verifies that when the per-profile invalidation service belonging
+// to the first user disconnects, the per-profile invalidation service belonging
+// to the second user is made available to the consumer instead.
+TEST_F(AffiliatedInvalidationServiceProviderTest,
+ SwitchBetweenAffiliatedProfileInvalidationServices) {
+ // Register a consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer);
+
+ // Verify that a device-global invalidation service has been created.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Log in as a first affiliated user.
+ Profile* profile_1 = LogInAndReturnProfile(kAffiliatedUserID1);
+ EXPECT_TRUE(profile_1);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that a per-profile invalidation service for the first user has been
+ // created.
+ invalidation::FakeInvalidationService* profile_1_invalidation_service =
+ GetProfileInvalidationService(profile_1);
+ ASSERT_TRUE(profile_1_invalidation_service);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Indicate that the first user's per-profile invalidation service has
+ // connected. Verify that the consumer is informed about this.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer,
+ OnInvalidationServiceSet(profile_1_invalidation_service))
+ .Times(1);
+ profile_1_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that the device-global invalidation service has been destroyed.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Log in as a second affiliated user.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ Profile* profile_2 = LogInAndReturnProfile(kAffiliatedUserID2);
+ EXPECT_TRUE(profile_2);
+
+ // Verify that the device-global invalidation service still does not exist.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Verify that a per-profile invalidation service for the second user has been
+ // created.
+ invalidation::FakeInvalidationService* profile_2_invalidation_service =
+ GetProfileInvalidationService(profile_2);
+ ASSERT_TRUE(profile_2_invalidation_service);
+
+ // Indicate that the second user's per-profile invalidation service has
+ // connected. Verify that the consumer is not called back.
+ profile_2_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Indicate that the first user's per-profile invalidation service has
+ // disconnected. Verify that the consumer is informed that the first user's
+ // per-profile invalidation service is no longer to be used and the second
+ // user's per-profile invalidation service is to be used instead.
+ Sequence sequence;
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(nullptr))
+ .Times(1)
+ .InSequence(sequence);
+ EXPECT_CALL(consumer,
+ OnInvalidationServiceSet(profile_2_invalidation_service))
+ .Times(1)
+ .InSequence(sequence);
+ profile_1_invalidation_service->SetInvalidatorState(
+ syncer::INVALIDATION_CREDENTIALS_REJECTED);
+ Mock::VerifyAndClearExpectations(&consumer);
+
+ // Verify that the device-global invalidation service still does not exist.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Unregister the consumer.
+ EXPECT_CALL(consumer, OnInvalidationServiceSet(_)).Times(0);
+ provider_->UnregisterConsumer(&consumer);
+}
+
+// A consumer is registered with the AffiliatedInvalidationServiceProvider. A
+// device-global invalidation service exists, is connected and is made available
+// to the consumer. Verifies that when a second consumer registers, the
+// device-global invalidation service is made available to it as well. Further
+// verifies that when the first consumer unregisters, the device-global
+// invalidation service is not destroyed and remains available to the second
+// consumer. Further verifies that when the second consumer also unregisters,
+// the device-global invalidation service is destroyed.
+TEST_F(AffiliatedInvalidationServiceProviderTest, MultipleConsumers) {
+ // Register a first consumer. Verify that the consumer is not called back
+ // immediately as no connected invalidation service exists yet.
+ MockConsumer consumer_1;
+ EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0);
+ provider_->RegisterConsumer(&consumer_1);
+ Mock::VerifyAndClearExpectations(&consumer_1);
+
+ // Verify that a device-global invalidation service has been created.
+ invalidation::TiclInvalidationService* device_invalidation_service =
+ provider_->GetDeviceInvalidationServiceForTest();
+ ASSERT_TRUE(device_invalidation_service);
+
+ // Indicate that the device-global invalidation service has connected. Verify
+ // that the first consumer is informed about this.
+ EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer_1, OnInvalidationServiceSet(device_invalidation_service))
+ .Times(1);
+ device_invalidation_service->OnInvalidatorStateChange(
+ syncer::INVALIDATIONS_ENABLED);
+ Mock::VerifyAndClearExpectations(&consumer_1);
+ EXPECT_CALL(consumer_1, OnInvalidationServiceSet(_)).Times(0);
+
+ // Register a second consumer. Verify that the consumer is called back
+ // immediately as a connected invalidation service is available.
+ MockConsumer consumer_2;
+ EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0);
+ EXPECT_CALL(consumer_2, OnInvalidationServiceSet(device_invalidation_service))
+ .Times(1);
+ provider_->RegisterConsumer(&consumer_2);
+ Mock::VerifyAndClearExpectations(&consumer_2);
Mattias Nissler (ping if slow) 2015/01/16 13:24:29 nit: Put a blank line here, the expectation in the
bartfab (slow) 2015/01/20 13:22:47 Done.
+ EXPECT_CALL(consumer_2, OnInvalidationServiceSet(_)).Times(0);
+
+ // Unregister the first consumer.
+ provider_->UnregisterConsumer(&consumer_1);
+
+ // Verify that the device-global invalidation service still exists.
+ EXPECT_TRUE(provider_->GetDeviceInvalidationServiceForTest());
+
+ // Unregister the second consumer.
+ provider_->UnregisterConsumer(&consumer_2);
+
+ // Verify that the device-global invalidation service has been destroyed.
+ EXPECT_FALSE(provider_->GetDeviceInvalidationServiceForTest());
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698