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/image_fetcher/image_data_fetcher.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/callback.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "net/url_request/test_url_fetcher_factory.h" | |
| 14 #include "net/url_request/url_request_status.h" | |
| 15 #include "net/url_request/url_request_test_util.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const GURL kImageURL("http://www.example.com/image1"); | |
| 21 const char kURLResponseData[] = "EncodedImageData"; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 class ImageDataFetcherTest : public testing::Test { | |
| 26 public: | |
| 27 ImageDataFetcherTest() | |
| 28 : message_loop_(), | |
|
Marc Treib
2016/06/20 14:47:29
Not necessary (the default ctor will be called any
markusheintz_
2016/06/20 15:06:53
I know but it also does no harm. On the upside I f
Bernhard Bauer
2016/06/20 17:28:38
That's not really an argument for adding unnecessa
markusheintz_
2016/06/21 15:56:08
Removed
| |
| 29 test_request_context_getter_( | |
| 30 new net::TestURLRequestContextGetter(message_loop_.task_runner())), | |
| 31 image_data_fetcher_(test_request_context_getter_.get()), | |
| 32 num_callbacks_run_(0) {} | |
| 33 ~ImageDataFetcherTest() override {} | |
| 34 | |
| 35 // Callback that is called by the FetchImageData test. | |
| 36 void OnImageDataFetched(const std::string& image_data) { | |
|
Marc Treib
2016/06/20 14:47:29
I think it'd be more idiomatic (and easier to read
markusheintz_
2016/06/20 15:06:53
Sorry I'm a bit rusty when it comes to gtest. Woul
Marc Treib
2016/06/20 15:21:42
No, the mock method doesn't need to override anyth
Bernhard Bauer
2016/06/20 17:28:38
These callbacks could be protected.
markusheintz_
2016/06/21 15:56:08
Using MOCK_METHODS now
Bernhard Bauer
2016/06/21 16:24:19
They still could be protected :-D
| |
| 37 EXPECT_EQ(std::string(kURLResponseData), image_data); | |
| 38 } | |
| 39 | |
| 40 // Callback that is called by the FetchImageData_FailedRequest test. | |
| 41 void OnImageDataFetchedFailedRequest(const std::string& image_data) { | |
| 42 EXPECT_EQ(std::string(), image_data); | |
| 43 } | |
| 44 | |
| 45 // Callback that is called by the FetchImageData_MultipleRequests test. | |
| 46 void OnImageDataFetchedMultipleRequests(const std::string& image_data) { | |
| 47 ++num_callbacks_run_; | |
| 48 } | |
| 49 | |
| 50 protected: | |
| 51 base::MessageLoop message_loop_; | |
| 52 | |
| 53 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_; | |
| 54 | |
| 55 image_fetcher::ImageDataFetcher image_data_fetcher_; | |
| 56 | |
| 57 net::TestURLFetcherFactory fetcher_factory_; | |
| 58 | |
| 59 // The number of times the OnImageDataFetchedMultipleRequests callback was | |
| 60 // called during a test run. | |
| 61 int num_callbacks_run_; | |
| 62 | |
| 63 private: | |
| 64 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest); | |
| 65 }; | |
| 66 | |
| 67 TEST_F(ImageDataFetcherTest, FetchImageData) { | |
| 68 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback = | |
|
Marc Treib
2016/06/20 14:47:29
optional: If you don't use this again, just do the
markusheintz_
2016/06/20 15:06:53
Done.
| |
| 69 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | |
| 70 base::Unretained(this)); | |
| 71 | |
| 72 image_data_fetcher_.FetchImageData(kImageURL, callback); | |
| 73 | |
| 74 // Get and configure the TestURLFetcher. | |
| 75 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 76 ASSERT_NE(nullptr, test_url_fetcher); | |
| 77 test_url_fetcher->set_status( | |
| 78 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | |
| 79 test_url_fetcher->SetResponseString(std::string(kURLResponseData)); | |
| 80 | |
| 81 // Call the URLFetcher delegate to continue the test. | |
| 82 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 83 } | |
| 84 | |
| 85 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { | |
| 86 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback = | |
|
Marc Treib
2016/06/20 14:47:29
Same here.
markusheintz_
2016/06/20 15:06:53
Done.
| |
| 87 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, | |
| 88 base::Unretained(this)); | |
| 89 | |
| 90 image_data_fetcher_.FetchImageData(kImageURL, callback); | |
| 91 | |
| 92 // Get and configure the TestURLFetcher. | |
| 93 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 94 ASSERT_NE(nullptr, test_url_fetcher); | |
| 95 test_url_fetcher->set_status( | |
| 96 net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
| 97 net::ERR_INVALID_URL)); | |
| 98 | |
| 99 // Call the URLFetcher delegate to continue the test. | |
| 100 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 101 } | |
| 102 | |
| 103 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) { | |
| 104 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback1 = | |
| 105 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, | |
| 106 base::Unretained(this)); | |
| 107 image_fetcher::ImageDataFetcher::ImageDataFetcherCallback callback2 = | |
|
Marc Treib
2016/06/20 14:47:29
You can use the same callback twice.
markusheintz_
2016/06/20 15:06:53
I want to simulate two individual calls. Therefore
Marc Treib
2016/06/20 15:21:42
You'd still check the two calls with the EXPECT_EQ
| |
| 108 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, | |
| 109 base::Unretained(this)); | |
| 110 | |
| 111 image_data_fetcher_.FetchImageData(kImageURL, callback1); | |
| 112 image_data_fetcher_.FetchImageData(kImageURL, callback2); | |
| 113 | |
| 114 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 115 ASSERT_NE(nullptr, test_url_fetcher); | |
| 116 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 117 | |
| 118 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); | |
| 119 ASSERT_NE(nullptr, test_url_fetcher); | |
| 120 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | |
| 121 | |
| 122 EXPECT_EQ(num_callbacks_run_, 2); | |
| 123 } | |
| OLD | NEW |