Commit 99a3034d authored by Vitalii Iarko's avatar Vitalii Iarko Committed by Commit Bot

[NTP::Push] Add SubscriptionManager interface.

This CL adds SubscriptionManager interface and renames the current
implementation to SubscriptionManagerImpl.

Bug: 745383
Change-Id: I1cbbb2b77e987a1d300f5926ae5bef8d20839dc1
Reviewed-on: https://chromium-review.googlesource.com/576027Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Commit-Queue: vitaliii <vitaliii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487462}
parent 8c909cb1
......@@ -42,6 +42,7 @@
#include "components/ntp_snippets/breaking_news/breaking_news_gcm_app_handler.h"
#include "components/ntp_snippets/breaking_news/breaking_news_suggestions_provider.h"
#include "components/ntp_snippets/breaking_news/subscription_manager.h"
#include "components/ntp_snippets/breaking_news/subscription_manager_impl.h"
#include "components/ntp_snippets/category_rankers/category_ranker.h"
#include "components/ntp_snippets/content_suggestions_service.h"
#include "components/ntp_snippets/features.h"
......@@ -114,7 +115,7 @@ using ntp_snippets::RemoteSuggestionsFetcherImpl;
using ntp_snippets::RemoteSuggestionsProviderImpl;
using ntp_snippets::RemoteSuggestionsSchedulerImpl;
using ntp_snippets::RemoteSuggestionsStatusService;
using ntp_snippets::SubscriptionManager;
using ntp_snippets::SubscriptionManagerImpl;
using ntp_snippets::TabDelegateSyncAdapter;
using ntp_snippets::UserClassifier;
using suggestions::ImageDecoderImpl;
......@@ -404,7 +405,7 @@ void SubscribeForGCMPushUpdates(
: google_apis::GetNonStableAPIKey();
}
auto subscription_manager = base::MakeUnique<SubscriptionManager>(
auto subscription_manager = base::MakeUnique<SubscriptionManagerImpl>(
request_context, pref_service, signin_manager, token_service, api_key,
GetPushUpdatesSubscriptionEndpoint(chrome::GetChannel()),
GetPushUpdatesUnsubscriptionEndpoint(chrome::GetChannel()));
......
......@@ -79,7 +79,7 @@
#include "components/language/core/browser/url_language_histogram.h"
#include "components/network_time/network_time_tracker.h"
#include "components/ntp_snippets/breaking_news/breaking_news_gcm_app_handler.h"
#include "components/ntp_snippets/breaking_news/subscription_manager.h"
#include "components/ntp_snippets/breaking_news/subscription_manager_impl.h"
#include "components/ntp_snippets/content_suggestions_service.h"
#include "components/ntp_snippets/remote/remote_suggestions_provider_impl.h"
#include "components/ntp_snippets/remote/remote_suggestions_scheduler_impl.h"
......@@ -477,7 +477,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
ntp_snippets::RemoteSuggestionsProviderImpl::RegisterProfilePrefs(registry);
ntp_snippets::RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs(registry);
ntp_snippets::RequestThrottler::RegisterProfilePrefs(registry);
ntp_snippets::SubscriptionManager::RegisterProfilePrefs(registry);
ntp_snippets::SubscriptionManagerImpl::RegisterProfilePrefs(registry);
ntp_snippets::UserClassifier::RegisterProfilePrefs(registry);
ntp_tiles::MostVisitedSites::RegisterProfilePrefs(registry);
password_bubble_experiment::RegisterPrefs(registry);
......
......@@ -24,6 +24,8 @@ static_library("ntp_snippets") {
"breaking_news/subscription_json_request.h",
"breaking_news/subscription_manager.cc",
"breaking_news/subscription_manager.h",
"breaking_news/subscription_manager_impl.cc",
"breaking_news/subscription_manager_impl.h",
"callbacks.h",
"category.cc",
"category.h",
......@@ -162,7 +164,7 @@ source_set("unit_tests") {
"bookmarks/bookmark_suggestions_provider_unittest.cc",
"breaking_news/breaking_news_suggestions_provider_unittest.cc",
"breaking_news/subscription_json_request_unittest.cc",
"breaking_news/subscription_manager_unittest.cc",
"breaking_news/subscription_manager_impl_unittest.cc",
"category_rankers/click_based_category_ranker_unittest.cc",
"category_rankers/constant_category_ranker_unittest.cc",
"category_unittest.cc",
......
......@@ -5,18 +5,11 @@
#ifndef COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_H_
#define COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_H_
#include "components/ntp_snippets/breaking_news/subscription_json_request.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include <string>
#include "components/version_info/version_info.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"
class AccessTokenFetcher;
class OAuth2TokenService;
class PrefRegistrySimple;
class PrefService;
namespace ntp_snippets {
// Returns the appropriate API endpoint for subscribing for push updates, in
......@@ -27,82 +20,23 @@ GURL GetPushUpdatesSubscriptionEndpoint(version_info::Channel channel);
// consideration of the channel and field trial parameters.
GURL GetPushUpdatesUnsubscriptionEndpoint(version_info::Channel channel);
// Class that wraps around the functionality of SubscriptionJsonRequest. It uses
// the SubscriptionJsonRequest to send subscription and unsubscription requests
// to the content suggestions server and does the bookkeeping for the data used
// for subscription. Bookkeeping is required to detect any change (e.g. the
// token render invalid), and resubscribe accordingly.
// Handles subscription to content suggestions server for push updates (e.g. via
// GCM).
class SubscriptionManager {
public:
SubscriptionManager(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
PrefService* pref_service,
SigninManagerBase* signin_manager,
OAuth2TokenService* access_token_service,
const std::string& api_key,
const GURL& subscribe_url,
const GURL& unsubscribe_url);
virtual ~SubscriptionManager() = default;
~SubscriptionManager();
virtual void Subscribe(const std::string& token) = 0;
virtual void Unsubscribe() = 0;
virtual bool IsSubscribed() = 0;
void Subscribe(const std::string& token);
void Unsubscribe();
bool IsSubscribed();
void Resubscribe(const std::string& new_token);
virtual void Resubscribe(const std::string& new_token) = 0;
// Checks if some data that has been used when subscribing has changed. For
// example, the user has signed in.
bool NeedsToResubscribe();
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
private:
class SigninObserver;
void SigninStatusChanged();
void DidSubscribe(const std::string& subscription_token,
bool is_authenticated,
const Status& status);
void DidUnsubscribe(const std::string& new_token, const Status& status);
void SubscribeInternal(const std::string& subscription_token,
const std::string& access_token);
// If |new_token| is empty, this will just unsubscribe. If |new_token| is
// non-empty, a subscription request with the |new_token| will be started upon
// successful unsubscription.
void ResubscribeInternal(const std::string& old_token,
const std::string& new_token);
// |subscription_token| is the token when subscribing after obtaining the
// access token.
void StartAccessTokenRequest(const std::string& subscription_token);
void AccessTokenFetchFinished(const std::string& subscription_token,
const GoogleServiceAuthError& error,
const std::string& access_token);
// Holds the URL request context.
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
std::unique_ptr<internal::SubscriptionJsonRequest> request_;
std::unique_ptr<AccessTokenFetcher> access_token_fetcher_;
PrefService* pref_service_;
// Authentication for signed-in users.
SigninManagerBase* signin_manager_;
std::unique_ptr<SigninObserver> signin_observer_;
OAuth2TokenService* access_token_service_;
// API key to use for non-authenticated requests.
const std::string api_key_;
virtual bool NeedsToResubscribe() = 0;
};
// API endpoint for subscribing and unsubscribing.
const GURL subscribe_url_;
const GURL unsubscribe_url_;
} // namespace ntp_snippets
DISALLOW_COPY_AND_ASSIGN(SubscriptionManager);
};
}
#endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_H_
// Copyright 2017 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/ntp_snippets/breaking_news/subscription_manager_impl.h"
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/stringprintf.h"
#include "components/ntp_snippets/breaking_news/subscription_json_request.h"
#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/ntp_snippets_constants.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/access_token_fetcher.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "net/base/url_util.h"
namespace ntp_snippets {
using internal::SubscriptionJsonRequest;
namespace {
const char kApiKeyParamName[] = "key";
const char kAuthorizationRequestHeaderFormat[] = "Bearer %s";
} // namespace
class SubscriptionManagerImpl::SigninObserver
: public SigninManagerBase::Observer {
public:
SigninObserver(SigninManagerBase* signin_manager,
const base::Closure& signin_status_changed_callback)
: signin_manager_(signin_manager),
signin_status_changed_callback_(signin_status_changed_callback) {
signin_manager_->AddObserver(this);
}
~SigninObserver() override { signin_manager_->RemoveObserver(this); }
private:
// SigninManagerBase::Observer implementation.
void GoogleSigninSucceeded(const std::string& account_id,
const std::string& username) override {
signin_status_changed_callback_.Run();
}
void GoogleSignedOut(const std::string& account_id,
const std::string& username) override {
signin_status_changed_callback_.Run();
}
SigninManagerBase* const signin_manager_;
base::Closure signin_status_changed_callback_;
};
SubscriptionManagerImpl::SubscriptionManagerImpl(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
PrefService* pref_service,
SigninManagerBase* signin_manager,
OAuth2TokenService* access_token_service,
const std::string& api_key,
const GURL& subscribe_url,
const GURL& unsubscribe_url)
: url_request_context_getter_(std::move(url_request_context_getter)),
pref_service_(pref_service),
signin_manager_(signin_manager),
signin_observer_(base::MakeUnique<SigninObserver>(
signin_manager,
base::Bind(&SubscriptionManagerImpl::SigninStatusChanged,
base::Unretained(this)))),
access_token_service_(access_token_service),
api_key_(api_key),
subscribe_url_(subscribe_url),
unsubscribe_url_(unsubscribe_url) {}
SubscriptionManagerImpl::~SubscriptionManagerImpl() = default;
void SubscriptionManagerImpl::Subscribe(const std::string& subscription_token) {
// If there is a request in flight, cancel it.
if (request_) {
request_ = nullptr;
}
if (signin_manager_->IsAuthenticated()) {
StartAccessTokenRequest(subscription_token);
} else {
SubscribeInternal(subscription_token, /*access_token=*/std::string());
}
}
void SubscriptionManagerImpl::SubscribeInternal(
const std::string& subscription_token,
const std::string& access_token) {
SubscriptionJsonRequest::Builder builder;
builder.SetToken(subscription_token)
.SetUrlRequestContextGetter(url_request_context_getter_);
if (!access_token.empty()) {
builder.SetUrl(subscribe_url_);
builder.SetAuthenticationHeader(base::StringPrintf(
kAuthorizationRequestHeaderFormat, access_token.c_str()));
} else {
// When not providing OAuth token, we need to pass the Google API key.
builder.SetUrl(
net::AppendQueryParameter(subscribe_url_, kApiKeyParamName, api_key_));
}
request_ = builder.Build();
request_->Start(base::BindOnce(&SubscriptionManagerImpl::DidSubscribe,
base::Unretained(this), subscription_token,
/*is_authenticated=*/!access_token.empty()));
}
void SubscriptionManagerImpl::StartAccessTokenRequest(
const std::string& subscription_token) {
// If there is already an ongoing token request, destroy it.
if (access_token_fetcher_) {
access_token_fetcher_ = nullptr;
}
OAuth2TokenService::ScopeSet scopes = {kContentSuggestionsApiScope};
access_token_fetcher_ = base::MakeUnique<AccessTokenFetcher>(
"ntp_snippets", signin_manager_, access_token_service_, scopes,
base::BindOnce(&SubscriptionManagerImpl::AccessTokenFetchFinished,
base::Unretained(this), subscription_token));
}
void SubscriptionManagerImpl::AccessTokenFetchFinished(
const std::string& subscription_token,
const GoogleServiceAuthError& error,
const std::string& access_token) {
// Delete the fetcher only after we leave this method (which is called from
// the fetcher itself).
std::unique_ptr<AccessTokenFetcher> access_token_fetcher_deleter(
std::move(access_token_fetcher_));
if (error.state() != GoogleServiceAuthError::NONE) {
// In case of error, we will retry on next Chrome restart.
return;
}
DCHECK(!access_token.empty());
SubscribeInternal(subscription_token, access_token);
}
void SubscriptionManagerImpl::DidSubscribe(
const std::string& subscription_token,
bool is_authenticated,
const Status& status) {
// Delete the request only after we leave this method (which is called from
// the request itself).
std::unique_ptr<internal::SubscriptionJsonRequest> request_deleter(
std::move(request_));
switch (status.code) {
case StatusCode::SUCCESS:
// In case of successful subscription, store the same data used for
// subscription in order to be able to resubscribe in case of data
// change.
// TODO(mamir): Store region and language.
pref_service_->SetString(prefs::kBreakingNewsSubscriptionDataToken,
subscription_token);
pref_service_->SetBoolean(
prefs::kBreakingNewsSubscriptionDataIsAuthenticated,
is_authenticated);
break;
default:
// TODO(mamir): Handle failure.
break;
}
}
void SubscriptionManagerImpl::Unsubscribe() {
std::string token =
pref_service_->GetString(prefs::kBreakingNewsSubscriptionDataToken);
ResubscribeInternal(/*old_token=*/token, /*new_token=*/std::string());
}
void SubscriptionManagerImpl::ResubscribeInternal(
const std::string& old_token,
const std::string& new_token) {
// If there is an request in flight, cancel it.
if (request_) {
request_ = nullptr;
}
SubscriptionJsonRequest::Builder builder;
builder.SetToken(old_token).SetUrlRequestContextGetter(
url_request_context_getter_);
builder.SetUrl(
net::AppendQueryParameter(unsubscribe_url_, kApiKeyParamName, api_key_));
request_ = builder.Build();
request_->Start(base::BindOnce(&SubscriptionManagerImpl::DidUnsubscribe,
base::Unretained(this), new_token));
}
bool SubscriptionManagerImpl::IsSubscribed() {
std::string subscription_token =
pref_service_->GetString(prefs::kBreakingNewsSubscriptionDataToken);
return !subscription_token.empty();
}
bool SubscriptionManagerImpl::NeedsToResubscribe() {
// Check if authentication state changed after subscription.
bool is_auth_subscribe = pref_service_->GetBoolean(
prefs::kBreakingNewsSubscriptionDataIsAuthenticated);
bool is_authenticated = signin_manager_->IsAuthenticated();
return is_auth_subscribe != is_authenticated;
}
void SubscriptionManagerImpl::Resubscribe(const std::string& new_token) {
std::string old_token =
pref_service_->GetString(prefs::kBreakingNewsSubscriptionDataToken);
if (old_token == new_token) {
// If the token didn't change, subscribe directly. The server handles the
// unsubscription if previous subscriptions exists.
Subscribe(old_token);
} else {
ResubscribeInternal(old_token, new_token);
}
}
void SubscriptionManagerImpl::DidUnsubscribe(const std::string& new_token,
const Status& status) {
// Delete the request only after we leave this method (which is called from
// the request itself).
std::unique_ptr<internal::SubscriptionJsonRequest> request_deleter(
std::move(request_));
switch (status.code) {
case StatusCode::SUCCESS:
// In case of successful unsubscription, clear the previously stored data.
// TODO(mamir): Clear stored region and language.
pref_service_->ClearPref(prefs::kBreakingNewsSubscriptionDataToken);
pref_service_->ClearPref(
prefs::kBreakingNewsSubscriptionDataIsAuthenticated);
if (!new_token.empty()) {
Subscribe(new_token);
}
break;
default:
// TODO(mamir): Handle failure.
break;
}
}
void SubscriptionManagerImpl::SigninStatusChanged() {
// If subscribed already, resubscribe.
if (IsSubscribed()) {
if (request_) {
request_ = nullptr;
}
std::string token =
pref_service_->GetString(prefs::kBreakingNewsSubscriptionDataToken);
Subscribe(token);
}
}
void SubscriptionManagerImpl::RegisterProfilePrefs(
PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kBreakingNewsSubscriptionDataToken,
std::string());
registry->RegisterBooleanPref(
prefs::kBreakingNewsSubscriptionDataIsAuthenticated, false);
}
} // namespace ntp_snippets
// Copyright 2017 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_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_IMPL_H_
#define COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_IMPL_H_
#include <memory>
#include <string>
#include "base/memory/ref_counted.h"
#include "components/ntp_snippets/breaking_news/subscription_json_request.h"
#include "components/ntp_snippets/breaking_news/subscription_manager.h"
#include "components/signin/core/browser/signin_manager_base.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"
class AccessTokenFetcher;
class OAuth2TokenService;
class PrefRegistrySimple;
class PrefService;
namespace ntp_snippets {
// Class that wraps around the functionality of SubscriptionJsonRequest. It uses
// the SubscriptionJsonRequest to send subscription and unsubscription requests
// to the content suggestions server and does the bookkeeping for the data used
// for subscription. Bookkeeping is required to detect any change (e.g. the
// token render invalid), and resubscribe accordingly.
class SubscriptionManagerImpl : public SubscriptionManager {
public:
SubscriptionManagerImpl(
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter,
PrefService* pref_service,
SigninManagerBase* signin_manager,
OAuth2TokenService* access_token_service,
const std::string& api_key,
const GURL& subscribe_url,
const GURL& unsubscribe_url);
~SubscriptionManagerImpl() override;
// SubscriptionManager implementation.
void Subscribe(const std::string& token) override;
void Unsubscribe() override;
bool IsSubscribed() override;
void Resubscribe(const std::string& new_token) override;
// Checks if some data that has been used when subscribing has changed. For
// example, the user has signed in.
bool NeedsToResubscribe() override;
static void RegisterProfilePrefs(PrefRegistrySimple* registry);
private:
class SigninObserver;
void SigninStatusChanged();
void DidSubscribe(const std::string& subscription_token,
bool is_authenticated,
const Status& status);
void DidUnsubscribe(const std::string& new_token, const Status& status);
void SubscribeInternal(const std::string& subscription_token,
const std::string& access_token);
// If |new_token| is empty, this will just unsubscribe. If |new_token| is
// non-empty, a subscription request with the |new_token| will be started upon
// successful unsubscription.
void ResubscribeInternal(const std::string& old_token,
const std::string& new_token);
// |subscription_token| is the token when subscribing after obtaining the
// access token.
void StartAccessTokenRequest(const std::string& subscription_token);
void AccessTokenFetchFinished(const std::string& subscription_token,
const GoogleServiceAuthError& error,
const std::string& access_token);
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
std::unique_ptr<internal::SubscriptionJsonRequest> request_;
std::unique_ptr<AccessTokenFetcher> access_token_fetcher_;
PrefService* pref_service_;
// Authentication for signed-in users.
SigninManagerBase* signin_manager_;
std::unique_ptr<SigninObserver> signin_observer_;
OAuth2TokenService* access_token_service_;
// API key to use for non-authenticated requests.
const std::string api_key_;
// API endpoint for subscribing and unsubscribing.
const GURL subscribe_url_;
const GURL unsubscribe_url_;
DISALLOW_COPY_AND_ASSIGN(SubscriptionManagerImpl);
};
} // namespace ntp_snippets
#endif // COMPONENTS_NTP_SNIPPETS_BREAKING_NEWS_SUBSCRIPTION_MANAGER_IMPL_H_
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/ntp_snippets/breaking_news/subscription_manager.h"
#include "components/ntp_snippets/breaking_news/subscription_manager_impl.h"
#include "base/message_loop/message_loop.h"
#include "build/build_config.h"
......@@ -31,15 +31,15 @@ const char kUnsubscriptionUrlSignedIn[] = "http://valid-url.test/unsubscribe";
const char kUnsubscriptionUrlSignedOut[] =
"http://valid-url.test/unsubscribe?key=fakeAPIkey";
class SubscriptionManagerTest : public testing::Test {
class SubscriptionManagerImplTest : public testing::Test {
public:
SubscriptionManagerTest()
SubscriptionManagerImplTest()
: request_context_getter_(
new net::TestURLRequestContextGetter(message_loop_.task_runner())) {
}
void SetUp() override {
SubscriptionManager::RegisterProfilePrefs(
SubscriptionManagerImpl::RegisterProfilePrefs(
utils_.pref_service()->registry());
}
......@@ -141,19 +141,18 @@ class SubscriptionManagerTest : public testing::Test {
url_fetcher->delegate()->OnURLFetchComplete(url_fetcher);
}
base::MessageLoop message_loop_;
test::RemoteSuggestionsTestUtils utils_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
net::TestURLFetcherFactory url_fetcher_factory_;
};
TEST_F(SubscriptionManagerTest, SubscribeSuccessfully) {
TEST_F(SubscriptionManagerImplTest, SubscribeSuccessfully) {
std::string subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(subscription_token);
RespondToSubscriptionRequestSuccessfully(/*is_signed_in=*/false);
ASSERT_TRUE(manager.IsSubscribed());
......@@ -166,7 +165,7 @@ TEST_F(SubscriptionManagerTest, SubscribeSuccessfully) {
// This test is relevant only on non-ChromeOS platforms, as the flow being
// tested here is not possible on ChromeOS.
#if !defined(OS_CHROMEOS)
TEST_F(SubscriptionManagerTest,
TEST_F(SubscriptionManagerImplTest,
ShouldSubscribeWithAuthenticationWhenAuthenticated) {
// Sign in.
FakeProfileOAuth2TokenService* auth_token_service = GetOAuth2TokenService();
......@@ -175,9 +174,10 @@ TEST_F(SubscriptionManagerTest,
// Create manager and subscribe.
std::string subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), auth_token_service, kAPIKey,
GURL(kSubscriptionUrl), GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), auth_token_service,
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(subscription_token);
// Make sure that subscription is pending an access token.
......@@ -198,24 +198,24 @@ TEST_F(SubscriptionManagerTest,
}
#endif
TEST_F(SubscriptionManagerTest, ShouldNotSubscribeIfError) {
TEST_F(SubscriptionManagerImplTest, ShouldNotSubscribeIfError) {
std::string subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(subscription_token);
RespondToSubscriptionWithError(/*is_signed_in=*/false, net::ERR_TIMED_OUT);
EXPECT_FALSE(manager.IsSubscribed());
}
TEST_F(SubscriptionManagerTest, UnsubscribeSuccessfully) {
TEST_F(SubscriptionManagerImplTest, UnsubscribeSuccessfully) {
std::string subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(subscription_token);
RespondToSubscriptionRequestSuccessfully(/*is_signed_in=*/false);
ASSERT_TRUE(manager.IsSubscribed());
......@@ -226,13 +226,13 @@ TEST_F(SubscriptionManagerTest, UnsubscribeSuccessfully) {
GetPrefService()->HasPrefPath(prefs::kBreakingNewsSubscriptionDataToken));
}
TEST_F(SubscriptionManagerTest,
TEST_F(SubscriptionManagerImplTest,
ShouldRemainSubscribedIfErrorDuringUnsubscribe) {
std::string subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(subscription_token);
RespondToSubscriptionRequestSuccessfully(/*is_signed_in=*/false);
ASSERT_TRUE(manager.IsSubscribed());
......@@ -246,13 +246,15 @@ TEST_F(SubscriptionManagerTest,
// This test is relevant only on non-ChromeOS platforms, as the flow being
// tested here is not possible on ChromeOS.
#if !defined(OS_CHROMEOS)
TEST_F(SubscriptionManagerTest, ShouldResubscribeIfSignInAfterSubscription) {
TEST_F(SubscriptionManagerImplTest,
ShouldResubscribeIfSignInAfterSubscription) {
// Create manager and subscribe.
FakeProfileOAuth2TokenService* auth_token_service = GetOAuth2TokenService();
std::string subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), auth_token_service, kAPIKey,
GURL(kSubscriptionUrl), GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), auth_token_service,
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(subscription_token);
RespondToSubscriptionRequestSuccessfully(/*is_signed_in=*/false);
ASSERT_FALSE(manager.NeedsToResubscribe());
......@@ -274,15 +276,17 @@ TEST_F(SubscriptionManagerTest, ShouldResubscribeIfSignInAfterSubscription) {
// This test is relevant only on non-ChromeOS platforms, as the flow being
// tested here is not possible on ChromeOS.
#if !defined(OS_CHROMEOS)
TEST_F(SubscriptionManagerTest, ShouldResubscribeIfSignOutAfterSubscription) {
TEST_F(SubscriptionManagerImplTest,
ShouldResubscribeIfSignOutAfterSubscription) {
// Signin and subscribe.
FakeProfileOAuth2TokenService* auth_token_service = GetOAuth2TokenService();
SignIn();
IssueRefreshToken(auth_token_service);
std::string subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), auth_token_service, kAPIKey,
GURL(kSubscriptionUrl), GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), auth_token_service,
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(subscription_token);
ASSERT_EQ(1u, auth_token_service->GetPendingRequests().size());
IssueAccessToken(auth_token_service);
......@@ -299,14 +303,14 @@ TEST_F(SubscriptionManagerTest, ShouldResubscribeIfSignOutAfterSubscription) {
}
#endif
TEST_F(SubscriptionManagerTest,
TEST_F(SubscriptionManagerImplTest,
ShouldUpdateTokenInPrefWhenResubscribeWithChangeInToken) {
// Create manager and subscribe.
std::string old_subscription_token = "1234567890";
SubscriptionManager manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
SubscriptionManagerImpl manager(GetRequestContext(), GetPrefService(),
GetSigninManager(), GetOAuth2TokenService(),
kAPIKey, GURL(kSubscriptionUrl),
GURL(kUnsubscriptionUrl));
manager.Subscribe(old_subscription_token);
RespondToSubscriptionRequestSuccessfully(/*is_signed_in=*/false);
EXPECT_EQ(
......
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