Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / url_request / url_request_throttler_unittest.cc
index 32f94ff..a3fb410 100644 (file)
 #include "base/stl_util.h"
 #include "base/strings/string_number_conversions.h"
 #include "base/strings/stringprintf.h"
-#include "base/test/statistics_delta_reader.h"
+#include "base/test/histogram_tester.h"
 #include "base/time/time.h"
 #include "net/base/load_flags.h"
 #include "net/base/request_priority.h"
 #include "net/base/test_completion_callback.h"
+#include "net/url_request/url_request.h"
 #include "net/url_request/url_request_context.h"
 #include "net/url_request/url_request_test_util.h"
 #include "net/url_request/url_request_throttler_header_interface.h"
@@ -33,17 +34,17 @@ const char kRequestThrottledHistogramName[] = "Throttling.RequestThrottled";
 class MockURLRequestThrottlerEntry : public URLRequestThrottlerEntry {
  public:
   explicit MockURLRequestThrottlerEntry(
-      net::URLRequestThrottlerManager* manager)
-      : net::URLRequestThrottlerEntry(manager, std::string()),
+      URLRequestThrottlerManager* manager)
+      : URLRequestThrottlerEntry(manager, std::string()),
         mock_backoff_entry_(&backoff_policy_) {
     InitPolicy();
   }
   MockURLRequestThrottlerEntry(
-      net::URLRequestThrottlerManager* manager,
+      URLRequestThrottlerManager* manager,
       const TimeTicks& exponential_backoff_release_time,
       const TimeTicks& sliding_window_release_time,
       const TimeTicks& fake_now)
