Commit 297c4e75 authored by Carlos Caballero's avatar Carlos Caballero Committed by Commit Bot

[scheduling] Use ScopedTaskEnvironment instead of MessageLoop in ntp_snippets

MessageLoop will go away, eventually.

BUG=891670

Change-Id: I443fe09079186ef8a2e2680efd6346b5f509b61c
Reviewed-on: https://chromium-review.googlesource.com/c/1352780Reviewed-by: default avatarPatrick Noland <pnoland@chromium.org>
Reviewed-by: default avatarSami Kyöstilä <skyostil@chromium.org>
Commit-Queue: Carlos Caballero <carlscab@google.com>
Cr-Commit-Position: refs/heads/master@{#615986}
parent 2e5e1813
...@@ -95,7 +95,7 @@ BreakingNewsGCMAppHandler::BreakingNewsGCMAppHandler( ...@@ -95,7 +95,7 @@ BreakingNewsGCMAppHandler::BreakingNewsGCMAppHandler(
PrefService* pref_service, PrefService* pref_service,
std::unique_ptr<SubscriptionManager> subscription_manager, std::unique_ptr<SubscriptionManager> subscription_manager,
const ParseJSONCallback& parse_json_callback, const ParseJSONCallback& parse_json_callback,
base::Clock* clock, const base::Clock* clock,
std::unique_ptr<base::OneShotTimer> token_validation_timer, std::unique_ptr<base::OneShotTimer> token_validation_timer,
std::unique_ptr<base::OneShotTimer> forced_subscription_timer) std::unique_ptr<base::OneShotTimer> forced_subscription_timer)
: gcm_driver_(gcm_driver), : gcm_driver_(gcm_driver),
......
...@@ -53,7 +53,7 @@ class BreakingNewsGCMAppHandler : public BreakingNewsListener, ...@@ -53,7 +53,7 @@ class BreakingNewsGCMAppHandler : public BreakingNewsListener,
PrefService* pref_service_, PrefService* pref_service_,
std::unique_ptr<SubscriptionManager> subscription_manager, std::unique_ptr<SubscriptionManager> subscription_manager,
const ParseJSONCallback& parse_json_callback, const ParseJSONCallback& parse_json_callback,
base::Clock* clock, const base::Clock* clock,
std::unique_ptr<base::OneShotTimer> token_validation_timer, std::unique_ptr<base::OneShotTimer> token_validation_timer,
std::unique_ptr<base::OneShotTimer> forced_subscription_timer); std::unique_ptr<base::OneShotTimer> forced_subscription_timer);
...@@ -128,7 +128,7 @@ class BreakingNewsGCMAppHandler : public BreakingNewsListener, ...@@ -128,7 +128,7 @@ class BreakingNewsGCMAppHandler : public BreakingNewsListener,
PrefService* const pref_service_; PrefService* const pref_service_;
const std::unique_ptr<SubscriptionManager> subscription_manager_; const std::unique_ptr<SubscriptionManager> subscription_manager_;
const ParseJSONCallback parse_json_callback_; const ParseJSONCallback parse_json_callback_;
base::Clock* clock_; const base::Clock* clock_;
// Called every time a push-by-value message is received to notify the // Called every time a push-by-value message is received to notify the
// observer. // observer.
......
...@@ -9,9 +9,9 @@ ...@@ -9,9 +9,9 @@
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/message_loop/message_loop.h"
#include "base/test/metrics/histogram_tester.h" #include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h" #include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h"
#include "base/test/simple_test_clock.h" #include "base/test/simple_test_clock.h"
#include "base/test/test_mock_time_task_runner.h" #include "base/test/test_mock_time_task_runner.h"
#include "base/time/clock.h" #include "base/time/clock.h"
...@@ -214,12 +214,6 @@ ACTION_TEMPLATE(InvokeCallbackArgument, ...@@ -214,12 +214,6 @@ ACTION_TEMPLATE(InvokeCallbackArgument,
std::get<k>(args).Run(p0, p1); std::get<k>(args).Run(p0, p1);
} }
base::Time GetDummyNow() {
base::Time out_time;
EXPECT_TRUE(base::Time::FromUTCString("2017-01-02T00:00:01Z", &out_time));
return out_time;
}
base::TimeDelta GetTokenValidationPeriod() { base::TimeDelta GetTokenValidationPeriod() {
return base::TimeDelta::FromHours(24); return base::TimeDelta::FromHours(24);
} }
...@@ -238,7 +232,10 @@ std::string BoolToString(bool value) { ...@@ -238,7 +232,10 @@ std::string BoolToString(bool value) {
class BreakingNewsGCMAppHandlerTest : public testing::Test { class BreakingNewsGCMAppHandlerTest : public testing::Test {
public: public:
void SetUp() override { BreakingNewsGCMAppHandlerTest()
: scoped_task_environment_(
base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME),
start_time_(scoped_task_environment_.GetMockClock()->Now()) {
// Our app handler obtains InstanceID through InstanceIDDriver. We mock // Our app handler obtains InstanceID through InstanceIDDriver. We mock
// InstanceIDDriver and return MockInstanceID through it. // InstanceIDDriver and return MockInstanceID through it.
mock_instance_id_driver_ = mock_instance_id_driver_ =
...@@ -253,10 +250,7 @@ class BreakingNewsGCMAppHandlerTest : public testing::Test { ...@@ -253,10 +250,7 @@ class BreakingNewsGCMAppHandlerTest : public testing::Test {
.WillRepeatedly(Return(mock_instance_id_.get())); .WillRepeatedly(Return(mock_instance_id_.get()));
} }
std::unique_ptr<BreakingNewsGCMAppHandler> MakeHandler( std::unique_ptr<BreakingNewsGCMAppHandler> MakeHandler() {
scoped_refptr<TestMockTimeTaskRunner> timer_mock_task_runner) {
message_loop_.SetTaskRunner(timer_mock_task_runner);
// TODO(vitaliii): Initialize MockSubscriptionManager in the constructor, so // TODO(vitaliii): Initialize MockSubscriptionManager in the constructor, so
// that one could set up expectations before creating the handler. // that one could set up expectations before creating the handler.
auto wrapped_mock_subscription_manager = auto wrapped_mock_subscription_manager =
...@@ -264,17 +258,19 @@ class BreakingNewsGCMAppHandlerTest : public testing::Test { ...@@ -264,17 +258,19 @@ class BreakingNewsGCMAppHandlerTest : public testing::Test {
mock_subscription_manager_ = wrapped_mock_subscription_manager.get(); mock_subscription_manager_ = wrapped_mock_subscription_manager.get();
auto token_validation_timer = std::make_unique<base::OneShotTimer>( auto token_validation_timer = std::make_unique<base::OneShotTimer>(
timer_mock_task_runner->GetMockTickClock()); scoped_task_environment_.GetMockTickClock());
token_validation_timer->SetTaskRunner(timer_mock_task_runner); token_validation_timer->SetTaskRunner(
scoped_task_environment_.GetMainThreadTaskRunner());
auto forced_subscription_timer = std::make_unique<base::OneShotTimer>( auto forced_subscription_timer = std::make_unique<base::OneShotTimer>(
timer_mock_task_runner->GetMockTickClock()); scoped_task_environment_.GetMockTickClock());
forced_subscription_timer->SetTaskRunner(timer_mock_task_runner); forced_subscription_timer->SetTaskRunner(
scoped_task_environment_.GetMainThreadTaskRunner());
return std::make_unique<BreakingNewsGCMAppHandler>( return std::make_unique<BreakingNewsGCMAppHandler>(
mock_gcm_driver_.get(), mock_instance_id_driver_.get(), pref_service(), mock_gcm_driver_.get(), mock_instance_id_driver_.get(), pref_service(),
std::move(wrapped_mock_subscription_manager), base::Bind(&ParseJson), std::move(wrapped_mock_subscription_manager), base::Bind(&ParseJson),
timer_mock_task_runner->GetMockClock(), scoped_task_environment_.GetMockClock(),
std::move(token_validation_timer), std::move(token_validation_timer),
std::move(forced_subscription_timer)); std::move(forced_subscription_timer));
} }
...@@ -303,9 +299,18 @@ class BreakingNewsGCMAppHandlerTest : public testing::Test { ...@@ -303,9 +299,18 @@ class BreakingNewsGCMAppHandlerTest : public testing::Test {
return mock_instance_id_.get(); return mock_instance_id_.get();
} }
void RunUntilIdle() { scoped_task_environment_.RunUntilIdle(); }
void FastForwardBy(const base::TimeDelta& delta) {
scoped_task_environment_.FastForwardBy(delta);
}
base::Time GetDummyNow() { return start_time_; }
private: private:
base::test::ScopedTaskEnvironment scoped_task_environment_;
const base::Time start_time_;
variations::testing::VariationParamsManager params_manager_; variations::testing::VariationParamsManager params_manager_;
base::MessageLoop message_loop_;
test::RemoteSuggestionsTestUtils utils_; test::RemoteSuggestionsTestUtils utils_;
NiceMock<MockSubscriptionManager>* mock_subscription_manager_; NiceMock<MockSubscriptionManager>* mock_subscription_manager_;
std::unique_ptr<StrictMock<MockGCMDriver>> mock_gcm_driver_; std::unique_ptr<StrictMock<MockGCMDriver>> mock_gcm_driver_;
...@@ -326,9 +331,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -326,9 +331,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
// Omit receiving the token by putting it there directly. // Omit receiving the token by putting it there directly.
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
// Check that the handler validates the token through GetToken. ValidateToken // Check that the handler validates the token through GetToken. ValidateToken
// always returns true on Android, so it's not useful. Instead, the handler // always returns true on Android, so it's not useful. Instead, the handler
...@@ -338,7 +341,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -338,7 +341,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
task_runner->RunUntilIdle(); RunUntilIdle();
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -356,22 +359,19 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -356,22 +359,19 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
// Check that handler does not validate the token yet. // Check that handler does not validate the token yet.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0);
EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
task_runner->FastForwardBy(time_to_validation - FastForwardBy(time_to_validation - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
// But when it is time, validation happens. // But when it is time, validation happens.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce( .WillOnce(
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
} }
TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotValidateTokenBeforeListening) { TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotValidateTokenBeforeListening) {
...@@ -384,15 +384,12 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotValidateTokenBeforeListening) { ...@@ -384,15 +384,12 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotValidateTokenBeforeListening) {
pref_service()->SetInt64(prefs::kBreakingNewsGCMLastTokenValidationTime, pref_service()->SetInt64(prefs::kBreakingNewsGCMLastTokenValidationTime,
SerializeTime(last_validation)); SerializeTime(last_validation));
scoped_refptr<TestMockTimeTaskRunner> task_runner(
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
// Check that handler does not validate the token before StartListening even // Check that handler does not validate the token before StartListening even
// though a validation is due. // though a validation is due.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0);
EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0);
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner); std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
task_runner->FastForwardBy(10 * GetTokenValidationPeriod()); FastForwardBy(10 * GetTokenValidationPeriod());
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -410,17 +407,14 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -410,17 +407,14 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner(
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
// Check that handler does not validate the token after StopListening even // Check that handler does not validate the token after StopListening even
// though a validation is due. // though a validation is due.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0);
EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0);
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner); std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
handler->StopListening(); handler->StopListening();
task_runner->FastForwardBy(10 * GetTokenValidationPeriod()); FastForwardBy(10 * GetTokenValidationPeriod());
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -435,9 +429,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -435,9 +429,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetInt64(prefs::kBreakingNewsGCMLastTokenValidationTime, pref_service()->SetInt64(prefs::kBreakingNewsGCMLastTokenValidationTime,
SerializeTime(last_validation)); SerializeTime(last_validation));
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
// There is no token yet, thus, handler retrieves it on StartListening. // There is no token yet, thus, handler retrieves it on StartListening.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
...@@ -448,14 +440,13 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -448,14 +440,13 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
// Check that the validation schedule has changed. "Old validation" should not // Check that the validation schedule has changed. "Old validation" should not
// happen because the token was retrieved recently. // happen because the token was retrieved recently.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0);
task_runner->FastForwardBy(GetTokenValidationPeriod() - FastForwardBy(GetTokenValidationPeriod() - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
// The new validation should happen. // The new validation should happen.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce( .WillOnce(
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -473,9 +464,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -473,9 +464,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
// Handler validates the token. // Handler validates the token.
...@@ -483,17 +472,16 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -483,17 +472,16 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
.WillOnce( .WillOnce(
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0);
task_runner->FastForwardBy(time_to_validation); FastForwardBy(time_to_validation);
// Check that the next validation is scheduled in time. // Check that the next validation is scheduled in time.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0);
task_runner->FastForwardBy(GetTokenValidationPeriod() - FastForwardBy(GetTokenValidationPeriod() - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce( .WillOnce(
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -510,9 +498,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -510,9 +498,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"old_token"); "old_token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
// Check that handler resubscribes with the new token after a validation, if // Check that handler resubscribes with the new token after a validation, if
...@@ -523,7 +509,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -523,7 +509,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0);
EXPECT_CALL(*mock_subscription_manager(), Resubscribe("new_token")); EXPECT_CALL(*mock_subscription_manager(), Resubscribe("new_token"));
EXPECT_CALL(*mock_subscription_manager(), Subscribe(_)).Times(0); EXPECT_CALL(*mock_subscription_manager(), Subscribe(_)).Times(0);
task_runner->RunUntilIdle(); RunUntilIdle();
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -540,9 +526,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -540,9 +526,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
// Check that provider does not resubscribe if the old token is still valid // Check that provider does not resubscribe if the old token is still valid
...@@ -553,7 +537,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -553,7 +537,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), ValidateToken(_, _, _, _)).Times(0);
EXPECT_CALL(*mock_subscription_manager(), Subscribe(_)).Times(0); EXPECT_CALL(*mock_subscription_manager(), Subscribe(_)).Times(0);
EXPECT_CALL(*mock_subscription_manager(), Resubscribe(_)).Times(0); EXPECT_CALL(*mock_subscription_manager(), Resubscribe(_)).Times(0);
task_runner->RunUntilIdle(); RunUntilIdle();
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -570,9 +554,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -570,9 +554,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetInt64(prefs::kBreakingNewsGCMLastTokenValidationTime, pref_service()->SetInt64(prefs::kBreakingNewsGCMLastTokenValidationTime,
SerializeTime(last_validation)); SerializeTime(last_validation));
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
// Save the GetToken callback during the subscription. // Save the GetToken callback during the subscription.
base::RepeatingCallback<void(const std::string&, InstanceID::Result)> base::RepeatingCallback<void(const std::string&, InstanceID::Result)>
...@@ -589,7 +571,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -589,7 +571,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
// The GCM returns the new token (it does not know that we are not interested // The GCM returns the new token (it does not know that we are not interested
// anymore). Imitate an asynchronous call via time delay. // anymore). Imitate an asynchronous call via time delay.
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
get_token_callback.Run("new_token", InstanceID::Result::SUCCESS); get_token_callback.Run("new_token", InstanceID::Result::SUCCESS);
// The new token must be completely ignored. It should not be saved. // The new token must be completely ignored. It should not be saved.
...@@ -616,30 +598,27 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -616,30 +598,27 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"old_token"); "old_token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening( handler->StartListening(
base::BindRepeating( base::BindRepeating(
[](std::unique_ptr<RemoteSuggestion> remote_suggestion) {}), [](std::unique_ptr<RemoteSuggestion> remote_suggestion) {}),
base::DoNothing()); base::DoNothing());
task_runner->FastForwardBy(time_to_validation - FastForwardBy(time_to_validation - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
// Save the GetToken callback during the validation. // Save the GetToken callback during the validation.
base::RepeatingCallback<void(const std::string&, InstanceID::Result)> base::RepeatingCallback<void(const std::string&, InstanceID::Result)>
get_token_callback; get_token_callback;
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce(SaveArg<4>(&get_token_callback)); .WillOnce(SaveArg<4>(&get_token_callback));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
// The client stops listening for the push updates. // The client stops listening for the push updates.
handler->StopListening(); handler->StopListening();
// The GCM returns the new token (it does not know that we are not interested // The GCM returns the new token (it does not know that we are not interested
// anymore). // anymore).
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
get_token_callback.Run("new_token", InstanceID::Result::SUCCESS); get_token_callback.Run("new_token", InstanceID::Result::SUCCESS);
// The new token must be completely ignored. It should not be saved. // The new token must be completely ignored. It should not be saved.
...@@ -656,9 +635,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -656,9 +635,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
SetFeatureParams(/*enable_token_validation=*/false, SetFeatureParams(/*enable_token_validation=*/false,
/*enable_forced_subscription=*/false); /*enable_forced_subscription=*/false);
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
EXPECT_FALSE(handler->IsListening()); EXPECT_FALSE(handler->IsListening());
} }
...@@ -668,9 +645,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -668,9 +645,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
SetFeatureParams(/*enable_token_validation=*/false, SetFeatureParams(/*enable_token_validation=*/false,
/*enable_forced_subscription=*/false); /*enable_forced_subscription=*/false);
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
ASSERT_FALSE(handler->IsListening()); ASSERT_FALSE(handler->IsListening());
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
...@@ -686,9 +661,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -686,9 +661,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
SetFeatureParams(/*enable_token_validation=*/false, SetFeatureParams(/*enable_token_validation=*/false,
/*enable_forced_subscription=*/false); /*enable_forced_subscription=*/false);
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
ASSERT_FALSE(handler->IsListening()); ASSERT_FALSE(handler->IsListening());
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
...@@ -707,9 +680,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -707,9 +680,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
SetFeatureParams(/*enable_token_validation=*/false, SetFeatureParams(/*enable_token_validation=*/false,
/*enable_forced_subscription=*/false); /*enable_forced_subscription=*/false);
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
ASSERT_FALSE(handler->IsListening()); ASSERT_FALSE(handler->IsListening());
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
...@@ -738,13 +709,11 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldForceSubscribeImmediatelyIfDue) { ...@@ -738,13 +709,11 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldForceSubscribeImmediatelyIfDue) {
// Omit receiving the token by putting it there directly. // Omit receiving the token by putting it there directly.
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token"));
task_runner->RunUntilIdle(); RunUntilIdle();
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -761,9 +730,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -761,9 +730,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
// Omit receiving the token by putting it there directly. // Omit receiving the token by putting it there directly.
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
// Check that handler does not force subscribe yet. // Check that handler does not force subscribe yet.
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
...@@ -771,13 +738,12 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -771,13 +738,12 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
// IsSubscribed() affects forced subscriptions. Currently we have to carefully // IsSubscribed() affects forced subscriptions. Currently we have to carefully
// avoid the initial subscription. // avoid the initial subscription.
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0);
task_runner->FastForwardBy(time_to_subscription - FastForwardBy(time_to_subscription - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
// But when it is time, forced subscription happens. // But when it is time, forced subscription happens.
testing::Mock::VerifyAndClearExpectations(mock_subscription_manager()); testing::Mock::VerifyAndClearExpectations(mock_subscription_manager());
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token"));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
} }
TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotForceSubscribeBeforeListening) { TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotForceSubscribeBeforeListening) {
...@@ -792,14 +758,12 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotForceSubscribeBeforeListening) { ...@@ -792,14 +758,12 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldNotForceSubscribeBeforeListening) {
// Omit receiving the token by putting it there directly. // Omit receiving the token by putting it there directly.
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner(
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
// Check that handler does not force subscribe before StartListening even // Check that handler does not force subscribe before StartListening even
// though a forced subscription is due. // though a forced subscription is due.
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner); std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0);
task_runner->FastForwardBy(10 * GetForcedSubscriptionPeriod()); FastForwardBy(10 * GetForcedSubscriptionPeriod());
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -816,16 +780,14 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -816,16 +780,14 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
// Omit receiving the token by putting it there directly. // Omit receiving the token by putting it there directly.
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner(
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
// Check that handler does not force subscribe after StopListening even // Check that handler does not force subscribe after StopListening even
// though a forced subscription is due. // though a forced subscription is due.
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner); std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
handler->StopListening(); handler->StopListening();
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0);
task_runner->FastForwardBy(10 * GetForcedSubscriptionPeriod()); FastForwardBy(10 * GetForcedSubscriptionPeriod());
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -843,22 +805,20 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -843,22 +805,20 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
// Handler force subscribes. // Handler force subscribes.
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token"));
task_runner->FastForwardBy(time_to_subscription); FastForwardBy(time_to_subscription);
// Check that the next forced subscription is scheduled in time. // Check that the next forced subscription is scheduled in time.
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0);
task_runner->FastForwardBy(GetForcedSubscriptionPeriod() - FastForwardBy(GetForcedSubscriptionPeriod() -
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token"));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
} }
TEST_F(BreakingNewsGCMAppHandlerTest, TEST_F(BreakingNewsGCMAppHandlerTest,
...@@ -884,28 +844,25 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -884,28 +844,25 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
// Check that the next validation is scheduled in time. // Check that the next validation is scheduled in time.
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0); EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)).Times(0);
task_runner->FastForwardBy(time_to_validation - FastForwardBy(time_to_validation - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce( .WillOnce(
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
// Check that the next forced subscription is scheduled in time. // Check that the next forced subscription is scheduled in time.
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")).Times(0);
task_runner->FastForwardBy((time_to_subscription - time_to_validation) - FastForwardBy((time_to_subscription - time_to_validation) -
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
EXPECT_CALL(*mock_subscription_manager(), Subscribe("token")); EXPECT_CALL(*mock_subscription_manager(), Subscribe("token"));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
} }
TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportMissingAction) { TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportMissingAction) {
...@@ -917,9 +874,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportMissingAction) { ...@@ -917,9 +874,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportMissingAction) {
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
handler->OnMessage("com.google.breakingnews.gcm", gcm::IncomingMessage()); handler->OnMessage("com.google.breakingnews.gcm", gcm::IncomingMessage());
...@@ -941,9 +896,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportInvalidAction) { ...@@ -941,9 +896,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportInvalidAction) {
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
gcm::IncomingMessage message; gcm::IncomingMessage message;
...@@ -969,9 +922,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportPushToRefreshAction) { ...@@ -969,9 +922,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportPushToRefreshAction) {
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
gcm::IncomingMessage message; gcm::IncomingMessage message;
...@@ -997,9 +948,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportPushByValueAction) { ...@@ -997,9 +948,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportPushByValueAction) {
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
gcm::IncomingMessage message; gcm::IncomingMessage message;
...@@ -1043,9 +992,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -1043,9 +992,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
MockOnNewRemoteSuggestionCallback mock_on_new_remote_suggestion_callback; MockOnNewRemoteSuggestionCallback mock_on_new_remote_suggestion_callback;
...@@ -1069,9 +1016,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -1069,9 +1016,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
base::MockCallback<BreakingNewsListener::OnRefreshRequestedCallback> base::MockCallback<BreakingNewsListener::OnRefreshRequestedCallback>
on_refresh_requested_callback; on_refresh_requested_callback;
...@@ -1091,9 +1036,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportTokenRetrievalResult) { ...@@ -1091,9 +1036,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ShouldReportTokenRetrievalResult) {
SetFeatureParams(/*enable_token_validation=*/false, SetFeatureParams(/*enable_token_validation=*/false,
/*enable_forced_subscription=*/false); /*enable_forced_subscription=*/false);
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce(InvokeCallbackArgument<4>(/*token=*/"", .WillOnce(InvokeCallbackArgument<4>(/*token=*/"",
...@@ -1124,14 +1067,11 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -1124,14 +1067,11 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
// Check that handler does not report the metric before the validation. // Check that handler does not report the metric before the validation.
task_runner->FastForwardBy(time_to_validation - FastForwardBy(time_to_validation - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
EXPECT_THAT( EXPECT_THAT(
histogram_tester.GetAllSamples( histogram_tester.GetAllSamples(
"NewTabPage.ContentSuggestions.BreakingNews.TokenRetrievalResult"), "NewTabPage.ContentSuggestions.BreakingNews.TokenRetrievalResult"),
...@@ -1141,7 +1081,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -1141,7 +1081,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce( .WillOnce(
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
histogram_tester.ExpectTimeBucketCount( histogram_tester.ExpectTimeBucketCount(
"NewTabPage.ContentSuggestions.BreakingNews.TimeSinceLastTokenValidation", "NewTabPage.ContentSuggestions.BreakingNews.TimeSinceLastTokenValidation",
...@@ -1169,14 +1109,11 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -1169,14 +1109,11 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache, pref_service()->SetString(prefs::kBreakingNewsGCMSubscriptionTokenCache,
"token"); "token");
scoped_refptr<TestMockTimeTaskRunner> task_runner( std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler();
new TestMockTimeTaskRunner(GetDummyNow(), TimeTicks::Now()));
std::unique_ptr<BreakingNewsGCMAppHandler> handler = MakeHandler(task_runner);
handler->StartListening(base::DoNothing(), base::DoNothing()); handler->StartListening(base::DoNothing(), base::DoNothing());
// Check that handler does not report the metric before the validation. // Check that handler does not report the metric before the validation.
task_runner->FastForwardBy(time_to_validation - FastForwardBy(time_to_validation - base::TimeDelta::FromSeconds(1));
base::TimeDelta::FromSeconds(1));
EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.ContentSuggestions." EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.ContentSuggestions."
"BreakingNews." "BreakingNews."
"WasTokenValidBeforeValidation"), "WasTokenValidBeforeValidation"),
...@@ -1186,7 +1123,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest, ...@@ -1186,7 +1123,7 @@ TEST_F(BreakingNewsGCMAppHandlerTest,
EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _)) EXPECT_CALL(*mock_instance_id(), GetToken(_, _, _, _, _))
.WillOnce( .WillOnce(
InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS)); InvokeCallbackArgument<4>("token", InstanceID::Result::SUCCESS));
task_runner->FastForwardBy(base::TimeDelta::FromSeconds(1)); FastForwardBy(base::TimeDelta::FromSeconds(1));
EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.ContentSuggestions." EXPECT_THAT(histogram_tester.GetAllSamples("NewTabPage.ContentSuggestions."
"BreakingNews." "BreakingNews."
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
#include "components/ntp_snippets/breaking_news/subscription_json_request.h" #include "components/ntp_snippets/breaking_news/subscription_json_request.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/message_loop/message_loop.h" #include "base/run_loop.h"
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/gtest_util.h" #include "base/test/gtest_util.h"
#include "base/test/mock_callback.h" #include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h"
#include "base/values.h" #include "base/values.h"
#include "net/http/http_util.h" #include "net/http/http_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h" #include "services/network/public/cpp/shared_url_loader_factory.h"
...@@ -79,7 +80,7 @@ class SubscriptionJsonRequestTest : public testing::Test { ...@@ -79,7 +80,7 @@ class SubscriptionJsonRequestTest : public testing::Test {
} }
private: private:
base::MessageLoop message_loop_; base::test::ScopedTaskEnvironment scoped_task_environment_;
network::TestURLLoaderFactory test_url_loader_factory_; network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_; scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
......
...@@ -5,8 +5,9 @@ ...@@ -5,8 +5,9 @@
#include "components/ntp_snippets/breaking_news/subscription_manager_impl.h" #include "components/ntp_snippets/breaking_news/subscription_manager_impl.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/message_loop/message_loop.h" #include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h" #include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_task_environment.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "components/ntp_snippets/pref_names.h" #include "components/ntp_snippets/pref_names.h"
#include "components/ntp_snippets/remote/test_utils.h" #include "components/ntp_snippets/remote/test_utils.h"
...@@ -122,7 +123,7 @@ class SubscriptionManagerImplTest : public testing::Test { ...@@ -122,7 +123,7 @@ class SubscriptionManagerImplTest : public testing::Test {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
base::MessageLoop message_loop_; base::test::ScopedTaskEnvironment scoped_task_environment_;
test::RemoteSuggestionsTestUtils utils_; test::RemoteSuggestionsTestUtils utils_;
identity::IdentityTestEnvironment identity_test_env_; identity::IdentityTestEnvironment identity_test_env_;
network::TestURLLoaderFactory test_url_loader_factory_; network::TestURLLoaderFactory test_url_loader_factory_;
......
...@@ -10,10 +10,10 @@ ...@@ -10,10 +10,10 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/test/mock_callback.h" #include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h"
#include "base/time/default_clock.h" #include "base/time/default_clock.h"
#include "components/ntp_snippets/category_info.h" #include "components/ntp_snippets/category_info.h"
#include "components/ntp_snippets/category_rankers/constant_category_ranker.h" #include "components/ntp_snippets/category_rankers/constant_category_ranker.h"
...@@ -291,8 +291,7 @@ TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) { ...@@ -291,8 +291,7 @@ TEST_F(ContentSuggestionsServiceTest, ShouldRedirectFetchSuggestionImage) {
TEST_F(ContentSuggestionsServiceTest, TEST_F(ContentSuggestionsServiceTest,
ShouldCallbackEmptyImageForUnavailableProvider) { ShouldCallbackEmptyImageForUnavailableProvider) {
// Setup the current thread's MessageLoop. base::test::ScopedTaskEnvironment scoped_task_environment;
base::MessageLoop message_loop;
base::RunLoop run_loop; base::RunLoop run_loop;
// Assuming there will never be a category with the id below. // Assuming there will never be a category with the id below.
......
...@@ -11,10 +11,10 @@ ...@@ -11,10 +11,10 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/test/mock_callback.h" #include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h"
#include "components/image_fetcher/core/image_fetcher_impl.h" #include "components/image_fetcher/core/image_fetcher_impl.h"
#include "components/ntp_snippets/category_info.h" #include "components/ntp_snippets/category_info.h"
#include "components/ntp_snippets/content_suggestion.h" #include "components/ntp_snippets/content_suggestion.h"
...@@ -126,7 +126,7 @@ class ContextualContentSuggestionsServiceTest : public testing::Test { ...@@ -126,7 +126,7 @@ class ContextualContentSuggestionsServiceTest : public testing::Test {
private: private:
FakeContextualSuggestionsFetcher* fetcher_; FakeContextualSuggestionsFetcher* fetcher_;
base::MessageLoop message_loop_; base::test::ScopedTaskEnvironment scoped_task_environment_;
TestingPrefServiceSimple pref_service_; TestingPrefServiceSimple pref_service_;
std::unique_ptr<ContextualContentSuggestionsService> source_; std::unique_ptr<ContextualContentSuggestionsService> source_;
......
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