Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/thread_task_runner_handle.h" | |
| 10 #include "net/url_request/test_url_fetcher_factory.h" | |
| 11 #include "net/url_request/url_request_test_util.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace ntp_snippets { | |
| 16 namespace { | |
| 17 | |
| 18 using testing::IsNull; | |
| 19 using testing::NotNull; | |
| 20 | |
| 21 const char kTestContentSnippetsServerUrl[] = | |
| 22 "https://chromereader-pa.googleapis.com/v1/fetch?key=dummytoken"; | |
| 23 | |
| 24 class MockSnippetsAvailableCallback { | |
| 25 public: | |
| 26 MOCK_METHOD2(Run, void(const std::string& snippets_json, | |
| 27 const std::string& status_message)); | |
| 28 }; | |
| 29 | |
| 30 class NTPSnippetsFetcherTest : public testing::Test { | |
| 31 public: | |
| 32 NTPSnippetsFetcherTest() | |
| 33 : fake_url_fetcher_factory_( | |
| 34 /*default_factory=*/nullptr), | |
|
Marc Treib
2016/05/02 11:12:01
Should fit on the previous line?
mastiz
2016/05/02 12:34:28
Done.
| |
| 35 snippets_fetcher_(scoped_refptr<base::SequencedTaskRunner>(), | |
| 36 scoped_refptr<net::TestURLRequestContextGetter>( | |
| 37 new net::TestURLRequestContextGetter( | |
| 38 base::ThreadTaskRunnerHandle::Get())), | |
| 39 /*is_stable_channel=*/true) { | |
| 40 snippets_fetcher_subscription_ = snippets_fetcher_.AddCallback( | |
| 41 base::Bind(&MockSnippetsAvailableCallback::Run, | |
| 42 base::Unretained(&mock_callback_))); | |
| 43 } | |
| 44 | |
| 45 NTPSnippetsFetcher& snippets_fetcher() { return snippets_fetcher_; } | |
| 46 | |
| 47 net::FakeURLFetcherFactory& fake_url_fetcher_factory() { | |
| 48 return fake_url_fetcher_factory_; | |
| 49 } | |
| 50 | |
| 51 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } | |
| 52 | |
| 53 private: | |
| 54 // Instantiation of factory automatically sets itself as URLFetcher's factory. | |
| 55 net::FakeURLFetcherFactory fake_url_fetcher_factory_; | |
| 56 // Needed to use ThreadTaskRunnerHandle. | |
| 57 base::MessageLoop message_loop_; | |
| 58 NTPSnippetsFetcher snippets_fetcher_; | |
| 59 MockSnippetsAvailableCallback mock_callback_; | |
| 60 std::unique_ptr< | |
| 61 NTPSnippetsFetcher::SnippetsAvailableCallbackList::Subscription> | |
| 62 snippets_fetcher_subscription_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | |
| 65 }; | |
| 66 | |
| 67 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { | |
| 68 // The lack of registered baked in responses would cause any fetch to fail. | |
| 69 base::RunLoop().RunUntilIdle(); | |
| 70 } | |
| 71 | |
| 72 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { | |
| 73 const std::string json_str = "{ \"recos\": [] }"; | |
| 74 fake_url_fetcher_factory().SetFakeResponse( | |
| 75 GURL(kTestContentSnippetsServerUrl), | |
| 76 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS); | |
| 77 EXPECT_CALL(mock_callback(), | |
| 78 Run(/*snippets_json=*/json_str, /*status_message=*/"")); | |
|
Marc Treib
2016/05/02 11:12:00
nit: We generally use std::string() to denote empt
mastiz
2016/05/02 12:34:28
Done.
| |
| 79 snippets_fetcher().FetchSnippets(/*hosts=*/{}, /*count=*/1); | |
| 80 base::RunLoop().RunUntilIdle(); | |
| 81 } | |
| 82 | |
| 83 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | |
| 84 fake_url_fetcher_factory().SetFakeResponse( | |
| 85 GURL(kTestContentSnippetsServerUrl), | |
| 86 /*data=*/"", net::HTTP_NOT_FOUND, net::URLRequestStatus::FAILED); | |
| 87 EXPECT_CALL(mock_callback(), | |
| 88 Run(/*snippets_json=*/"", | |
| 89 /*status_message=*/"URLRequestStatus error -2")); | |
| 90 snippets_fetcher().FetchSnippets(/*hosts=*/{}, /*count=*/1); | |
| 91 base::RunLoop().RunUntilIdle(); | |
| 92 } | |
| 93 | |
| 94 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | |
| 95 fake_url_fetcher_factory().SetFakeResponse( | |
| 96 GURL(kTestContentSnippetsServerUrl), | |
| 97 /*data=*/"", net::HTTP_NOT_FOUND, net::URLRequestStatus::SUCCESS); | |
| 98 EXPECT_CALL(mock_callback(), Run(/*snippets_json=*/"", | |
| 99 /*status_message=*/"HTTP error 404")); | |
| 100 snippets_fetcher().FetchSnippets(/*hosts=*/{}, /*count=*/1); | |
| 101 base::RunLoop().RunUntilIdle(); | |
| 102 } | |
| 103 | |
| 104 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | |
| 105 const std::string json_str = "{ \"recos\": [] }"; | |
| 106 fake_url_fetcher_factory().SetFakeResponse( | |
| 107 GURL(kTestContentSnippetsServerUrl), | |
| 108 /*data=*/json_str, net::HTTP_OK, net::URLRequestStatus::SUCCESS); | |
| 109 EXPECT_CALL(mock_callback(), | |
| 110 Run(/*snippets_json=*/json_str, /*status_message=*/"")); | |
| 111 snippets_fetcher().FetchSnippets(/*hosts=*/{}, /*count=*/1); | |
| 112 // Second call to FetchSnippets() overrides/cancels the previous. Callback is | |
| 113 // expected to be called once. | |
|
Marc Treib
2016/05/02 11:12:00
I think you actually need to say ".Times(1)" after
mastiz
2016/05/02 12:34:28
Done.
You however don't need to: "If neither Will
| |
| 114 snippets_fetcher().FetchSnippets(/*hosts=*/{}, /*count=*/1); | |
| 115 base::RunLoop().RunUntilIdle(); | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 } // namespace ntp_snippets | |
| OLD | NEW |