1 // Copyright (c) 2012 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.
5 #include "base/basictypes.h"
6 #include "base/callback.h"
7 #include "chrome/browser/chromeos/policy/proxy_policy_provider.h"
8 #include "components/policy/core/common/external_data_fetcher.h"
9 #include "components/policy/core/common/mock_configuration_policy_provider.h"
10 #include "components/policy/core/common/schema_registry.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 class ProxyPolicyProviderTest
: public testing::Test
{
20 ProxyPolicyProviderTest() {
21 mock_provider_
.Init();
22 proxy_provider_
.Init(&schema_registry_
);
23 proxy_provider_
.AddObserver(&observer_
);
26 virtual ~ProxyPolicyProviderTest() {
27 proxy_provider_
.RemoveObserver(&observer_
);
28 proxy_provider_
.Shutdown();
29 mock_provider_
.Shutdown();
32 SchemaRegistry schema_registry_
;
33 MockConfigurationPolicyObserver observer_
;
34 MockConfigurationPolicyProvider mock_provider_
;
35 ProxyPolicyProvider proxy_provider_
;
37 static scoped_ptr
<PolicyBundle
> CopyBundle(const PolicyBundle
& bundle
) {
38 scoped_ptr
<PolicyBundle
> copy(new PolicyBundle());
39 copy
->CopyFrom(bundle
);
43 DISALLOW_COPY_AND_ASSIGN(ProxyPolicyProviderTest
);
46 TEST_F(ProxyPolicyProviderTest
, Init
) {
47 EXPECT_TRUE(proxy_provider_
.IsInitializationComplete(POLICY_DOMAIN_CHROME
));
48 EXPECT_TRUE(PolicyBundle().Equals(proxy_provider_
.policies()));
51 TEST_F(ProxyPolicyProviderTest
, Delegate
) {
53 bundle
.Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
55 POLICY_LEVEL_MANDATORY
,
57 base::Value::CreateStringValue("value"),
59 mock_provider_
.UpdatePolicy(CopyBundle(bundle
));
61 EXPECT_CALL(observer_
, OnUpdatePolicy(&proxy_provider_
));
62 proxy_provider_
.SetDelegate(&mock_provider_
);
63 Mock::VerifyAndClearExpectations(&observer_
);
64 EXPECT_TRUE(bundle
.Equals(proxy_provider_
.policies()));
66 EXPECT_CALL(observer_
, OnUpdatePolicy(&proxy_provider_
));
67 bundle
.Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
69 POLICY_LEVEL_MANDATORY
,
71 base::Value::CreateStringValue("new value"),
73 mock_provider_
.UpdatePolicy(CopyBundle(bundle
));
74 Mock::VerifyAndClearExpectations(&observer_
);
75 EXPECT_TRUE(bundle
.Equals(proxy_provider_
.policies()));
77 EXPECT_CALL(observer_
, OnUpdatePolicy(&proxy_provider_
));
78 proxy_provider_
.SetDelegate(NULL
);
79 EXPECT_TRUE(PolicyBundle().Equals(proxy_provider_
.policies()));
82 TEST_F(ProxyPolicyProviderTest
, RefreshPolicies
) {
83 EXPECT_CALL(observer_
, OnUpdatePolicy(&proxy_provider_
));
84 proxy_provider_
.RefreshPolicies();
85 Mock::VerifyAndClearExpectations(&observer_
);
87 EXPECT_CALL(observer_
, OnUpdatePolicy(&proxy_provider_
));
88 proxy_provider_
.SetDelegate(&mock_provider_
);
89 Mock::VerifyAndClearExpectations(&observer_
);
91 EXPECT_CALL(observer_
, OnUpdatePolicy(&proxy_provider_
)).Times(0);
92 EXPECT_CALL(mock_provider_
, RefreshPolicies());
93 proxy_provider_
.RefreshPolicies();
94 Mock::VerifyAndClearExpectations(&observer_
);
95 Mock::VerifyAndClearExpectations(&mock_provider_
);
97 EXPECT_CALL(observer_
, OnUpdatePolicy(&proxy_provider_
));
98 mock_provider_
.UpdatePolicy(scoped_ptr
<PolicyBundle
>(new PolicyBundle()));
99 Mock::VerifyAndClearExpectations(&observer_
);
102 } // namespace policy