OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/extensions/extension_webrequest_time_tracker.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace { |
| 10 const base::TimeDelta kRequestDelta = base::TimeDelta::FromMilliseconds(100); |
| 11 const base::TimeDelta kTinyDelay = base::TimeDelta::FromMilliseconds(1); |
| 12 const base::TimeDelta kModerateDelay = base::TimeDelta::FromMilliseconds(25); |
| 13 const base::TimeDelta kExcessiveDelay = base::TimeDelta::FromMilliseconds(75); |
| 14 } // namespace |
| 15 |
| 16 //class ExtensionWebRequestTimeTrackerTest : public testing::Test {}; |
| 17 |
| 18 TEST(ExtensionWebRequestTimeTrackerTest, Basic) { |
| 19 ExtensionWebRequestTimeTracker tracker; |
| 20 base::Time start; |
| 21 |
| 22 tracker.LogRequestStartTime(42, start, GURL()); |
| 23 EXPECT_EQ(1u, tracker.request_time_logs_.size()); |
| 24 ASSERT_EQ(1u, tracker.request_ids_.size()); |
| 25 EXPECT_EQ(42, tracker.request_ids_.front()); |
| 26 tracker.LogRequestEndTime(42, start + kRequestDelta); |
| 27 EXPECT_EQ(1u, tracker.request_time_logs_.size()); |
| 28 EXPECT_EQ(0u, tracker.moderate_delays_.size()); |
| 29 EXPECT_EQ(0u, tracker.excessive_delays_.size()); |
| 30 } |
| 31 |
| 32 TEST(ExtensionWebRequestTimeTrackerTest, CancelOrRedirect) { |
| 33 ExtensionWebRequestTimeTracker tracker; |
| 34 base::Time start; |
| 35 |
| 36 tracker.LogRequestStartTime(1, start, GURL()); |
| 37 EXPECT_EQ(1u, tracker.request_time_logs_.size()); |
| 38 tracker.SetRequestCanceled(1); |
| 39 tracker.LogRequestEndTime(1, start + kRequestDelta); |
| 40 EXPECT_EQ(0u, tracker.request_time_logs_.size()); |
| 41 |
| 42 tracker.LogRequestStartTime(2, start, GURL()); |
| 43 EXPECT_EQ(1u, tracker.request_time_logs_.size()); |
| 44 tracker.SetRequestRedirected(2); |
| 45 tracker.LogRequestEndTime(2, start + kRequestDelta); |
| 46 EXPECT_EQ(0u, tracker.request_time_logs_.size()); |
| 47 } |
| 48 |
| 49 TEST(ExtensionWebRequestTimeTrackerTest, Delays) { |
| 50 ExtensionWebRequestTimeTracker tracker; |
| 51 base::Time start; |
| 52 std::string extension1_id("1"); |
| 53 std::string extension2_id("2"); |
| 54 |
| 55 // Start 3 requests with different amounts of delay from 2 extensions. |
| 56 tracker.LogRequestStartTime(1, start, GURL()); |
| 57 tracker.LogRequestStartTime(2, start, GURL()); |
| 58 tracker.LogRequestStartTime(3, start, GURL()); |
| 59 tracker.IncrementExtensionBlockTime(extension1_id, 1, kTinyDelay); |
| 60 tracker.IncrementExtensionBlockTime(extension1_id, 2, kTinyDelay); |
| 61 tracker.IncrementExtensionBlockTime(extension1_id, 3, kTinyDelay); |
| 62 tracker.IncrementExtensionBlockTime(extension2_id, 2, kModerateDelay); |
| 63 tracker.IncrementExtensionBlockTime(extension2_id, 3, kExcessiveDelay); |
| 64 tracker.IncrementTotalBlockTime(1, kTinyDelay); |
| 65 tracker.IncrementTotalBlockTime(2, kModerateDelay); |
| 66 tracker.IncrementTotalBlockTime(3, kExcessiveDelay); |
| 67 tracker.LogRequestEndTime(1, start + kRequestDelta); |
| 68 tracker.LogRequestEndTime(2, start + kRequestDelta); |
| 69 tracker.LogRequestEndTime(3, start + kRequestDelta); |
| 70 EXPECT_EQ(3u, tracker.request_time_logs_.size()); |
| 71 EXPECT_EQ(1u, tracker.moderate_delays_.size()); |
| 72 EXPECT_EQ(1u, tracker.moderate_delays_.count(2)); |
| 73 EXPECT_EQ(1u, tracker.excessive_delays_.size()); |
| 74 EXPECT_EQ(1u, tracker.excessive_delays_.count(3)); |
| 75 |
| 76 // Now issue a bunch more requests and ensure that the old delays are |
| 77 // forgotten. |
| 78 for (int64 i = 4; i < 500; ++i) { |
| 79 tracker.LogRequestStartTime(i, start, GURL()); |
| 80 tracker.LogRequestEndTime(i, start + kRequestDelta); |
| 81 } |
| 82 EXPECT_EQ(0u, tracker.moderate_delays_.size()); |
| 83 EXPECT_EQ(0u, tracker.excessive_delays_.size()); |
| 84 } |
OLD | NEW |