Skip to content

Commit

Permalink
[Offline pages] Refactoring request_coordinator_unittest.cc to create…
Browse files Browse the repository at this point in the history
… RC builder

Extracting classes defined in the RC request_coordinator_unittest in
order to include them in test_request_coordinator_factory.

BUG=

Review-Url: https://codereview.chromium.org/2523603004
Cr-Commit-Position: refs/heads/master@{#434391}
  • Loading branch information
fgorski authored and Commit bot committed Nov 24, 2016
1 parent ff4bdcb commit 7fa3a9e
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
#include "chrome/browser/android/offline_pages/request_coordinator_factory.h"
#include "chrome/browser/android/offline_pages/test_offline_page_model_builder.h"
#include "chrome/browser/android/offline_pages/test_request_coordinator_builder.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/base/testing_profile.h"
#include "components/offline_pages/background/network_quality_provider_stub.h"
#include "components/offline_pages/background/request_coordinator.h"
#include "components/offline_pages/client_namespace_constants.h"
#include "components/offline_pages/offline_page_feature.h"
#include "components/offline_pages/offline_page_model.h"
Expand All @@ -42,6 +46,7 @@ const GURL kTestPage4Url("http://test.org/page4");
const int64_t kTestFileSize = 876543LL;
const char* kTestPage1ClientId = "1234";
const char* kTestPage2ClientId = "5678";
const char* kTestPage3ClientId = "7890";
const char* kTestPage4ClientId = "9876";

void HasDuplicatesCallback(bool* out_has_duplicates,
Expand Down Expand Up @@ -86,6 +91,7 @@ class OfflinePageUtilsTest

private:
void CreateOfflinePages();
void CreateRequests();
std::unique_ptr<OfflinePageTestArchiver> BuildArchiver(
const GURL& url,
const base::FilePath& file_name);
Expand All @@ -112,9 +118,15 @@ void OfflinePageUtilsTest::SetUp() {
&profile_, BuildTestOfflinePageModel);
RunUntilIdle();

// Make sure the store contains the right offline pages before the load
// happens.
NetworkQualityProviderStub::SetUserData(&profile_,
new NetworkQualityProviderStub());
RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse(
&profile_, BuildTestRequestCoordinator);
RunUntilIdle();

// Make sure to create offline pages and requests.
CreateOfflinePages();
CreateRequests();
}

void OfflinePageUtilsTest::RunUntilIdle() {
Expand Down Expand Up @@ -185,6 +197,19 @@ void OfflinePageUtilsTest::CreateOfflinePages() {
RunUntilIdle();
}

void OfflinePageUtilsTest::CreateRequests() {
RequestCoordinator* request_coordinator =
RequestCoordinatorFactory::GetForBrowserContext(profile());

offline_pages::ClientId client_id;
client_id.name_space = kDownloadNamespace;
client_id.id = kTestPage3ClientId;
request_coordinator->SavePageLater(
kTestPage3Url, client_id, true,
RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER);
RunUntilIdle();
}

std::unique_ptr<OfflinePageTestArchiver> OfflinePageUtilsTest::BuildArchiver(
const GURL& url,
const base::FilePath& file_name) {
Expand All @@ -205,15 +230,36 @@ TEST_F(OfflinePageUtilsTest, CheckExistenceOfPagesWithURL) {
base::Unretained(&latest_saved_time)));
RunUntilIdle();
EXPECT_TRUE(has_duplicates);
EXPECT_NE(base::Time(), latest_saved_time);
EXPECT_FALSE(latest_saved_time.is_null());
// This one should be missing
OfflinePageUtils::CheckExistenceOfPagesWithURL(
profile(), kDownloadNamespace, kTestPage3Url,
base::Bind(&HasDuplicatesCallback, base::Unretained(&has_duplicates),
base::Unretained(&latest_saved_time)));
RunUntilIdle();
EXPECT_FALSE(has_duplicates);
EXPECT_EQ(base::Time(), latest_saved_time);
EXPECT_TRUE(latest_saved_time.is_null());
}

TEST_F(OfflinePageUtilsTest, CheckExistenceOfRequestsWithURL) {
bool has_duplicates = false;
base::Time latest_saved_time;
// This page should be available.
OfflinePageUtils::CheckExistenceOfRequestsWithURL(
profile(), kDownloadNamespace, kTestPage3Url,
base::Bind(&HasDuplicatesCallback, base::Unretained(&has_duplicates),
base::Unretained(&latest_saved_time)));
RunUntilIdle();
EXPECT_TRUE(has_duplicates);
EXPECT_FALSE(latest_saved_time.is_null());
// This one should be missing
OfflinePageUtils::CheckExistenceOfRequestsWithURL(
profile(), kDownloadNamespace, kTestPage1Url,
base::Bind(&HasDuplicatesCallback, base::Unretained(&has_duplicates),
base::Unretained(&latest_saved_time)));
RunUntilIdle();
EXPECT_FALSE(has_duplicates);
EXPECT_TRUE(latest_saved_time.is_null());
}

} // namespace offline_pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2016 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 "chrome/browser/android/offline_pages/test_request_coordinator_builder.h"

