1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// Copyright 2014 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 "chrome/browser/search/suggestions/image_fetcher_impl.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/suggestions/image_fetcher_delegate.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
class SkBitmap;
namespace suggestions {
namespace {
const char kTestUrl[] = "http://go.com/";
const char kTestImagePath[] = "/image_decoding/droids.png";
const char kInvalidImagePath[] = "/DOESNOTEXIST";
const base::FilePath::CharType kDocRoot[] =
FILE_PATH_LITERAL("chrome/test/data");
class TestImageFetcherDelegate : public ImageFetcherDelegate {
public:
TestImageFetcherDelegate()
: num_delegate_valid_called_(0),
num_delegate_null_called_(0) {}
~TestImageFetcherDelegate() override{};
// Perform additional tasks when an image has been fetched.
void OnImageFetched(const GURL& url, const SkBitmap* bitmap) override {
if (bitmap) {
num_delegate_valid_called_++;
} else {
num_delegate_null_called_++;
}
};
int num_delegate_valid_called() { return num_delegate_valid_called_; }
int num_delegate_null_called() { return num_delegate_null_called_; }
private:
int num_delegate_valid_called_;
int num_delegate_null_called_;
};
} // end namespace
class ImageFetcherImplBrowserTest : public InProcessBrowserTest {
protected:
ImageFetcherImplBrowserTest()
: num_callback_valid_called_(0), num_callback_null_called_(0) {
test_server_.ServeFilesFromSourceDirectory(base::FilePath(kDocRoot));
}
void SetUpInProcessBrowserTestFixture() override {
ASSERT_TRUE(test_server_.Start());
InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
}
ImageFetcherImpl* CreateImageFetcher() {
ImageFetcherImpl* fetcher =
new ImageFetcherImpl(browser()->profile()->GetRequestContext());
fetcher->SetImageFetcherDelegate(&delegate_);
return fetcher;
}
void OnImageAvailable(base::RunLoop* loop,
const GURL& url,
const SkBitmap* bitmap) {
if (bitmap) {
num_callback_valid_called_++;
} else {
num_callback_null_called_++;
}
loop->Quit();
}
void StartOrQueueNetworkRequestHelper(const GURL& image_url) {
scoped_ptr<ImageFetcherImpl> image_fetcher_(CreateImageFetcher());
base::RunLoop run_loop;
image_fetcher_->StartOrQueueNetworkRequest(
GURL(kTestUrl),
image_url,
base::Bind(&ImageFetcherImplBrowserTest::OnImageAvailable,
base::Unretained(this), &run_loop));
run_loop.Run();
}
int num_callback_valid_called_;
int num_callback_null_called_;
net::EmbeddedTestServer test_server_;
TestImageFetcherDelegate delegate_;
DISALLOW_COPY_AND_ASSIGN(ImageFetcherImplBrowserTest);
};
IN_PROC_BROWSER_TEST_F(ImageFetcherImplBrowserTest, NormalFetch) {
GURL image_url(test_server_.GetURL(kTestImagePath).spec());
StartOrQueueNetworkRequestHelper(image_url);
EXPECT_EQ(1, num_callback_valid_called_);
EXPECT_EQ(0, num_callback_null_called_);
EXPECT_EQ(1, delegate_.num_delegate_valid_called());
EXPECT_EQ(0, delegate_.num_delegate_null_called());
}
IN_PROC_BROWSER_TEST_F(ImageFetcherImplBrowserTest, MultipleFetch) {
GURL image_url(test_server_.GetURL(kTestImagePath).spec());
for (int i = 0; i < 5; i++) {
StartOrQueueNetworkRequestHelper(image_url);
}
EXPECT_EQ(5, num_callback_valid_called_);
EXPECT_EQ(0, num_callback_null_called_);
EXPECT_EQ(5, delegate_.num_delegate_valid_called());
EXPECT_EQ(0, delegate_.num_delegate_null_called());
}
IN_PROC_BROWSER_TEST_F(ImageFetcherImplBrowserTest, InvalidFetch) {
GURL invalid_image_url(test_server_.GetURL(kInvalidImagePath).spec());
StartOrQueueNetworkRequestHelper(invalid_image_url);
EXPECT_EQ(0, num_callback_valid_called_);
EXPECT_EQ(1, num_callback_null_called_);
EXPECT_EQ(0, delegate_.num_delegate_valid_called());
EXPECT_EQ(1, delegate_.num_delegate_null_called());
}
} // namespace suggestions
|