1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
6
7 #include "base/callback.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/sequenced_task_runner.h"
11 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
12 #include "components/policy/core/common/cloud/mock_user_cloud_policy_store.h"
13 #include "components/policy/core/common/external_data_fetcher.h"
14 #include "components/policy/core/common/mock_configuration_policy_provider.h"
15 #include "components/policy/core/common/schema_registry.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace em = enterprise_management;
21
22 using testing::AnyNumber;
23 using testing::AtLeast;
24 using testing::Mock;
25 using testing::_;
26
27 namespace policy {
28 namespace {
29
30 class UserCloudPolicyManagerTest : public testing::Test {
31 protected:
UserCloudPolicyManagerTest()32 UserCloudPolicyManagerTest() : store_(NULL) {}
33
SetUp()34 virtual void SetUp() OVERRIDE {
35 // Set up a policy map for testing.
36 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
37 base::Value::CreateStringValue("value"), NULL);
38 expected_bundle_.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
39 .CopyFrom(policy_map_);
40 }
41
TearDown()42 virtual void TearDown() OVERRIDE {
43 if (manager_) {
44 manager_->RemoveObserver(&observer_);
45 manager_->Shutdown();
46 }
47 }
48
CreateManager()49 void CreateManager() {
50 store_ = new MockUserCloudPolicyStore();
51 EXPECT_CALL(*store_, Load());
52 manager_.reset(new UserCloudPolicyManager(
53 scoped_ptr<UserCloudPolicyStore>(store_),
54 base::FilePath(),
55 scoped_ptr<CloudExternalDataManager>(),
56 loop_.message_loop_proxy(),
57 loop_.message_loop_proxy(),
58 loop_.message_loop_proxy()));
59 manager_->Init(&schema_registry_);
60 manager_->AddObserver(&observer_);
61 Mock::VerifyAndClearExpectations(store_);
62 }
63
64 // Required by the refresh scheduler that's created by the manager.
65 base::MessageLoop loop_;
66
67 // Convenience policy objects.
68 PolicyMap policy_map_;
69 PolicyBundle expected_bundle_;
70
71 // Policy infrastructure.
72 SchemaRegistry schema_registry_;
73 MockConfigurationPolicyObserver observer_;
74 MockUserCloudPolicyStore* store_; // Not owned.
75 scoped_ptr<UserCloudPolicyManager> manager_;
76
77 private:
78 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest);
79 };
80
TEST_F(UserCloudPolicyManagerTest,DisconnectAndRemovePolicy)81 TEST_F(UserCloudPolicyManagerTest, DisconnectAndRemovePolicy) {
82 // Load policy, make sure it goes away when DisconnectAndRemovePolicy() is
83 // called.
84 CreateManager();
85 store_->policy_map_.CopyFrom(policy_map_);
86 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
87 store_->NotifyStoreLoaded();
88 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
89 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
90 EXPECT_CALL(*store_, Clear());
91 manager_->DisconnectAndRemovePolicy();
92 EXPECT_FALSE(manager_->core()->service());
93 }
94
95 } // namespace
96 } // namespace policy
97