Chromium Code Reviews| Index: chrome/browser/policy/policy_statistics_collector_unittest.cc |
| diff --git a/chrome/browser/policy/policy_statistics_collector_unittest.cc b/chrome/browser/policy/policy_statistics_collector_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1d00278e8afd6224af03fb49db00b70ac6813276 |
| --- /dev/null |
| +++ b/chrome/browser/policy/policy_statistics_collector_unittest.cc |
| @@ -0,0 +1,177 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <cstring> |
| +#include <string> |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/time.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/policy/mock_policy_service.h" |
| +#include "chrome/browser/policy/policy_map.h" |
| +#include "chrome/browser/policy/policy_statistics_collector.h" |
| +#include "chrome/browser/policy/policy_types.h" |
| +#include "chrome/browser/policy/test_task_runner.h" |
| +#include "chrome/browser/prefs/browser_prefs.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/test/base/testing_pref_service.h" |
| +#include "policy/policy_constants.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace policy { |
| + |
| +namespace { |
| + |
| +using testing::_; |
| +using testing::Lt; |
| +using testing::Return; |
| +using testing::ReturnRef; |
| + |
| +// Arbitrary policy names used for testing. |
| +const char* const kTestPolicy1 = key::kHomepageIsNewTabPage; |
| +const char* const kTestPolicy2 = key::kInstantEnabled; |
| + |
| +class TestPolicyStatisticsCollector : public PolicyStatisticsCollector { |
| + public: |
| + TestPolicyStatisticsCollector( |
| + PolicyService* policy_service, |
| + PrefService* prefs, |
| + const scoped_refptr<base::TaskRunner>& task_runner) |
| + : PolicyStatisticsCollector(policy_service, prefs, task_runner) { |
| + } |
| + |
| + MOCK_METHOD1(RecordPolicyUse, void(int)); |
|
Joao da Silva
2012/09/20 12:06:21
I'm cool with this test, I'll just leave this sugg
|
| +}; |
| + |
| +} // namespace |
| + |
| +class PolicyStatisticsCollectorTest : public testing::Test { |
| + protected: |
| + PolicyStatisticsCollectorTest() |
| + : update_delay_(base::TimeDelta::FromMilliseconds( |
| + PolicyStatisticsCollector::kStatisticsUpdateRate)), |
| + test_policy_id1_(-1), |
| + test_policy_id2_(-1), |
| + task_runner_(new TestTaskRunner) { |
| + chrome::RegisterLocalState(&prefs_); |
| + |
| + // Find ids for kTestPolicy1 and kTestPolicy2. |
| + const policy::PolicyDefinitionList* policy_list = |
| + policy::GetChromePolicyDefinitionList(); |
| + for (const policy::PolicyDefinitionList::Entry* policy = policy_list->begin; |
| + policy != policy_list->end; ++policy) { |
| + if (strcmp(policy->name, kTestPolicy1) == 0) |
| + test_policy_id1_ = policy->id; |
| + else if (strcmp(policy->name, kTestPolicy2) == 0) |
| + test_policy_id2_ = policy->id; |
| + } |
| + ASSERT_TRUE(test_policy_id1_ != -1); |
| + ASSERT_TRUE(test_policy_id2_ != -1); |
| + |
| + // Set up default function behaviour. |
| + EXPECT_CALL(policy_service_, GetPolicies(POLICY_DOMAIN_CHROME, |
| + std::string())). |
| + WillRepeatedly(ReturnRef(policy_map_)); |
| + } |
| + |
| + virtual void SetUp() OVERRIDE { |
| + // Arbitrary negative value (so it'll be different from |update_delay_|). |
| + last_delay_ = base::TimeDelta::FromDays(-1); |
| + policy_map_.Clear(); |
| + policy_statistics_collector_.reset(new TestPolicyStatisticsCollector( |
| + &policy_service_, |
| + &prefs_, |
| + task_runner_)); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + policy_statistics_collector_.reset(); |
|
Joao da Silva
2012/09/20 12:06:21
No need to do this explicitly.
qfel
2012/09/20 13:00:02
Just to have eventual problems in cleanup caught b
|
| + } |
| + |
| + void SetPolicy(const std::string& name) { |
| + policy_map_.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| + base::Value::CreateBooleanValue(true)); |
| + } |
| + |
| + void UnsetPolicy(const std::string& name) { |
| + policy_map_.Erase(name); |
| + } |
| + |
| + const base::TimeDelta update_delay_; |
| + |
| + int test_policy_id1_; |
| + int test_policy_id2_; |
| + |
| + base::TimeDelta last_delay_; |
| + |
| + TestingPrefService prefs_; |
| + MockPolicyService policy_service_; |
| + PolicyMap policy_map_; |
| + |
| + scoped_refptr<TestTaskRunner> task_runner_; |
| + scoped_ptr<TestPolicyStatisticsCollector> policy_statistics_collector_; |
| +}; |
| + |
| +TEST_F(PolicyStatisticsCollectorTest, CollectPending) { |
| + SetPolicy(kTestPolicy1); |
| + |
| + prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate, |
| + (base::Time::Now() - update_delay_).ToInternalValue()); |
| + |
| + EXPECT_CALL(*policy_statistics_collector_.get(), |
| + RecordPolicyUse(test_policy_id1_)); |
| + EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, update_delay_)). |
| + WillOnce(Return(true)); |
| + |
| + policy_statistics_collector_->Initialize(); |
| +} |
| + |
| +TEST_F(PolicyStatisticsCollectorTest, CollectPendingVeryOld) { |
| + SetPolicy(kTestPolicy1); |
| + |
| + // Must not be 0.0 (read comment for Time::FromDoubleT). |
| + prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate, |
| + base::Time::FromDoubleT(1.0).ToInternalValue()); |
| + |
| + EXPECT_CALL(*policy_statistics_collector_.get(), |
| + RecordPolicyUse(test_policy_id1_)); |
| + EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, update_delay_)). |
| + WillOnce(Return(true)); |
| + |
| + policy_statistics_collector_->Initialize(); |
| +} |
| + |
| +TEST_F(PolicyStatisticsCollectorTest, CollectLater) { |
| + SetPolicy(kTestPolicy1); |
| + |
| + prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate, |
| + (base::Time::Now() - update_delay_ / 2).ToInternalValue()); |
| + |
| + EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, Lt(update_delay_))). |
| + WillOnce(Return(true)); |
| + |
| + policy_statistics_collector_->Initialize(); |
| +} |
| + |
| +TEST_F(PolicyStatisticsCollectorTest, MultiplePolicies) { |
| + SetPolicy(kTestPolicy1); |
| + SetPolicy(kTestPolicy2); |
| + |
| + prefs_.SetInt64(prefs::kLastPolicyStatisticsUpdate, |
| + (base::Time::Now() - update_delay_).ToInternalValue()); |
| + |
| + EXPECT_CALL(*policy_statistics_collector_.get(), |
| + RecordPolicyUse(test_policy_id1_)); |
| + EXPECT_CALL(*policy_statistics_collector_.get(), |
| + RecordPolicyUse(test_policy_id2_)); |
| + EXPECT_CALL(*task_runner_, PostDelayedTask(_, _, _)). |
| + WillOnce(Return(true)); |
| + |
| + policy_statistics_collector_->Initialize(); |
| +} |
| + |
| +} // namespace policy |