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.
4
5 #include <string>
6
7 #include "base/files/file_path.h"
8 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h"
9 #include "chrome/browser/search/suggestions/thumbnail_manager.h"
10 #include "chrome/test/base/in_process_browser_test.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h"
15 #include "net/test/spawned_test_server/spawned_test_server.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/image/image_skia.h"
19 #include "url/gurl.h"
20
21 namespace {
22
23 const char kTestUrl1[] = "http://go.com/";
24 const char kTestUrl2[] = "http://goal.com/";
25 const char kTestImagePath[] = "files/image_decoding/droids.png";
26 const char kInvalidImagePath[] = "files/DOESNOTEXIST";
27
28 const base::FilePath::CharType kDocRoot[] =
29 FILE_PATH_LITERAL("chrome/test/data");
30
31 using content::BrowserThread;
32
33 class ThumbnailManagerBrowserTest : public InProcessBrowserTest {
34 public:
ThumbnailManagerBrowserTest()35 ThumbnailManagerBrowserTest()
36 : num_callback_null_called_(0),
37 num_callback_valid_called_(0),
38 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
39 test_server_(net::SpawnedTestServer::TYPE_HTTP,
40 net::SpawnedTestServer::kLocalhost,
41 base::FilePath(kDocRoot)) {}
42
SetUp()43 virtual void SetUp() OVERRIDE {
44 ASSERT_TRUE(test_server_.Start());
45
46 context_ = new net::TestURLRequestContextGetter(
47 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
48 context_->AddRef();
49 }
50
TearDown()51 virtual void TearDown() OVERRIDE {
52 BrowserThread::ReleaseSoon(BrowserThread::IO, FROM_HERE, context_);
53 }
54
OnThumbnailAvailable(const GURL & url,const SkBitmap * bitmap)55 void OnThumbnailAvailable(const GURL& url, const SkBitmap* bitmap) {
56 if (bitmap) {
57 num_callback_valid_called_++;
58 } else {
59 num_callback_null_called_++;
60 }
61 }
62 int num_callback_null_called_;
63 int num_callback_valid_called_;
64 content::TestBrowserThreadBundle thread_bundle_;
65 net::SpawnedTestServer test_server_;
66 net::TestURLRequestContextGetter* context_;
67 };
68
69 } // namespace
70
71 namespace suggestions {
72
IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest,FetchThumbnails)73 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, FetchThumbnails) {
74 SuggestionsProfile suggestions_profile;
75 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions();
76 suggestion->set_url(kTestUrl1);
77 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec());
78
79 TestingProfile profile;
80 ThumbnailManager thumbnail_manager(&profile);
81 thumbnail_manager.InitializeThumbnailMap(suggestions_profile);
82 thumbnail_manager.set_request_context(context_);
83
84 // Fetch existing URL.
85 thumbnail_manager.GetPageThumbnail(
86 GURL(kTestUrl1),
87 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable,
88 base::Unretained(this)));
89
90 content::RunMessageLoop();
91
92 EXPECT_EQ(0, num_callback_null_called_);
93 EXPECT_EQ(1, num_callback_valid_called_);
94
95 // Fetch non-existing URL.
96 thumbnail_manager.GetPageThumbnail(
97 GURL(kTestUrl2),
98 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable,
99 base::Unretained(this)));
100
101 content::RunMessageLoop();
102
103 EXPECT_EQ(1, num_callback_null_called_);
104 EXPECT_EQ(1, num_callback_valid_called_);
105 }
106
IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest,FetchThumbnailsMultiple)107 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, FetchThumbnailsMultiple) {
108 SuggestionsProfile suggestions_profile;
109 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions();
110 suggestion->set_url(kTestUrl1);
111 suggestion->set_thumbnail(test_server_.GetURL(kTestImagePath).spec());
112
113 TestingProfile profile;
114 ThumbnailManager thumbnail_manager(&profile);
115 thumbnail_manager.InitializeThumbnailMap(suggestions_profile);
116 thumbnail_manager.set_request_context(context_);
117
118 // Fetch non-existing URL, and add more while request is in flight.
119 for (int i = 0 ; i < 5; i++) {
120 thumbnail_manager.GetPageThumbnail(
121 GURL(kTestUrl1),
122 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable,
123 base::Unretained(this)));
124 }
125
126 content::RunMessageLoop();
127
128 EXPECT_EQ(0, num_callback_null_called_);
129 EXPECT_EQ(5, num_callback_valid_called_);
130 }
131
IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest,FetchThumbnailsInvalid)132 IN_PROC_BROWSER_TEST_F(ThumbnailManagerBrowserTest, FetchThumbnailsInvalid) {
133 SuggestionsProfile suggestions_profile;
134 ChromeSuggestion* suggestion = suggestions_profile.add_suggestions();
135 suggestion->set_url(kTestUrl1);
136 suggestion->set_thumbnail(test_server_.GetURL(kInvalidImagePath).spec());
137
138 TestingProfile profile;
139 ThumbnailManager thumbnail_manager(&profile);
140 thumbnail_manager.InitializeThumbnailMap(suggestions_profile);
141 thumbnail_manager.set_request_context(context_);
142
143 // Fetch existing URL that has invalid thumbnail.
144 thumbnail_manager.GetPageThumbnail(
145 GURL(kTestUrl1),
146 base::Bind(&ThumbnailManagerBrowserTest::OnThumbnailAvailable,
147 base::Unretained(this)));
148
149 content::RunMessageLoop();
150
151 EXPECT_EQ(1, num_callback_null_called_);
152 EXPECT_EQ(0, num_callback_valid_called_);
153 }
154
155 } // namespace suggestions
156