1 // Copyright (c) 2012 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/ui/ash/launcher/launcher_favicon_loader.h"
7 #include "ash/shelf/shelf_constants.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
18 using content::WebContents
;
19 using content::WebContentsObserver
;
23 // Observer class to determine when favicons have completed loading.
24 class ContentsObserver
: public WebContentsObserver
{
26 explicit ContentsObserver(WebContents
* web_contents
)
27 : WebContentsObserver(web_contents
),
29 got_favicons_(false) {
32 virtual ~ContentsObserver() {}
35 got_favicons_
= false;
38 bool loaded() const { return loaded_
; }
39 bool got_favicons() const { return got_favicons_
; }
41 // WebContentsObserver overrides.
42 virtual void DidFinishLoad(
44 const GURL
& validated_url
,
46 content::RenderViewHost
* render_view_host
) OVERRIDE
{
50 virtual void DidUpdateFaviconURL(int32 page_id
,
51 const std::vector
<content::FaviconURL
>& candidates
) OVERRIDE
{
52 if (!candidates
.empty())
63 class LauncherFaviconLoaderBrowsertest
64 : public InProcessBrowserTest
,
65 public LauncherFaviconLoader::Delegate
{
67 LauncherFaviconLoaderBrowsertest() : favicon_updated_(false) {
70 virtual ~LauncherFaviconLoaderBrowsertest() {
73 virtual void SetUpOnMainThread() OVERRIDE
{
74 WebContents
* web_contents
=
75 browser()->tab_strip_model()->GetActiveWebContents();
76 contents_observer_
.reset(new ContentsObserver(web_contents
));
77 favicon_loader_
.reset(new LauncherFaviconLoader(this, web_contents
));
80 // LauncherFaviconLoader::Delegate overrides:
81 virtual void FaviconUpdated() OVERRIDE
{
82 favicon_updated_
= true;
86 bool NavigateTo(const char* url
) {
87 std::string url_path
= base::StringPrintf("files/ash/launcher/%s", url
);
88 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(url_path
));
92 bool WaitForContentsLoaded() {
93 const int64 max_seconds
= 10;
94 base::Time start_time
= base::Time::Now();
95 while (!(contents_observer_
->loaded() &&
96 contents_observer_
->got_favicons())) {
97 content::RunAllPendingInMessageLoop();
98 base::TimeDelta delta
= base::Time::Now() - start_time
;
99 if (delta
.InSeconds() >= max_seconds
) {
100 LOG(ERROR
) << " WaitForContentsLoaded timed out.";
107 bool WaitForFaviconUpdated() {
108 const int64 max_seconds
= 10;
109 base::Time start_time
= base::Time::Now();
110 while (favicon_loader_
->HasPendingDownloads()) {
111 content::RunAllPendingInMessageLoop();
112 base::TimeDelta delta
= base::Time::Now() - start_time
;
113 if (delta
.InSeconds() >= max_seconds
) {
114 LOG(ERROR
) << " WaitForFaviconDownlads timed out.";
121 void ResetDownloads() {
122 contents_observer_
->Reset();
125 bool favicon_updated_
;
126 scoped_ptr
<ContentsObserver
> contents_observer_
;
127 scoped_ptr
<LauncherFaviconLoader
> favicon_loader_
;
130 DISALLOW_COPY_AND_ASSIGN(LauncherFaviconLoaderBrowsertest
);
133 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest
, SmallLauncherIcon
) {
134 ASSERT_TRUE(test_server()->Start());
135 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html"));
136 EXPECT_TRUE(WaitForContentsLoaded());
137 EXPECT_TRUE(WaitForFaviconUpdated());
139 // No large favicons specified, bitmap should be empty.
140 EXPECT_TRUE(favicon_loader_
->GetFavicon().empty());
143 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest
, LargeLauncherIcon
) {
144 ASSERT_TRUE(test_server()->Start());
145 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html"));
146 EXPECT_TRUE(WaitForContentsLoaded());
147 EXPECT_TRUE(WaitForFaviconUpdated());
149 EXPECT_FALSE(favicon_loader_
->GetFavicon().empty());
150 EXPECT_EQ(128, favicon_loader_
->GetFavicon().height());
153 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest
, ManyLauncherIcons
) {
154 ASSERT_TRUE(test_server()->Start());
155 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html"));
156 EXPECT_TRUE(WaitForContentsLoaded());
157 EXPECT_TRUE(WaitForFaviconUpdated());
159 EXPECT_FALSE(favicon_loader_
->GetFavicon().empty());
160 // When multiple favicons are present, the correctly sized icon should be
161 // chosen. The icons are sized assuming ash::kShelfPreferredSize < 128.
162 EXPECT_GT(128, ash::kShelfPreferredSize
);
163 EXPECT_EQ(48, favicon_loader_
->GetFavicon().height());
166 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest
, ChangeLauncherIcons
) {
167 ASSERT_TRUE(test_server()->Start());
168 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html"));
169 EXPECT_TRUE(WaitForContentsLoaded());
170 EXPECT_TRUE(WaitForFaviconUpdated());
172 EXPECT_FALSE(favicon_loader_
->GetFavicon().empty());
173 EXPECT_EQ(48, favicon_loader_
->GetFavicon().height());
174 ASSERT_NO_FATAL_FAILURE(ResetDownloads());
176 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html"));
177 ASSERT_TRUE(WaitForContentsLoaded());
178 EXPECT_TRUE(WaitForFaviconUpdated());
180 EXPECT_TRUE(favicon_loader_
->GetFavicon().empty());
181 ASSERT_NO_FATAL_FAILURE(ResetDownloads());
183 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html"));
184 ASSERT_TRUE(WaitForContentsLoaded());
185 EXPECT_TRUE(WaitForFaviconUpdated());
187 EXPECT_FALSE(favicon_loader_
->GetFavicon().empty());
188 EXPECT_EQ(128, favicon_loader_
->GetFavicon().height());