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
20namespace em = enterprise_management;
21
22using testing::AnyNumber;
23using testing::AtLeast;
24using testing::Mock;
25using testing::_;
26
27namespace policy {
28namespace {
29
30class UserCloudPolicyManagerTest : public testing::Test {
31 protected:
32  UserCloudPolicyManagerTest() : store_(NULL) {}
33
34  virtual void SetUp() OVERRIDE {
35    // Set up a policy map for testing.
36    policy_map_.Set("key",
37                    POLICY_LEVEL_MANDATORY,
38                    POLICY_SCOPE_USER,
39                    new base::StringValue("value"),
40                    NULL);
41    expected_bundle_.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
42        .CopyFrom(policy_map_);
43  }
44
45  virtual void TearDown() OVERRIDE {
46    if (manager_) {
47      manager_->RemoveObserver(&observer_);
48      manager_->Shutdown();
49    }
50  }
51
52  void CreateManager() {
53    store_ = new MockUserCloudPolicyStore();
54    EXPECT_CALL(*store_, Load());
55    manager_.reset(new UserCloudPolicyManager(
56        scoped_ptr<UserCloudPolicyStore>(store_),
57        base::FilePath(),
58        scoped_ptr<CloudExternalDataManager>(),
59        loop_.message_loop_proxy(),
60        loop_.message_loop_proxy(),
61        loop_.message_loop_proxy()));
62    manager_->Init(&schema_registry_);
63    manager_->AddObserver(&observer_);
64    Mock::VerifyAndClearExpectations(store_);
65  }
66
67  // Required by the refresh scheduler that's created by the manager.
68  base::MessageLoop loop_;
69
70  // Convenience policy objects.
71  PolicyMap policy_map_;
72  PolicyBundle expected_bundle_;
73
74  // Policy infrastructure.
75  SchemaRegistry schema_registry_;
76  MockConfigurationPolicyObserver observer_;
77  MockUserCloudPolicyStore* store_;  // Not owned.
78  scoped_ptr<UserCloudPolicyManager> manager_;
79
80 private:
81  DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest);
82};
83
84TEST_F(UserCloudPolicyManagerTest, DisconnectAndRemovePolicy) {
85  // Load policy, make sure it goes away when DisconnectAndRemovePolicy() is
86  // called.
87  CreateManager();
88  store_->policy_map_.CopyFrom(policy_map_);
89  EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
90  store_->NotifyStoreLoaded();
91  EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
92  EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
93  EXPECT_CALL(*store_, Clear());
94  manager_->DisconnectAndRemovePolicy();
95  EXPECT_FALSE(manager_->core()->service());
96}
97
98}  // namespace
99}  // namespace policy
100