#include <utility>

#include "components/offline_pages/background/network_quality_provider_stub.h"
#include "components/offline_pages/background/offliner_factory_stub.h"
#include "components/offline_pages/background/offliner_policy.h"
#include "components/offline_pages/background/request_coordinator.h"
#include "components/offline_pages/background/request_queue.h"
#include "components/offline_pages/background/request_queue_in_memory_store.h"
#include "components/offline_pages/background/scheduler_stub.h"
#include "content/public/browser/browser_context.h"

namespace offline_pages {

std::unique_ptr<KeyedService> BuildTestRequestCoordinator(
content::BrowserContext* context) {
// Use original policy.
std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy());

// Use the in-memory store.
std::unique_ptr<RequestQueueInMemoryStore> store(
new RequestQueueInMemoryStore());
// Use the regular test queue (should work).
std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(store)));

// Initialize the rest with stubs.
std::unique_ptr<OfflinerFactory> offliner_factory(new OfflinerFactoryStub());
std::unique_ptr<Scheduler> scheduler_stub(new SchedulerStub());

// NetworkQualityProviderStub should be set by the test on the context first.
NetworkQualityProviderStub* network_quality_provider =
NetworkQualityProviderStub::GetUserData(context);

return std::unique_ptr<RequestCoordinator>(new RequestCoordinator(
std::move(policy), std::move(offliner_factory), std::move(queue),
std::move(scheduler_stub), network_quality_provider));
}

} // namespace offline_pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2016 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.

#ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_TEST_REQUEST_COORDINATOR_BUILDER_H_
#define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_TEST_REQUEST_COORDINATOR_BUILDER_H_

#include <memory>

class KeyedService;

namespace content {
class BrowserContext;
}

namespace offline_pages {

// Helper function to be used with
// BrowserContextKeyedServiceFactory::SetTestingFactory() that returns a
// RequestCoordinator object with mocked store.
std::unique_ptr<KeyedService> BuildTestRequestCoordinator(
content::BrowserContext* context);

} // namespace offline_pages

#endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_TEST_REQUEST_COORDINATOR_BUILDER_H_
3 changes: 3 additions & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3521,13 +3521,16 @@ test("unit_tests") {
"../browser/android/offline_pages/request_coordinator_factory_unittest.cc",
"../browser/android/offline_pages/test_offline_page_model_builder.cc",
"../browser/android/offline_pages/test_offline_page_model_builder.h",
"../browser/android/offline_pages/test_request_coordinator_builder.cc",
"../browser/android/offline_pages/test_request_coordinator_builder.h",
]
sources -= [ "../browser/policy/policy_path_parser_unittest.cc" ]
deps += [
":unit_tests_java",
"//components/gcm_driver/instance_id/android:instance_id_driver_java",
"//components/gcm_driver/instance_id/android:instance_id_driver_test_support_java",
"//components/offline_pages:test_support",
"//components/offline_pages/background:test_support",
"//v8:v8_external_startup_data_assets",
]
deps -= [ "//third_party/libaddressinput" ]
Expand Down
21 changes: 21 additions & 0 deletions components/offline_pages/background/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ static_library("background_offliner") {
]
}

static_library("test_support") {
testonly = true
sources = [
"network_quality_provider_stub.cc",
"network_quality_provider_stub.h",
"offliner_factory_stub.cc",
"offliner_factory_stub.h",
"offliner_stub.cc",
"offliner_stub.h",
"scheduler_stub.cc",
"scheduler_stub.h",
]

deps = [
":background_offliner",
"//base",
"//net",
]
}

