Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / search / suggestions / image_fetcher_impl_browsertest.cc
blob904a637d91abf741ff60208c3128aeec420622b8
1 // Copyright 2014 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.
5 #include "chrome/browser/search/suggestions/image_fetcher_impl.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "components/suggestions/image_fetcher_delegate.h"
15 #include "net/test/spawned_test_server/spawned_test_server.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 class SkBitmap;
21 namespace suggestions {
23 namespace {
25 const char kTestUrl[] = "http://go.com/";
26 const char kTestImagePath[] = "files/image_decoding/droids.png";
27 const char kInvalidImagePath[] = "files/DOESNOTEXIST";
29 const base::FilePath::CharType kDocRoot[] =
30 FILE_PATH_LITERAL("chrome/test/data");
32 class TestImageFetcherDelegate : public ImageFetcherDelegate {
33 public:
34 TestImageFetcherDelegate()
35 : num_delegate_valid_called_(0),
36 num_delegate_null_called_(0) {}
37 ~TestImageFetcherDelegate() override{};
39 // Perform additional tasks when an image has been fetched.
40 void OnImageFetched(const GURL& url, const SkBitmap* bitmap) override {
41 if (bitmap) {
42 num_delegate_valid_called_++;
43 } else {
44 num_delegate_null_called_++;
48 int num_delegate_valid_called() { return num_delegate_valid_called_; }
49 int num_delegate_null_called() { return num_delegate_null_called_; }
51 private:
52 int num_delegate_valid_called_;
53 int num_delegate_null_called_;
56 } // end namespace
58 class ImageFetcherImplBrowserTest : public InProcessBrowserTest {
59 protected:
60 ImageFetcherImplBrowserTest()
61 : num_callback_valid_called_(0),
62 num_callback_null_called_(0),
63 test_server_(net::SpawnedTestServer::TYPE_HTTP,
64 net::SpawnedTestServer::kLocalhost,
65 base::FilePath(kDocRoot)) {}
67 void SetUpInProcessBrowserTestFixture() override {
68 ASSERT_TRUE(test_server_.Start());
69 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
72 void TearDownInProcessBrowserTestFixture() override { test_server_.Stop(); }
74 ImageFetcherImpl* CreateImageFetcher() {
75 ImageFetcherImpl* fetcher =
76 new ImageFetcherImpl(browser()->profile()->GetRequestContext());
77 fetcher->SetImageFetcherDelegate(&delegate_);
78 return fetcher;
81 void OnImageAvailable(base::RunLoop* loop,
82 const GURL& url,
83 const SkBitmap* bitmap) {
84 if (bitmap) {
85 num_callback_valid_called_++;
86 } else {
87 num_callback_null_called_++;
89 loop->Quit();
92 void StartOrQueueNetworkRequestHelper(const GURL& image_url) {
93 scoped_ptr<ImageFetcherImpl> image_fetcher_(CreateImageFetcher());
95 base::RunLoop run_loop;
96 image_fetcher_->StartOrQueueNetworkRequest(
97 GURL(kTestUrl),
98 image_url,
99 base::Bind(&ImageFetcherImplBrowserTest::OnImageAvailable,
100 base::Unretained(this), &run_loop));
101 run_loop.Run();
104 int num_callback_valid_called_;
105 int num_callback_null_called_;
107 net::SpawnedTestServer test_server_;
108 TestImageFetcherDelegate delegate_;
110 DISALLOW_COPY_AND_ASSIGN(ImageFetcherImplBrowserTest);
113 IN_PROC_BROWSER_TEST_F(ImageFetcherImplBrowserTest, NormalFetch) {
114 GURL image_url(test_server_.GetURL(kTestImagePath).spec());
115 StartOrQueueNetworkRequestHelper(image_url);
117 EXPECT_EQ(1, num_callback_valid_called_);
118 EXPECT_EQ(0, num_callback_null_called_);
119 EXPECT_EQ(1, delegate_.num_delegate_valid_called());
120 EXPECT_EQ(0, delegate_.num_delegate_null_called());
123 IN_PROC_BROWSER_TEST_F(ImageFetcherImplBrowserTest, MultipleFetch) {
124 GURL image_url(test_server_.GetURL(kTestImagePath).spec());
126 for (int i = 0; i < 5; i++) {
127 StartOrQueueNetworkRequestHelper(image_url);
130 EXPECT_EQ(5, num_callback_valid_called_);
131 EXPECT_EQ(0, num_callback_null_called_);
132 EXPECT_EQ(5, delegate_.num_delegate_valid_called());
133 EXPECT_EQ(0, delegate_.num_delegate_null_called());
136 IN_PROC_BROWSER_TEST_F(ImageFetcherImplBrowserTest, InvalidFetch) {
137 GURL invalid_image_url(test_server_.GetURL(kInvalidImagePath).spec());
138 StartOrQueueNetworkRequestHelper(invalid_image_url);
140 EXPECT_EQ(0, num_callback_valid_called_);
141 EXPECT_EQ(1, num_callback_null_called_);
142 EXPECT_EQ(0, delegate_.num_delegate_valid_called());
143 EXPECT_EQ(1, delegate_.num_delegate_null_called());
146 } // namespace suggestions