OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <vector> | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/time.h" | |
9 #include "chrome/browser/ui/browser.h" | |
10 #include "chrome/browser/ui/browser_tabstrip.h" | |
11 #include "chrome/browser/ui/views/ash/launcher/browser_launcher_item_controller.
h" | |
12 #include "chrome/browser/ui/views/ash/launcher/launcher_favicon_loader.h" | |
13 #include "chrome/browser/ui/views/frame/browser_view.h" | |
14 #include "chrome/common/favicon_url.h" | |
15 #include "chrome/common/icon_messages.h" | |
16 #include "chrome/test/base/in_process_browser_test.h" | |
17 #include "chrome/test/base/ui_test_utils.h" | |
18 #include "content/public/browser/web_contents.h" | |
19 #include "content/public/browser/web_contents_delegate.h" | |
20 #include "net/test/test_server.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 namespace { | |
24 | |
25 // Observer class to determine when favicons have completed loading. | |
26 class ContentsObserver : public content::WebContentsObserver { | |
27 public: | |
28 explicit ContentsObserver(content::WebContents* web_contents) | |
29 : content::WebContentsObserver(web_contents), | |
30 got_favicons_(false), | |
31 downloads_received_(0) { | |
32 } | |
33 | |
34 virtual ~ContentsObserver() {} | |
35 | |
36 bool got_favicons() const { return got_favicons_; } | |
37 int downloads_received() const { return downloads_received_; } | |
38 void Reset() { | |
39 got_favicons_ = false; | |
40 downloads_received_ = 0; | |
41 } | |
42 | |
43 // content::WebContentsObserver overrides. | |
44 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | |
45 bool message_handled = false; // Allow other handlers to receive these. | |
46 IPC_BEGIN_MESSAGE_MAP(ContentsObserver, message) | |
47 IPC_MESSAGE_HANDLER(IconHostMsg_DidDownloadFavicon, OnDidDownloadFavicon) | |
48 IPC_MESSAGE_HANDLER(IconHostMsg_UpdateFaviconURL, OnUpdateFaviconURL) | |
49 IPC_MESSAGE_UNHANDLED(message_handled = false) | |
50 IPC_END_MESSAGE_MAP() | |
51 return message_handled; | |
52 } | |
53 | |
54 private: | |
55 void OnUpdateFaviconURL(int32 page_id, | |
56 const std::vector<FaviconURL>& candidates) { | |
57 got_favicons_ = true; | |
58 } | |
59 | |
60 void OnDidDownloadFavicon(int id, | |
61 const GURL& image_url, | |
62 bool errored, | |
63 int requested_size, | |
64 const std::vector<SkBitmap>& bitmaps) { | |
65 ++downloads_received_; | |
66 } | |
67 | |
68 bool got_favicons_; | |
69 int downloads_received_; | |
70 }; | |
71 | |
72 } // namespace | |
73 | |
74 class LauncherFaviconLoaderBrowsertest : public InProcessBrowserTest { | |
75 protected: | |
76 void NavigateTo(Browser* browser, const char* url) { | |
77 std::string url_path = base::StringPrintf("files/ash/launcher/%s", url); | |
78 ui_test_utils::NavigateToURL(browser, test_server()->GetURL(url_path)); | |
79 } | |
80 | |
81 void CreatePanelBrowser(const char* url, Browser** result) { | |
82 Browser* panel_browser = new Browser( | |
83 Browser::CreateParams::CreateForApp( | |
84 Browser::TYPE_PANEL, "Test Panel", gfx::Rect(), | |
85 browser()->profile())); | |
86 EXPECT_TRUE(panel_browser->is_type_panel()); | |
87 ASSERT_EQ(static_cast<void*>(NULL), contents_observer_.get()); | |
88 // Load initial tab contents before setting the observer. | |
89 ui_test_utils::NavigateToURL(panel_browser, GURL()); | |
90 contents_observer_.reset( | |
91 new ContentsObserver(chrome::GetWebContentsAt(panel_browser, 0))); | |
92 NavigateTo(panel_browser, url); | |
93 *result = panel_browser; | |
94 } | |
95 | |
96 LauncherFaviconLoader* GetFaviconLoader(Browser* browser) { | |
97 BrowserView* browser_view = static_cast<BrowserView*>(browser->window()); | |
98 BrowserLauncherItemController* launcher_item_controller = | |
99 browser_view->launcher_item_controller(); | |
100 if (!launcher_item_controller) | |
101 return NULL; | |
102 EXPECT_EQ(BrowserLauncherItemController::TYPE_EXTENSION_PANEL, | |
103 launcher_item_controller->type()); | |
104 LauncherFaviconLoader* loader = launcher_item_controller->favicon_loader(); | |
105 return loader; | |
106 } | |
107 | |
108 void ResetDownloads() { | |
109 ASSERT_NE(static_cast<void*>(NULL), contents_observer_.get()); | |
110 contents_observer_->Reset(); | |
111 } | |
112 | |
113 bool WaitForFaviconDownlads(int expected) { | |
114 const int64 max_seconds = 2; | |
115 base::Time start_time = base::Time::Now(); | |
116 while (!contents_observer_->got_favicons() || | |
117 contents_observer_->downloads_received() < expected) { | |
118 content::RunAllPendingInMessageLoop(); | |
119 base::TimeDelta delta = base::Time::Now() - start_time; | |
120 if (delta.InSeconds() >= max_seconds) { | |
121 LOG(ERROR) << " WaitForFaviconDownlads timed out:" | |
122 << " Got Favicons:" << contents_observer_->got_favicons() | |
123 << " Received: " << contents_observer_->downloads_received() | |
124 << " / " << expected; | |
125 return false; | |
126 } | |
127 } | |
128 return true; | |
129 } | |
130 | |
131 private: | |
132 scoped_ptr<ContentsObserver> contents_observer_; | |
133 scoped_ptr<net::TestServer> test_server_; | |
134 }; | |
135 | |
136 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, SmallLauncherIcon) { | |
137 ASSERT_TRUE(test_server()->Start()); | |
138 Browser* panel_browser; | |
139 ASSERT_NO_FATAL_FAILURE( | |
140 CreatePanelBrowser("launcher-smallfavicon.html", &panel_browser)); | |
141 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(panel_browser); | |
142 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | |
143 EXPECT_TRUE(WaitForFaviconDownlads(1)); | |
144 // No large favicons specified, bitmap should be empty. | |
145 EXPECT_TRUE(favicon_loader->GetFavicon().empty()); | |
146 } | |
147 | |
148 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, LargeLauncherIcon) { | |
149 ASSERT_TRUE(test_server()->Start()); | |
150 Browser* panel_browser; | |
151 ASSERT_NO_FATAL_FAILURE( | |
152 CreatePanelBrowser("launcher-largefavicon.html", &panel_browser)); | |
153 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(panel_browser); | |
154 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | |
155 EXPECT_TRUE(WaitForFaviconDownlads(1)); | |
156 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | |
157 EXPECT_EQ(128, favicon_loader->GetFavicon().height()); | |
158 } | |
159 | |
160 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ManyLauncherIcons) { | |
161 ASSERT_TRUE(test_server()->Start()); | |
162 Browser* panel_browser; | |
163 ASSERT_NO_FATAL_FAILURE( | |
164 CreatePanelBrowser("launcher-manyfavicon.html", &panel_browser)); | |
165 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(panel_browser); | |
166 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | |
167 | |
168 EXPECT_TRUE(WaitForFaviconDownlads(3)); | |
169 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | |
170 // When multiple favicons are present, the correctly sized icon should be | |
171 // chosen. The icons are sized assuming ash::kLauncherPreferredSize < 128. | |
172 EXPECT_GT(128, ash::kLauncherPreferredSize); | |
173 EXPECT_EQ(48, favicon_loader->GetFavicon().height()); | |
174 } | |
175 | |
176 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ChangeLauncherIcons) { | |
177 ASSERT_TRUE(test_server()->Start()); | |
178 Browser* panel_browser; | |
179 ASSERT_NO_FATAL_FAILURE( | |
180 CreatePanelBrowser("launcher-manyfavicon.html", &panel_browser)); | |
181 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(panel_browser); | |
182 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | |
183 | |
184 EXPECT_TRUE(WaitForFaviconDownlads(3)); | |
185 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | |
186 EXPECT_EQ(48, favicon_loader->GetFavicon().height()); | |
187 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); | |
188 | |
189 NavigateTo(panel_browser, "launcher-smallfavicon.html"); | |
190 EXPECT_TRUE(WaitForFaviconDownlads(1)); | |
191 EXPECT_TRUE(favicon_loader->GetFavicon().empty()); | |
192 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); | |
193 | |
194 NavigateTo(panel_browser, "launcher-largefavicon.html"); | |
195 EXPECT_TRUE(WaitForFaviconDownlads(1)); | |
196 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | |
197 EXPECT_EQ(128, favicon_loader->GetFavicon().height()); | |
198 } | |
OLD | NEW |