source_set("unit_tests") {
testonly = true
sources = [
Expand All @@ -76,6 +96,7 @@ source_set("unit_tests") {

deps = [
":background_offliner",
":test_support",
"//base",
"//base/test:test_support",
"//components/offline_pages:offline_pages",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2016 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 "components/offline_pages/background/network_quality_provider_stub.h"

namespace offline_pages {

const char kOfflineNQPKey[] = "OfflineNQP";

NetworkQualityProviderStub::NetworkQualityProviderStub()
: connection_type_(
net::EffectiveConnectionType::EFFECTIVE_CONNECTION_TYPE_3G) {}

NetworkQualityProviderStub::~NetworkQualityProviderStub() {}

// static
NetworkQualityProviderStub* NetworkQualityProviderStub::GetUserData(
base::SupportsUserData* supports_user_data) {
return static_cast<NetworkQualityProviderStub*>(
supports_user_data->GetUserData(&kOfflineNQPKey));
}

// static
void NetworkQualityProviderStub::SetUserData(
base::SupportsUserData* supports_user_data,
NetworkQualityProviderStub* stub) {
DCHECK(supports_user_data);
DCHECK(stub);
supports_user_data->SetUserData(&kOfflineNQPKey, stub);
}

void NetworkQualityProviderStub::AddEffectiveConnectionTypeObserver(
net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) {}

void NetworkQualityProviderStub::RemoveEffectiveConnectionTypeObserver(
net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer) {}

net::EffectiveConnectionType
NetworkQualityProviderStub::GetEffectiveConnectionType() const {
return connection_type_;
}
} // namespace offline_pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2016 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.

#ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_NETWORK_QUALITY_PROVIDER_STUB_H_
#define COMPONENTS_OFFLINE_PAGES_BACKGROUND_NETWORK_QUALITY_PROVIDER_STUB_H_

#include "base/supports_user_data.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator.h"

namespace offline_pages {

// Test class stubbing out the functionality of NQE::NetworkQualityProvider.
// It is only used for test support.
class NetworkQualityProviderStub
: public net::NetworkQualityEstimator::NetworkQualityProvider,
public base::SupportsUserData::Data {
public:
NetworkQualityProviderStub();
~NetworkQualityProviderStub() override;

static NetworkQualityProviderStub* GetUserData(
base::SupportsUserData* supports_user_data);
static void SetUserData(base::SupportsUserData* supports_user_data,
NetworkQualityProviderStub* stub);

net::EffectiveConnectionType GetEffectiveConnectionType() const override;

void AddEffectiveConnectionTypeObserver(
net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer)
override;

void RemoveEffectiveConnectionTypeObserver(
net::NetworkQualityEstimator::EffectiveConnectionTypeObserver* observer)
override;

void SetEffectiveConnectionTypeForTest(net::EffectiveConnectionType type) {
connection_type_ = type;
}

private:
net::EffectiveConnectionType connection_type_;
};

} // namespace offline_pages

#endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_NETWORK_QUALITY_PROVIDER_STUB_H_
22 changes: 22 additions & 0 deletions components/offline_pages/background/offliner_factory_stub.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2016 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 "components/offline_pages/background/offliner_factory_stub.h"

#include "components/offline_pages/background/offliner_stub.h"

namespace offline_pages {

OfflinerFactoryStub::OfflinerFactoryStub() : offliner_(nullptr) {}

OfflinerFactoryStub::~OfflinerFactoryStub() {}

Offliner* OfflinerFactoryStub::GetOffliner(const OfflinerPolicy* policy) {
if (offliner_.get() == nullptr) {
offliner_.reset(new OfflinerStub());
}
return offliner_.get();
}

} // namespace offline_pages
31 changes: 31 additions & 0 deletions components/offline_pages/background/offliner_factory_stub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2016 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.

#ifndef COMPONENTS_OFFLINE_PAGES_BACKGROUND_OFFLINER_FACTORY_STUB_H_
#define COMPONENTS_OFFLINE_PAGES_BACKGROUND_OFFLINER_FACTORY_STUB_H_

#include <memory>

#include "components/offline_pages/background/offliner_factory.h"

namespace offline_pages {

class OfflinerStub;

// Test class stubbing out the functionality of OfflinerFactory.
// It is only used for test support.
class OfflinerFactoryStub : public OfflinerFactory {
public:
OfflinerFactoryStub();
~OfflinerFactoryStub() override;

Offliner* GetOffliner(const OfflinerPolicy* policy) override;

private:
std::unique_ptr<OfflinerStub> offliner_;
};

} // namespace offline_pages

#endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_OFFLINER_FACTORY_STUB_H_
Loading

0 comments on commit 7fa3a9e

Please sign in to comment.