-      : net::URLRequestThrottlerEntry(manager, std::string()),
+      : URLRequestThrottlerEntry(manager, std::string()),
         fake_time_now_(fake_now),
         mock_backoff_entry_(&backoff_policy_) {
     InitPolicy();
@@ -170,7 +171,8 @@ struct GurlAndString {
 class URLRequestThrottlerEntryTest : public testing::Test {
  protected:
   URLRequestThrottlerEntryTest()
-      : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {}
+      : request_(context_.CreateRequest(GURL(), DEFAULT_PRIORITY, NULL, NULL)) {
+  }
 
   virtual void SetUp();
 
@@ -179,11 +181,11 @@ class URLRequestThrottlerEntryTest : public testing::Test {
   scoped_refptr<MockURLRequestThrottlerEntry> entry_;
 
   TestURLRequestContext context_;
-  TestURLRequest request_;
+  scoped_ptr<URLRequest> request_;
 };
 
 void URLRequestThrottlerEntryTest::SetUp() {
-  request_.SetLoadFlags(0);
+  request_->SetLoadFlags(0);
 
   now_ = TimeTicks::Now();
   entry_ = new MockURLRequestThrottlerEntry(&manager_);
@@ -201,41 +203,41 @@ TEST_F(URLRequestThrottlerEntryTest, CanThrottleRequest) {
       entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
 
   d.set_can_throttle_requests(false);
-  EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
+  EXPECT_FALSE(entry_->ShouldRejectRequest(*request_,
+                                           context_.network_delegate()));
   d.set_can_throttle_requests(true);
-  EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
+  EXPECT_TRUE(entry_->ShouldRejectRequest(*request_,
+                                          context_.network_delegate()));
 }
 
 TEST_F(URLRequestThrottlerEntryTest, InterfaceDuringExponentialBackoff) {
-  base::StatisticsDeltaReader statistics_delta_reader;
+  base::HistogramTester histogram_tester;
   entry_->set_exponential_backoff_release_time(
       entry_->fake_time_now_ + TimeDelta::FromMilliseconds(1));
-  EXPECT_TRUE(entry_->ShouldRejectRequest(request_));
+  EXPECT_TRUE(entry_->ShouldRejectRequest(*request_,
+                                          context_.network_delegate()));
 
   // Also end-to-end test the load flags exceptions.
-  request_.SetLoadFlags(LOAD_MAYBE_USER_GESTURE);
-  EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
-
-  scoped_ptr<base::HistogramSamples> samples(
-      statistics_delta_reader.GetHistogramSamplesSinceCreation(
-          kRequestThrottledHistogramName));
-  ASSERT_EQ(1, samples->GetCount(0));
-  ASSERT_EQ(1, samples->GetCount(1));
+  request_->SetLoadFlags(LOAD_MAYBE_USER_GESTURE);
+  EXPECT_FALSE(entry_->ShouldRejectRequest(*request_,
+                                           context_.network_delegate()));
+
+  histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 1);
+  histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 1);
 }
 
 TEST_F(URLRequestThrottlerEntryTest, InterfaceNotDuringExponentialBackoff) {
-  base::StatisticsDeltaReader statistics_delta_reader;
+  base::HistogramTester histogram_tester;
   entry_->set_exponential_backoff_release_time(entry_->fake_time_now_);
-  EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
+  EXPECT_FALSE(entry_->ShouldRejectRequest(*request_,
+                                           context_.network_delegate()));
   entry_->set_exponential_backoff_release_time(
       entry_->fake_time_now_ - TimeDelta::FromMilliseconds(1));
-  EXPECT_FALSE(entry_->ShouldRejectRequest(request_));
+  EXPECT_FALSE(entry_->ShouldRejectRequest(*request_,
+                                           context_.network_delegate()));
 
-  scoped_ptr<base::HistogramSamples> samples(
-      statistics_delta_reader.GetHistogramSamplesSinceCreation(
-          kRequestThrottledHistogramName));
-  ASSERT_EQ(2, samples->GetCount(0));
-  ASSERT_EQ(0, samples->GetCount(1));
+  histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 0, 2);
+  histogram_tester.ExpectBucketCount(kRequestThrottledHistogramName, 1, 0);
 }
 
 TEST_F(URLRequestThrottlerEntryTest, InterfaceUpdateFailure) {
@@ -352,15 +354,44 @@ TEST_F(URLRequestThrottlerEntryTest, ExplicitUserRequest) {
 class URLRequestThrottlerManagerTest : public testing::Test {
  protected:
   URLRequestThrottlerManagerTest()
-      : request_(GURL(), DEFAULT_PRIORITY, NULL, &context_) {}
+      : request_(context_.CreateRequest(GURL(), DEFAULT_PRIORITY, NULL, NULL)) {
+  }
 
   virtual void SetUp() {
-    request_.SetLoadFlags(0);
+    request_->SetLoadFlags(0);
+  }
+
+  void ExpectEntryAllowsAllOnErrorIfOptedOut(
+      URLRequestThrottlerEntryInterface* entry,
+      bool opted_out,
+      const URLRequest& request) {
+    EXPECT_FALSE(entry->ShouldRejectRequest(request,
+                                            context_.network_delegate()));
+    MockURLRequestThrottlerHeaderAdapter failure_adapter(503);
+    for (int i = 0; i < 10; ++i) {
+      // Host doesn't really matter in this scenario so we skip it.
+      entry->UpdateWithResponse(std::string(), &failure_adapter);
+    }
+    EXPECT_NE(opted_out, entry->ShouldRejectRequest(
+                  request, context_.network_delegate()));
+
+    if (opted_out) {
+      // We're not mocking out GetTimeNow() in this scenario
+      // so add a 100 ms buffer to avoid flakiness (that should always
+      // give enough time to get from the TimeTicks::Now() call here
+      // to the TimeTicks::Now() call in the entry class).
+      EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
+                entry->GetExponentialBackoffReleaseTime());
+    } else {
+      // As above, add 100 ms.
+      EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
+                entry->GetExponentialBackoffReleaseTime());
+    }
   }
 
   // context_ must be declared before request_.
   TestURLRequestContext context_;
-  TestURLRequest request_;
+  scoped_ptr<URLRequest> request_;
 };
 
 TEST_F(URLRequestThrottlerManagerTest, IsUrlStandardised) {
@@ -427,35 +458,9 @@ TEST_F(URLRequestThrottlerManagerTest, IsHostBeingRegistered) {
   EXPECT_EQ(3, manager.GetNumberOfEntries());
 }
 
-void ExpectEntryAllowsAllOnErrorIfOptedOut(
-    net::URLRequestThrottlerEntryInterface* entry,
-    bool opted_out,
-    const URLRequest& request) {
-  EXPECT_FALSE(entry->ShouldRejectRequest(request));
-  MockURLRequestThrottlerHeaderAdapter failure_adapter(503);
-  for (int i = 0; i < 10; ++i) {
-    // Host doesn't really matter in this scenario so we skip it.
-    entry->UpdateWithResponse(std::string(), &failure_adapter);
-  }
-  EXPECT_NE(opted_out, entry->ShouldRejectRequest(request));
-
-  if (opted_out) {
-    // We're not mocking out GetTimeNow() in this scenario
-    // so add a 100 ms buffer to avoid flakiness (that should always
-    // give enough time to get from the TimeTicks::Now() call here
-    // to the TimeTicks::Now() call in the entry class).
-    EXPECT_GT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
-              entry->GetExponentialBackoffReleaseTime());
-  } else {
-    // As above, add 100 ms.
-    EXPECT_LT(TimeTicks::Now() + TimeDelta::FromMilliseconds(100),
-              entry->GetExponentialBackoffReleaseTime());
-  }
-}
-
 TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) {
   MockURLRequestThrottlerManager manager;
-  scoped_refptr<net::URLRequestThrottlerEntryInterface> entry =
+  scoped_refptr<URLRequestThrottlerEntryInterface> entry =
       manager.RegisterRequestUrl(GURL("http://www.google.com/yodude"));
 
   // Fake a response with the opt-out header.
@@ -466,40 +471,41 @@ TEST_F(URLRequestThrottlerManagerTest, OptOutHeader) {
   entry->UpdateWithResponse("www.google.com", &response_adapter);
 
   // Ensure that the same entry on error always allows everything.
-  ExpectEntryAllowsAllOnErrorIfOptedOut(entry.get(), true, request_);
+  ExpectEntryAllowsAllOnErrorIfOptedOut(entry.get(), true, *request_);
 
   // Ensure that a freshly created entry (for a different URL on an
   // already opted-out host) also gets "always allow" behavior.
-  scoped_refptr<net::URLRequestThrottlerEntryInterface> other_entry =
+  scoped_refptr<URLRequestThrottlerEntryInterface> other_entry =
       manager.RegisterRequestUrl(GURL("http://www.google.com/bingobob"));
-  ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry.get(), true, request_);
+  ExpectEntryAllowsAllOnErrorIfOptedOut(other_entry.get(), true, *request_);
 
   // Fake a response with the opt-out header incorrectly specified.
-  scoped_refptr<net::URLRequestThrottlerEntryInterface> no_opt_out_entry =
+  scoped_refptr<URLRequestThrottlerEntryInterface> no_opt_out_entry =
       manager.RegisterRequestUrl(GURL("http://www.nike.com/justdoit"));
   MockURLRequestThrottlerHeaderAdapter wrong_adapter(
       std::string(), "yesplease", 200);
   no_opt_out_entry->UpdateWithResponse("www.nike.com", &wrong_adapter);
   ExpectEntryAllowsAllOnErrorIfOptedOut(
-      no_opt_out_entry.get(), false, request_);
+      no_opt_out_entry.get(), false, *request_);
 
   // A localhost entry should always be opted out.
