| 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "base/process/process.h" | 6 #include "base/process/process.h" |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "content/public/test/test_browser_thread.h" | 9 #include "content/public/test/test_browser_thread.h" |
| 10 #include "extensions/browser/extension_function.h" | 10 #include "extensions/browser/extension_function.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 const Config k2PerMinute = {2, TimeDelta::FromMinutes(1)}; | 30 const Config k2PerMinute = {2, TimeDelta::FromMinutes(1)}; |
| 31 const Config k20PerHour = {20, TimeDelta::FromHours(1)}; | 31 const Config k20PerHour = {20, TimeDelta::FromHours(1)}; |
| 32 const TimeTicks kStartTime = TimeTicks(); | 32 const TimeTicks kStartTime = TimeTicks(); |
| 33 const TimeTicks k1MinuteAfterStart = kStartTime + TimeDelta::FromMinutes(1); | 33 const TimeTicks k1MinuteAfterStart = kStartTime + TimeDelta::FromMinutes(1); |
| 34 | 34 |
| 35 class Mapper : public QuotaLimitHeuristic::BucketMapper { | 35 class Mapper : public QuotaLimitHeuristic::BucketMapper { |
| 36 public: | 36 public: |
| 37 Mapper() {} | 37 Mapper() {} |
| 38 virtual ~Mapper() { STLDeleteValues(&buckets_); } | 38 virtual ~Mapper() { STLDeleteValues(&buckets_); } |
| 39 virtual void GetBucketsForArgs(const base::ListValue* args, | 39 virtual void GetBucketsForArgs(const base::ListValue* args, |
| 40 BucketList* buckets) OVERRIDE { | 40 BucketList* buckets) override { |
| 41 for (size_t i = 0; i < args->GetSize(); i++) { | 41 for (size_t i = 0; i < args->GetSize(); i++) { |
| 42 int id; | 42 int id; |
| 43 ASSERT_TRUE(args->GetInteger(i, &id)); | 43 ASSERT_TRUE(args->GetInteger(i, &id)); |
| 44 if (buckets_.find(id) == buckets_.end()) | 44 if (buckets_.find(id) == buckets_.end()) |
| 45 buckets_[id] = new Bucket(); | 45 buckets_[id] = new Bucket(); |
| 46 buckets->push_back(buckets_[id]); | 46 buckets->push_back(buckets_[id]); |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 typedef std::map<int, Bucket*> BucketMap; | 51 typedef std::map<int, Bucket*> BucketMap; |
| 52 BucketMap buckets_; | 52 BucketMap buckets_; |
| 53 DISALLOW_COPY_AND_ASSIGN(Mapper); | 53 DISALLOW_COPY_AND_ASSIGN(Mapper); |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 class MockMapper : public QuotaLimitHeuristic::BucketMapper { | 56 class MockMapper : public QuotaLimitHeuristic::BucketMapper { |
| 57 public: | 57 public: |
| 58 virtual void GetBucketsForArgs(const base::ListValue* args, | 58 virtual void GetBucketsForArgs(const base::ListValue* args, |
| 59 BucketList* buckets) OVERRIDE {} | 59 BucketList* buckets) override {} |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 class MockFunction : public ExtensionFunction { | 62 class MockFunction : public ExtensionFunction { |
| 63 public: | 63 public: |
| 64 explicit MockFunction(const std::string& name) { set_name(name); } | 64 explicit MockFunction(const std::string& name) { set_name(name); } |
| 65 | 65 |
| 66 virtual void SetArgs(const base::ListValue* args) OVERRIDE {} | 66 virtual void SetArgs(const base::ListValue* args) override {} |
| 67 virtual std::string GetError() const OVERRIDE { return std::string(); } | 67 virtual std::string GetError() const override { return std::string(); } |
| 68 virtual void SetError(const std::string& error) OVERRIDE {} | 68 virtual void SetError(const std::string& error) override {} |
| 69 virtual void Destruct() const OVERRIDE { delete this; } | 69 virtual void Destruct() const override { delete this; } |
| 70 virtual ResponseAction Run() OVERRIDE { return RespondLater(); } | 70 virtual ResponseAction Run() override { return RespondLater(); } |
| 71 virtual void SendResponse(bool) OVERRIDE {} | 71 virtual void SendResponse(bool) override {} |
| 72 | 72 |
| 73 protected: | 73 protected: |
| 74 virtual ~MockFunction() {} | 74 virtual ~MockFunction() {} |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 class TimedLimitMockFunction : public MockFunction { | 77 class TimedLimitMockFunction : public MockFunction { |
| 78 public: | 78 public: |
| 79 explicit TimedLimitMockFunction(const std::string& name) | 79 explicit TimedLimitMockFunction(const std::string& name) |
| 80 : MockFunction(name) {} | 80 : MockFunction(name) {} |
| 81 virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const | 81 virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const |
| 82 OVERRIDE { | 82 override { |
| 83 heuristics->push_back( | 83 heuristics->push_back( |
| 84 new TimedLimit(k2PerMinute, new Mapper(), kGenericName)); | 84 new TimedLimit(k2PerMinute, new Mapper(), kGenericName)); |
| 85 } | 85 } |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 virtual ~TimedLimitMockFunction() {} | 88 virtual ~TimedLimitMockFunction() {} |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 class ChainedLimitsMockFunction : public MockFunction { | 91 class ChainedLimitsMockFunction : public MockFunction { |
| 92 public: | 92 public: |
| 93 explicit ChainedLimitsMockFunction(const std::string& name) | 93 explicit ChainedLimitsMockFunction(const std::string& name) |
| 94 : MockFunction(name) {} | 94 : MockFunction(name) {} |
| 95 virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const | 95 virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const |
| 96 OVERRIDE { | 96 override { |
| 97 // No more than 2 per minute sustained over 5 minutes. | 97 // No more than 2 per minute sustained over 5 minutes. |
| 98 heuristics->push_back(new SustainedLimit( | 98 heuristics->push_back(new SustainedLimit( |
| 99 TimeDelta::FromMinutes(5), k2PerMinute, new Mapper(), kGenericName)); | 99 TimeDelta::FromMinutes(5), k2PerMinute, new Mapper(), kGenericName)); |
| 100 // No more than 20 per hour. | 100 // No more than 20 per hour. |
| 101 heuristics->push_back( | 101 heuristics->push_back( |
| 102 new TimedLimit(k20PerHour, new Mapper(), kGenericName)); | 102 new TimedLimit(k20PerHour, new Mapper(), kGenericName)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 private: | 105 private: |
| 106 virtual ~ChainedLimitsMockFunction() {} | 106 virtual ~ChainedLimitsMockFunction() {} |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 class FrozenMockFunction : public MockFunction { | 109 class FrozenMockFunction : public MockFunction { |
| 110 public: | 110 public: |
| 111 explicit FrozenMockFunction(const std::string& name) : MockFunction(name) {} | 111 explicit FrozenMockFunction(const std::string& name) : MockFunction(name) {} |
| 112 virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const | 112 virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const |
| 113 OVERRIDE { | 113 override { |
| 114 heuristics->push_back( | 114 heuristics->push_back( |
| 115 new TimedLimit(kFrozenConfig, new Mapper(), kGenericName)); | 115 new TimedLimit(kFrozenConfig, new Mapper(), kGenericName)); |
| 116 } | 116 } |
| 117 | 117 |
| 118 private: | 118 private: |
| 119 virtual ~FrozenMockFunction() {} | 119 virtual ~FrozenMockFunction() {} |
| 120 }; | 120 }; |
| 121 } // namespace | 121 } // namespace |
| 122 | 122 |
| 123 class QuotaServiceTest : public testing::Test { | 123 class QuotaServiceTest : public testing::Test { |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 "", | 407 "", |
| 408 service_->Assess( | 408 service_->Assess( |
| 409 extension_a_, f.get(), &arg, kStartTime + TimeDelta::FromDays(1))); | 409 extension_a_, f.get(), &arg, kStartTime + TimeDelta::FromDays(1))); |
| 410 EXPECT_NE( | 410 EXPECT_NE( |
| 411 "", | 411 "", |
| 412 service_->Assess( | 412 service_->Assess( |
| 413 extension_a_, g.get(), &arg, kStartTime + TimeDelta::FromDays(1))); | 413 extension_a_, g.get(), &arg, kStartTime + TimeDelta::FromDays(1))); |
| 414 } | 414 } |
| 415 | 415 |
| 416 } // namespace extensions | 416 } // namespace extensions |
| OLD | NEW |