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

Unified Diff: chrome/browser/policy/cloud_policy_service_unittest.cc

Issue 10092010: Add CloudPolicyService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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/policy/cloud_policy_service_unittest.cc
diff --git a/chrome/browser/policy/cloud_policy_service_unittest.cc b/chrome/browser/policy/cloud_policy_service_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5be97a2ce28fbb2f24451c8f0012d3b84c38e537
--- /dev/null
+++ b/chrome/browser/policy/cloud_policy_service_unittest.cc
@@ -0,0 +1,198 @@
+// Copyright (c) 2012 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/policy/cloud_policy_service.h"
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "chrome/browser/policy/mock_cloud_policy_client.h"
+#include "chrome/browser/policy/proto/device_management_backend.pb.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace em = enterprise_management;
+
+using testing::_;
+
+namespace policy {
+
+namespace {
+
+class MockCloudPolicyStore : public CloudPolicyStore {
+ public:
+ MockCloudPolicyStore();
+
+ MOCK_METHOD1(Store, void(const em::PolicyFetchResponse&));
+ MOCK_METHOD0(Load, void(void));
+
+ // Publish the protected members.
+ using CloudPolicyStore::NotifyPolicyLoaded;
+ using CloudPolicyStore::NotifyStoreError;
+
+ using CloudPolicyStore::policy_map_;
+ using CloudPolicyStore::policy_;
+ using CloudPolicyStore::status_;
Joao da Silva 2012/04/17 00:30:58 Now that I see this, this is a nice way to create
+};
+
+MockCloudPolicyStore::MockCloudPolicyStore() {}
+
+} // namespace
+
+class CloudPolicyServiceTest : public testing::Test {
+ public:
+ CloudPolicyServiceTest()
+ : client_(new MockCloudPolicyClient),
Joao da Silva 2012/04/17 00:30:58 indent
Mattias Nissler (ping if slow) 2012/05/22 14:14:26 Done.
+ store_(new MockCloudPolicyStore),
+ service_(scoped_ptr<CloudPolicyClient>(client_),
+ scoped_ptr<CloudPolicyStore>(store_)) {}
+
+ MOCK_METHOD0(OnPolicyRefresh, void(void));
+
+ protected:
+ MockCloudPolicyClient* client_;
+ MockCloudPolicyStore* store_;
+ CloudPolicyService service_;
+};
+
+MATCHER_P(ProtoMatches, proto, "") {
+ return arg.SerializePartialAsString() == proto.SerializePartialAsString();
+}
+
+TEST_F(CloudPolicyServiceTest, ManagedByEmptyPolicy) {
+ EXPECT_EQ("", service_.ManagedBy());
+}
+
+TEST_F(CloudPolicyServiceTest, ManagedByValidPolicy) {
+ store_->policy_.set_username("user@example.com");
+ EXPECT_EQ("example.com", service_.ManagedBy());
+}
+
+TEST_F(CloudPolicyServiceTest, PolicyUpdateSuccess) {
+ em::PolicyFetchResponse policy;
+ policy.set_policy_data("fake policy");
+ client_->SetPolicy(policy);
+ EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1);
+ client_->NotifyPolicyFetched();
+}
+
+TEST_F(CloudPolicyServiceTest, PolicyUpdateClientFailure) {
+ client_->SetStatus(DM_STATUS_REQUEST_FAILED);
+ EXPECT_CALL(*store_, Store(_)).Times(0);
+ client_->NotifyPolicyFetched();
+}
+
+TEST_F(CloudPolicyServiceTest, RefreshPolicySuccess) {
+ testing::InSequence seq;
+
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
+ client_->SetDMToken("fake token");
+
+ // Trigger a fetch on the client.
+ EXPECT_CALL(*client_, FetchPolicy()).Times(1);
+ service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
+ base::Unretained(this)));
+
+ // Client responds, push policy to store.
+ em::PolicyFetchResponse policy;
+ policy.set_policy_data("fake policy");
+ client_->SetPolicy(policy);
+ EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1);
+ client_->NotifyPolicyFetched();
+
+ // Store reloads policy, callback gets triggered.
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
+ store_->NotifyPolicyLoaded();
+}
+
+TEST_F(CloudPolicyServiceTest, RefreshPolicyNotRegistered) {
+ // Clear the token so the client is not registered.
+ client_->SetDMToken("");
+
+ EXPECT_CALL(*client_, FetchPolicy()).Times(0);
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
+ service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
+ base::Unretained(this)));
+}
+
+TEST_F(CloudPolicyServiceTest, RefreshPolicyClientError) {
+ testing::InSequence seq;
+
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
+ client_->SetDMToken("fake token");
+
+ // Trigger a fetch on the client.
+ EXPECT_CALL(*client_, FetchPolicy()).Times(1);
+ service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
+ base::Unretained(this)));
+
+ // Client responds with an error, which should trigger the callback.
+ client_->SetStatus(DM_STATUS_REQUEST_FAILED);
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
+ client_->NotifyClientError();
+}
+
+TEST_F(CloudPolicyServiceTest, RefreshPolicyStoreError) {
+ testing::InSequence seq;
+
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
+ client_->SetDMToken("fake token");
+
+ // Trigger a fetch on the client.
+ EXPECT_CALL(*client_, FetchPolicy()).Times(1);
+ service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
+ base::Unretained(this)));
+
+ // Client responds, push policy to store.
+ em::PolicyFetchResponse policy;
+ policy.set_policy_data("fake policy");
+ client_->SetPolicy(policy);
+ EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1);
+ client_->NotifyPolicyFetched();
+
+ // Store fails, which should trigger the callback.
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(1);
+ store_->NotifyStoreError();
+}
+
+TEST_F(CloudPolicyServiceTest, RefreshPolicyConcurrent) {
+ testing::InSequence seq;
+
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
+ client_->SetDMToken("fake token");
+
+ // Trigger a fetch on the client.
+ EXPECT_CALL(*client_, FetchPolicy()).Times(1);
+ service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
+ base::Unretained(this)));
+
+ // Triggering another policy refresh should generate a new fetch request.
+ EXPECT_CALL(*client_, FetchPolicy()).Times(1);
+ service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
+ base::Unretained(this)));
+
+ // Client responds, push policy to store.
+ em::PolicyFetchResponse policy;
+ policy.set_policy_data("fake policy");
+ client_->SetPolicy(policy);
+ EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1);
+ client_->NotifyPolicyFetched();
+
+ // Trigger another policy fetch.
+ EXPECT_CALL(*client_, FetchPolicy()).Times(1);
+ service_.RefreshPolicy(base::Bind(&CloudPolicyServiceTest::OnPolicyRefresh,
+ base::Unretained(this)));
+
+ // The store finishing the first load should not generate callbacks.
+ store_->NotifyPolicyLoaded();
+
+ // Second policy fetch finishes.
+ EXPECT_CALL(*store_, Store(ProtoMatches(policy))).Times(1);
+ client_->NotifyPolicyFetched();
+
+ // Corresponding store operation finishes, all _three_ callbacks fire.
+ EXPECT_CALL(*this, OnPolicyRefresh()).Times(3);
+ store_->NotifyPolicyLoaded();
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698