1 // Copyright 2015 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 "components/favicon/content/content_favicon_driver.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "components/favicon/core/favicon_client.h"
9 #include "components/favicon/core/favicon_handler.h"
10 #include "components/favicon/core/favicon_service.h"
11 #include "content/public/browser/web_contents_observer.h"
12 #include "content/public/common/favicon_url.h"
13 #include "content/public/test/test_renderer_host.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/favicon_size.h"
20 class ContentFaviconDriverTest
: public content::RenderViewHostTestHarness
{
22 ContentFaviconDriverTest() {}
24 ~ContentFaviconDriverTest() override
{}
26 // content::RenderViewHostTestHarness:
27 void SetUp() override
{
28 RenderViewHostTestHarness::SetUp();
30 favicon_service_
.reset(new FaviconService(nullptr, nullptr));
31 ContentFaviconDriver::CreateForWebContents(
32 web_contents(), favicon_service(), nullptr, nullptr);
35 FaviconService
* favicon_service() {
36 return favicon_service_
.get();
40 scoped_ptr
<FaviconService
> favicon_service_
;
42 DISALLOW_COPY_AND_ASSIGN(ContentFaviconDriverTest
);
45 // Test that Favicon is not requested repeatedly during the same session if
46 // server returns HTTP 404 status.
47 TEST_F(ContentFaviconDriverTest
, UnableToDownloadFavicon
) {
48 const GURL
missing_icon_url("http://www.google.com/favicon.ico");
49 const GURL
another_icon_url("http://www.youtube.com/favicon.ico");
51 ContentFaviconDriver
* content_favicon_driver
=
52 ContentFaviconDriver::FromWebContents(web_contents());
54 std::vector
<SkBitmap
> empty_icons
;
55 std::vector
<gfx::Size
> empty_icon_sizes
;
58 // Try to download missing icon.
59 download_id
= content_favicon_driver
->StartDownload(missing_icon_url
, 0);
60 EXPECT_NE(0, download_id
);
61 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url
));
63 // Report download failure with HTTP 503 status.
64 content_favicon_driver
->DidDownloadFavicon(download_id
, 503, missing_icon_url
,
65 empty_icons
, empty_icon_sizes
);
66 // Icon is not marked as UnableToDownload as HTTP status is not 404.
67 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url
));
69 // Try to download again.
70 download_id
= content_favicon_driver
->StartDownload(missing_icon_url
, 0);
71 EXPECT_NE(0, download_id
);
72 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url
));
74 // Report download failure with HTTP 404 status.
75 content_favicon_driver
->DidDownloadFavicon(download_id
, 404, missing_icon_url
,
76 empty_icons
, empty_icon_sizes
);
77 // Icon is marked as UnableToDownload.
78 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url
));
80 // Try to download again.
81 download_id
= content_favicon_driver
->StartDownload(missing_icon_url
, 0);
82 // Download is not started and Icon is still marked as UnableToDownload.
83 EXPECT_EQ(0, download_id
);
84 EXPECT_TRUE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url
));
86 // Try to download another icon.
87 download_id
= content_favicon_driver
->StartDownload(another_icon_url
, 0);
88 // Download is started as another icon URL is not same as missing_icon_url.
89 EXPECT_NE(0, download_id
);
90 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url
));
92 // Clear the list of missing icons.
93 favicon_service()->ClearUnableToDownloadFavicons();
94 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url
));
95 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(another_icon_url
));
97 // Try to download again.
98 download_id
= content_favicon_driver
->StartDownload(missing_icon_url
, 0);
99 EXPECT_NE(0, download_id
);
100 // Report download success with HTTP 200 status.
101 content_favicon_driver
->DidDownloadFavicon(download_id
, 200, missing_icon_url
,
102 empty_icons
, empty_icon_sizes
);
103 // Icon is not marked as UnableToDownload as HTTP status is not 404.
104 EXPECT_FALSE(favicon_service()->WasUnableToDownloadFavicon(missing_icon_url
));
106 favicon_service()->Shutdown();
109 // Test that ContentFaviconDriver ignores updated favicon URLs if there is no
110 // last committed entry. This occurs when script is injected in about:blank.
111 // See crbug.com/520759 for more details
112 TEST_F(ContentFaviconDriverTest
, FaviconUpdateNoLastCommittedEntry
) {
113 ASSERT_EQ(nullptr, web_contents()->GetController().GetLastCommittedEntry());
115 std::vector
<content::FaviconURL
> favicon_urls
;
116 favicon_urls
.push_back(content::FaviconURL(
117 GURL("http://www.google.ca/favicon.ico"), content::FaviconURL::FAVICON
,
118 std::vector
<gfx::Size
>()));
119 favicon::ContentFaviconDriver
* driver
=
120 favicon::ContentFaviconDriver::FromWebContents(web_contents());
121 static_cast<content::WebContentsObserver
*>(driver
)
122 ->DidUpdateFaviconURL(favicon_urls
);
124 // Test that ContentFaviconDriver ignored the favicon url update.
125 EXPECT_TRUE(driver
->favicon_urls().empty());
129 } // namespace favicon