-  scoped_refptr<net::URLRequestThrottlerEntryInterface> localhost_entry =
+  scoped_refptr<URLRequestThrottlerEntryInterface> localhost_entry =
       manager.RegisterRequestUrl(GURL("http://localhost/hello"));
-  ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry.get(), true, request_);
+  ExpectEntryAllowsAllOnErrorIfOptedOut(localhost_entry.get(), true, *request_);
 }
 
 TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) {
   for (int i = 0; i < 3; ++i) {
     MockURLRequestThrottlerManager manager;
-    scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_before =
+    scoped_refptr<URLRequestThrottlerEntryInterface> entry_before =
         manager.RegisterRequestUrl(GURL("http://www.example.com/"));
     MockURLRequestThrottlerHeaderAdapter failure_adapter(503);
     for (int j = 0; j < 10; ++j) {
       // Host doesn't really matter in this scenario so we skip it.
       entry_before->UpdateWithResponse(std::string(), &failure_adapter);
     }
-    EXPECT_TRUE(entry_before->ShouldRejectRequest(request_));
+    EXPECT_TRUE(entry_before->ShouldRejectRequest(*request_,
+                                                  context_.network_delegate()));
 
     switch (i) {
       case 0:
@@ -507,19 +513,19 @@ TEST_F(URLRequestThrottlerManagerTest, ClearOnNetworkChange) {
         break;
       case 1:
         manager.OnConnectionTypeChanged(
-            net::NetworkChangeNotifier::CONNECTION_UNKNOWN);
+            NetworkChangeNotifier::CONNECTION_UNKNOWN);
         break;
       case 2:
-        manager.OnConnectionTypeChanged(
-            net::NetworkChangeNotifier::CONNECTION_NONE);
+        manager.OnConnectionTypeChanged(NetworkChangeNotifier::CONNECTION_NONE);
         break;
       default:
         FAIL();
     }
 
-    scoped_refptr<net::URLRequestThrottlerEntryInterface> entry_after =
+    scoped_refptr<URLRequestThrottlerEntryInterface> entry_after =
         manager.RegisterRequestUrl(GURL("http://www.example.com/"));
-    EXPECT_FALSE(entry_after->ShouldRejectRequest(request_));
+    EXPECT_FALSE(entry_after->ShouldRejectRequest(
+                     *request_, context_.network_delegate()));
   }
 }