OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/policy/core/common/async_policy_provider.h" | 5 #include "components/policy/core/common/async_policy_provider.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 27 matching lines...) Expand all Loading... | |
38 new base::StringValue(value), | 38 new base::StringValue(value), |
39 NULL); | 39 NULL); |
40 } | 40 } |
41 | 41 |
42 class MockPolicyLoader : public AsyncPolicyLoader { | 42 class MockPolicyLoader : public AsyncPolicyLoader { |
43 public: | 43 public: |
44 explicit MockPolicyLoader( | 44 explicit MockPolicyLoader( |
45 scoped_refptr<base::SequencedTaskRunner> task_runner); | 45 scoped_refptr<base::SequencedTaskRunner> task_runner); |
46 ~MockPolicyLoader() override; | 46 ~MockPolicyLoader() override; |
47 | 47 |
48 // Load() returns a scoped_ptr<PolicyBundle> but it can't be mocked because | 48 // Load() returns a std::unique_ptr<PolicyBundle> but it can't be mocked |
49 // because | |
Thiemo Nagel
2016/04/20 20:34:19
Please fix line wrapping and convert in-comment me
dcheng
2016/04/20 20:56:31
Done.
| |
49 // scoped_ptr is moveable but not copyable. This override forwards the | 50 // scoped_ptr is moveable but not copyable. This override forwards the |
50 // call to MockLoad() which returns a PolicyBundle*, and returns a copy | 51 // call to MockLoad() which returns a PolicyBundle*, and returns a copy |
51 // wrapped in a passed scoped_ptr. | 52 // wrapped in a passed scoped_ptr. |
52 scoped_ptr<PolicyBundle> Load() override; | 53 std::unique_ptr<PolicyBundle> Load() override; |
53 | 54 |
54 MOCK_METHOD0(MockLoad, const PolicyBundle*()); | 55 MOCK_METHOD0(MockLoad, const PolicyBundle*()); |
55 MOCK_METHOD0(InitOnBackgroundThread, void()); | 56 MOCK_METHOD0(InitOnBackgroundThread, void()); |
56 MOCK_METHOD0(LastModificationTime, base::Time()); | 57 MOCK_METHOD0(LastModificationTime, base::Time()); |
57 | 58 |
58 private: | 59 private: |
59 DISALLOW_COPY_AND_ASSIGN(MockPolicyLoader); | 60 DISALLOW_COPY_AND_ASSIGN(MockPolicyLoader); |
60 }; | 61 }; |
61 | 62 |
62 MockPolicyLoader::MockPolicyLoader( | 63 MockPolicyLoader::MockPolicyLoader( |
63 scoped_refptr<base::SequencedTaskRunner> task_runner) | 64 scoped_refptr<base::SequencedTaskRunner> task_runner) |
64 : AsyncPolicyLoader(task_runner) {} | 65 : AsyncPolicyLoader(task_runner) {} |
65 | 66 |
66 MockPolicyLoader::~MockPolicyLoader() {} | 67 MockPolicyLoader::~MockPolicyLoader() {} |
67 | 68 |
68 scoped_ptr<PolicyBundle> MockPolicyLoader::Load() { | 69 std::unique_ptr<PolicyBundle> MockPolicyLoader::Load() { |
69 scoped_ptr<PolicyBundle> bundle; | 70 std::unique_ptr<PolicyBundle> bundle; |
70 const PolicyBundle* loaded = MockLoad(); | 71 const PolicyBundle* loaded = MockLoad(); |
71 if (loaded) { | 72 if (loaded) { |
72 bundle.reset(new PolicyBundle()); | 73 bundle.reset(new PolicyBundle()); |
73 bundle->CopyFrom(*loaded); | 74 bundle->CopyFrom(*loaded); |
74 } | 75 } |
75 return bundle; | 76 return bundle; |
76 } | 77 } |
77 | 78 |
78 } // namespace | 79 } // namespace |
79 | 80 |
80 class AsyncPolicyProviderTest : public testing::Test { | 81 class AsyncPolicyProviderTest : public testing::Test { |
81 protected: | 82 protected: |
82 AsyncPolicyProviderTest(); | 83 AsyncPolicyProviderTest(); |
83 ~AsyncPolicyProviderTest() override; | 84 ~AsyncPolicyProviderTest() override; |
84 | 85 |
85 void SetUp() override; | 86 void SetUp() override; |
86 void TearDown() override; | 87 void TearDown() override; |
87 | 88 |
88 base::MessageLoop loop_; | 89 base::MessageLoop loop_; |
89 SchemaRegistry schema_registry_; | 90 SchemaRegistry schema_registry_; |
90 PolicyBundle initial_bundle_; | 91 PolicyBundle initial_bundle_; |
91 MockPolicyLoader* loader_; | 92 MockPolicyLoader* loader_; |
92 scoped_ptr<AsyncPolicyProvider> provider_; | 93 std::unique_ptr<AsyncPolicyProvider> provider_; |
93 | 94 |
94 private: | 95 private: |
95 DISALLOW_COPY_AND_ASSIGN(AsyncPolicyProviderTest); | 96 DISALLOW_COPY_AND_ASSIGN(AsyncPolicyProviderTest); |
96 }; | 97 }; |
97 | 98 |
98 AsyncPolicyProviderTest::AsyncPolicyProviderTest() {} | 99 AsyncPolicyProviderTest::AsyncPolicyProviderTest() {} |
99 | 100 |
100 AsyncPolicyProviderTest::~AsyncPolicyProviderTest() {} | 101 AsyncPolicyProviderTest::~AsyncPolicyProviderTest() {} |
101 | 102 |
102 void AsyncPolicyProviderTest::SetUp() { | 103 void AsyncPolicyProviderTest::SetUp() { |
103 SetPolicy(&initial_bundle_, "policy", "initial"); | 104 SetPolicy(&initial_bundle_, "policy", "initial"); |
104 loader_ = new MockPolicyLoader(loop_.task_runner()); | 105 loader_ = new MockPolicyLoader(loop_.task_runner()); |
105 EXPECT_CALL(*loader_, LastModificationTime()) | 106 EXPECT_CALL(*loader_, LastModificationTime()) |
106 .WillRepeatedly(Return(base::Time())); | 107 .WillRepeatedly(Return(base::Time())); |
107 EXPECT_CALL(*loader_, InitOnBackgroundThread()).Times(1); | 108 EXPECT_CALL(*loader_, InitOnBackgroundThread()).Times(1); |
108 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_)); | 109 EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_)); |
109 | 110 |
110 provider_.reset(new AsyncPolicyProvider( | 111 provider_.reset(new AsyncPolicyProvider( |
111 &schema_registry_, scoped_ptr<AsyncPolicyLoader>(loader_))); | 112 &schema_registry_, std::unique_ptr<AsyncPolicyLoader>(loader_))); |
112 provider_->Init(&schema_registry_); | 113 provider_->Init(&schema_registry_); |
113 // Verify that the initial load is done synchronously: | 114 // Verify that the initial load is done synchronously: |
114 EXPECT_TRUE(provider_->policies().Equals(initial_bundle_)); | 115 EXPECT_TRUE(provider_->policies().Equals(initial_bundle_)); |
115 | 116 |
116 loop_.RunUntilIdle(); | 117 loop_.RunUntilIdle(); |
117 Mock::VerifyAndClearExpectations(loader_); | 118 Mock::VerifyAndClearExpectations(loader_); |
118 | 119 |
119 EXPECT_CALL(*loader_, LastModificationTime()) | 120 EXPECT_CALL(*loader_, LastModificationTime()) |
120 .WillRepeatedly(Return(base::Time())); | 121 .WillRepeatedly(Return(base::Time())); |
121 } | 122 } |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
220 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); | 221 EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0); |
221 provider_->Shutdown(); | 222 provider_->Shutdown(); |
222 loop_.RunUntilIdle(); | 223 loop_.RunUntilIdle(); |
223 Mock::VerifyAndClearExpectations(&observer); | 224 Mock::VerifyAndClearExpectations(&observer); |
224 | 225 |
225 provider_->RemoveObserver(&observer); | 226 provider_->RemoveObserver(&observer); |
226 provider_.reset(); | 227 provider_.reset(); |
227 } | 228 } |
228 | 229 |
229 } // namespace policy | 230 } // namespace policy |
OLD | NEW |