OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/policy/user_cloud_policy_store.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/policy/policy_builder.h" |
| 10 #include "chrome/browser/signin/signin_manager.h" |
| 11 #include "chrome/browser/signin/signin_manager_factory.h" |
| 12 #include "chrome/browser/signin/signin_manager_fake.h" |
| 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "policy/policy_constants.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using testing::AllOf; |
| 20 using testing::Eq; |
| 21 using testing::Property; |
| 22 |
| 23 namespace policy { |
| 24 |
| 25 namespace { |
| 26 |
| 27 class MockCloudPolicyStoreObserver : public CloudPolicyStore::Observer { |
| 28 public: |
| 29 MockCloudPolicyStoreObserver() {} |
| 30 virtual ~MockCloudPolicyStoreObserver() {} |
| 31 |
| 32 MOCK_METHOD1(OnStoreLoaded, void(CloudPolicyStore* store)); |
| 33 MOCK_METHOD1(OnStoreError, void(CloudPolicyStore* store)); |
| 34 |
| 35 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(MockCloudPolicyStoreObserver); |
| 37 }; |
| 38 |
| 39 class UserCloudPolicyStoreTest : public testing::Test { |
| 40 public: |
| 41 UserCloudPolicyStoreTest() |
| 42 : loop_(MessageLoop::TYPE_UI), |
| 43 ui_thread_(content::BrowserThread::UI, &loop_), |
| 44 file_thread_(content::BrowserThread::FILE, &loop_), |
| 45 profile_(new TestingProfile()) {} |
| 46 |
| 47 virtual void SetUp() OVERRIDE { |
| 48 SigninManager* signin = static_cast<SigninManager*>( |
| 49 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 50 profile_.get(), FakeSigninManager::Build)); |
| 51 signin->SetAuthenticatedUsername(PolicyBuilder::kFakeUsername); |
| 52 store_.reset(new UserCloudPolicyStore(profile_.get())); |
| 53 store_->AddObserver(&observer_); |
| 54 |
| 55 policy_.payload().mutable_showhomebutton()->set_showhomebutton(true); |
| 56 policy_.Build(); |
| 57 } |
| 58 |
| 59 virtual void TearDown() OVERRIDE { |
| 60 store_->RemoveObserver(&observer_); |
| 61 store_.reset(); |
| 62 base::RunLoop run_loop; |
| 63 run_loop.RunUntilIdle(); |
| 64 } |
| 65 |
| 66 // Verifies that store_->policy_map() has the ShowHomeButton entry. |
| 67 void VerifyPolicyMap() { |
| 68 EXPECT_EQ(1U, store_->policy_map().size()); |
| 69 const PolicyMap::Entry* entry = |
| 70 store_->policy_map().Get(key::kShowHomeButton); |
| 71 ASSERT_TRUE(entry); |
| 72 EXPECT_TRUE(base::FundamentalValue(true).Equals(entry->value)); |
| 73 } |
| 74 |
| 75 // Install an expectation on |observer_| for an error code. |
| 76 void ExpectError(CloudPolicyStore::Status error) { |
| 77 EXPECT_CALL(observer_, |
| 78 OnStoreError(AllOf(Eq(store_.get()), |
| 79 Property(&CloudPolicyStore::status, |
| 80 Eq(error))))); |
| 81 } |
| 82 |
| 83 UserPolicyBuilder policy_; |
| 84 MockCloudPolicyStoreObserver observer_; |
| 85 scoped_ptr<UserCloudPolicyStore> store_; |
| 86 |
| 87 // CloudPolicyValidator() requires a FILE thread so declare one here. Both |
| 88 // |ui_thread_| and |file_thread_| share the same MessageLoop |loop_| so |
| 89 // callers can use RunLoop to manage both virtual threads. |
| 90 MessageLoop loop_; |
| 91 content::TestBrowserThread ui_thread_; |
| 92 content::TestBrowserThread file_thread_; |
| 93 |
| 94 scoped_ptr<TestingProfile> profile_; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyStoreTest); |
| 97 }; |
| 98 |
| 99 TEST_F(UserCloudPolicyStoreTest, Store) { |
| 100 EXPECT_FALSE(store_->policy()); |
| 101 EXPECT_TRUE(store_->policy_map().empty()); |
| 102 |
| 103 // Store a simple policy and make sure it ends up as the currently active |
| 104 // policy. |
| 105 store_->Store(policy_.policy()); |
| 106 base::RunLoop run_loop; |
| 107 run_loop.RunUntilIdle(); |
| 108 |
| 109 // Policy should be decoded and stored. |
| 110 ASSERT_TRUE(store_->policy()); |
| 111 EXPECT_EQ(policy_.policy_data().SerializeAsString(), |
| 112 store_->policy()->SerializeAsString()); |
| 113 VerifyPolicyMap(); |
| 114 EXPECT_EQ(CloudPolicyStore::STATUS_OK, store_->status()); |
| 115 } |
| 116 |
| 117 TEST_F(UserCloudPolicyStoreTest, StoreValidationError) { |
| 118 // Create an invalid policy (no policy type). |
| 119 policy_.policy_data().clear_policy_type(); |
| 120 policy_.Build(); |
| 121 |
| 122 // Store policy. |
| 123 ExpectError(CloudPolicyStore::STATUS_VALIDATION_ERROR); |
| 124 store_->Store(policy_.policy()); |
| 125 base::RunLoop run_loop; |
| 126 run_loop.RunUntilIdle(); |
| 127 ASSERT_FALSE(store_->policy()); |
| 128 } |
| 129 |
| 130 } // namespace |
| 131 |
| 132 } // namespace policy |
| 133 |
OLD | NEW |