Commit c0dd4756 authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

Move away from using base::Clock in AutofillDownloadManager tests

This is a preparation CL to move AutofillDownloadManager from URLFetcher
to SimpleURLLoader et al. Specifically, it moves some autofill tests away
from using base::Clock, for testing whether a autofill query request is
properly cached/fetched. Still, both CACHE_HIT and CACHE_MISS functionalities
are exercised.

Basically, instead of setting the base::Clock instance with manually configured
valid and expired dates, the following was changed:

- the original test (AutofillQueryTest.CacheableResponse) was spun off into
another test: AutofillQueryTest.ExpiredCacheInResponse.

- The former sets a Cache-Control max-age value to some time in the future
(100 seconds), so that its cached content is valid/reached.

- The later sets a Cache-Control max-age value to 0, and delay execution in
a few milliseconds to ensure the cache content is already expired, and never
hit.

BUG=837830,844929

Change-Id: Ib82ca230fb0169b9e6847d90019574eb571e4c97
Reviewed-on: https://chromium-review.googlesource.com/1122456Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Cr-Commit-Position: refs/heads/master@{#572238}
parent 54e088f6
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
#include "base/test/scoped_command_line.h" #include "base/test/scoped_command_line.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
#include "base/test/simple_test_clock.h"
#include "base/test/test_timeouts.h" #include "base/test/test_timeouts.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/autofill/core/browser/autofill_field.h" #include "components/autofill/core/browser/autofill_field.h"
...@@ -775,10 +774,6 @@ class AutofillQueryTest : public AutofillDownloadManager::Observer, ...@@ -775,10 +774,6 @@ class AutofillQueryTest : public AutofillDownloadManager::Observer,
request_context_getter_ = request_context_getter_ =
base::MakeRefCounted<net::TestURLRequestContextGetter>( base::MakeRefCounted<net::TestURLRequestContextGetter>(
scoped_task_environment_.GetMainThreadTaskRunner()); scoped_task_environment_.GetMainThreadTaskRunner());
request_context_getter_->GetURLRequestContext()
->http_transaction_factory()
->GetCache()
->SetClockForTesting(&clock_);
driver_ = std::make_unique<TestAutofillDriver>(); driver_ = std::make_unique<TestAutofillDriver>();
driver_->SetURLRequestContext(request_context_getter_.get()); driver_->SetURLRequestContext(request_context_getter_.get());
} }
...@@ -812,8 +807,10 @@ class AutofillQueryTest : public AutofillDownloadManager::Observer, ...@@ -812,8 +807,10 @@ class AutofillQueryTest : public AutofillDownloadManager::Observer,
response->set_content_type("text/proto"); response->set_content_type("text/proto");
response->AddCustomHeader( response->AddCustomHeader(
"Cache-Control", "Cache-Control",
base::StringPrintf("max-age=%" PRId64, base::StringPrintf(
base::TimeDelta::FromDays(2).InSeconds())); "max-age=%" PRId64,
base::TimeDelta::FromMilliseconds(cache_expiration_in_milliseconds_)
.InSeconds()));
return response; return response;
} }
...@@ -834,9 +831,9 @@ class AutofillQueryTest : public AutofillDownloadManager::Observer, ...@@ -834,9 +831,9 @@ class AutofillQueryTest : public AutofillDownloadManager::Observer,
base::test::ScopedCommandLine scoped_command_line_; base::test::ScopedCommandLine scoped_command_line_;
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
EmbeddedTestServer server_; EmbeddedTestServer server_;
int cache_expiration_in_milliseconds_ = 100000;
std::unique_ptr<base::RunLoop> run_loop_; std::unique_ptr<base::RunLoop> run_loop_;
size_t call_count_ = 0; size_t call_count_ = 0;
base::SimpleTestClock clock_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
std::unique_ptr<TestAutofillDriver> driver_; std::unique_ptr<TestAutofillDriver> driver_;
}; };
...@@ -855,8 +852,6 @@ TEST_F(AutofillQueryTest, CacheableResponse) { ...@@ -855,8 +852,6 @@ TEST_F(AutofillQueryTest, CacheableResponse) {
std::vector<std::unique_ptr<FormStructure>> form_structures; std::vector<std::unique_ptr<FormStructure>> form_structures;
form_structures.push_back(std::make_unique<FormStructure>(form)); form_structures.push_back(std::make_unique<FormStructure>(form));
clock_.SetNow(base::Time::Now());
// Query for the form. This should go to the embedded server. // Query for the form. This should go to the embedded server.
{ {
SCOPED_TRACE("First Query"); SCOPED_TRACE("First Query");
...@@ -873,7 +868,6 @@ TEST_F(AutofillQueryTest, CacheableResponse) { ...@@ -873,7 +868,6 @@ TEST_F(AutofillQueryTest, CacheableResponse) {
// Query again the next day. This should go to the cache, since the max-age // Query again the next day. This should go to the cache, since the max-age
// for the cached response is 2 days. // for the cached response is 2 days.
{ {
clock_.Advance(base::TimeDelta::FromDays(1));
SCOPED_TRACE("Second Query"); SCOPED_TRACE("Second Query");
base::HistogramTester histogram; base::HistogramTester histogram;
call_count_ = 0; call_count_ = 0;
...@@ -884,13 +878,47 @@ TEST_F(AutofillQueryTest, CacheableResponse) { ...@@ -884,13 +878,47 @@ TEST_F(AutofillQueryTest, CacheableResponse) {
histogram.ExpectBucketCount("Autofill.Query.Method", METHOD_GET, 1); histogram.ExpectBucketCount("Autofill.Query.Method", METHOD_GET, 1);
histogram.ExpectBucketCount("Autofill.Query.WasInCache", CACHE_HIT, 1); histogram.ExpectBucketCount("Autofill.Query.WasInCache", CACHE_HIT, 1);
} }
}
// Query after another 2 days (a total of 3 days since the entry was cached). TEST_F(AutofillQueryTest, ExpiredCacheInResponse) {
// The cache entry had a max age of 2 days, so it should be expired. This FormFieldData field;
// should go to the embedded server. field.label = ASCIIToUTF16("First Name:");
field.name = ASCIIToUTF16("firstname");
field.form_control_type = "text";
FormData form;
form.fields.push_back(field);
std::vector<std::unique_ptr<FormStructure>> form_structures;
form_structures.push_back(std::make_unique<FormStructure>(form));
// Set the cache expiration interval to 0.
cache_expiration_in_milliseconds_ = 0;
// Query for the form. This should go to the embedded server.
{ {
clock_.Advance(base::TimeDelta::FromDays(2)); SCOPED_TRACE("First Query");
SCOPED_TRACE("Third Query"); base::HistogramTester histogram;
call_count_ = 0;
ASSERT_NO_FATAL_FAILURE(SendQueryRequest(form_structures));
EXPECT_EQ(1u, call_count_);
histogram.ExpectBucketCount("Autofill.ServerQueryResponse",
AutofillMetrics::QUERY_SENT, 1);
histogram.ExpectBucketCount("Autofill.Query.Method", METHOD_GET, 1);
histogram.ExpectBucketCount("Autofill.Query.WasInCache", CACHE_MISS, 1);
}
// The cache entry had a max age of 0 ms, so delaying only a few milliseconds
// ensures the cache expires and no request are served by cached content
// (ie this should go to the embedded server).
base::RunLoop run_loop;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(),
base::TimeDelta::FromMilliseconds(100));
run_loop.Run();
{
SCOPED_TRACE("Second Query");
base::HistogramTester histogram; base::HistogramTester histogram;
call_count_ = 0; call_count_ = 0;
ASSERT_NO_FATAL_FAILURE(SendQueryRequest(form_structures)); ASSERT_NO_FATAL_FAILURE(SendQueryRequest(form_structures));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment