| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <vector> | 5 #include "chrome/browser/ui/ash/launcher/launcher_favicon_loader.h" |
| 6 | 6 |
| 7 #include "ash/ash_switches.h" | 7 #include "ash/launcher/launcher_types.h" |
| 8 #include "base/command_line.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 11 #include "base/time.h" | |
| 12 #include "chrome/browser/ui/ash/launcher/browser_launcher_item_controller.h" | |
| 13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | |
| 14 #include "chrome/browser/ui/ash/launcher/launcher_favicon_loader.h" | |
| 15 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 12 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 13 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/browser/web_contents_delegate.h" | |
| 22 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
| 23 #include "content/public/common/favicon_url.h" | |
| 24 #include "net/test/test_server.h" | |
| 25 #include "testing/gtest/include/gtest/gtest.h" | |
| 26 | 16 |
| 27 // TODO(skuhne): Remove this module together with launcher_favicon_loader.* | 17 using content::WebContents; |
| 28 // when the old launcher goes away. | 18 using content::WebContentsObserver; |
| 29 | 19 |
| 30 namespace { | 20 namespace { |
| 31 | 21 |
| 32 // Observer class to determine when favicons have completed loading. | 22 // Observer class to determine when favicons have completed loading. |
| 33 class ContentsObserver : public content::WebContentsObserver { | 23 class ContentsObserver : public WebContentsObserver { |
| 34 public: | 24 public: |
| 35 explicit ContentsObserver(content::WebContents* web_contents) | 25 explicit ContentsObserver(WebContents* web_contents) |
| 36 : content::WebContentsObserver(web_contents), | 26 : WebContentsObserver(web_contents), |
| 27 loaded_(false), |
| 37 got_favicons_(false) { | 28 got_favicons_(false) { |
| 38 } | 29 } |
| 39 | 30 |
| 40 virtual ~ContentsObserver() {} | 31 virtual ~ContentsObserver() {} |
| 41 | 32 |
| 42 bool got_favicons() const { return got_favicons_; } | |
| 43 void Reset() { | 33 void Reset() { |
| 44 got_favicons_ = false; | 34 got_favicons_ = false; |
| 45 } | 35 } |
| 46 | 36 |
| 47 // content::WebContentsObserver overrides. | 37 bool loaded() const { return loaded_; } |
| 38 bool got_favicons() const { return got_favicons_; } |
| 39 |
| 40 // WebContentsObserver overrides. |
| 41 virtual void DidFinishLoad( |
| 42 int64 frame_id, |
| 43 const GURL& validated_url, |
| 44 bool is_main_frame, |
| 45 content::RenderViewHost* render_view_host) OVERRIDE { |
| 46 loaded_ = true; |
| 47 } |
| 48 |
| 48 virtual void DidUpdateFaviconURL(int32 page_id, | 49 virtual void DidUpdateFaviconURL(int32 page_id, |
| 49 const std::vector<content::FaviconURL>& candidates) OVERRIDE { | 50 const std::vector<content::FaviconURL>& candidates) OVERRIDE { |
| 50 if (!candidates.empty()) | 51 if (!candidates.empty()) |
| 51 got_favicons_ = true; | 52 got_favicons_ = true; |
| 52 } | 53 } |
| 53 | 54 |
| 54 private: | 55 private: |
| 56 bool loaded_; |
| 55 bool got_favicons_; | 57 bool got_favicons_; |
| 56 }; | 58 }; |
| 57 | 59 |
| 58 } // namespace | 60 } // namespace |
| 59 | 61 |
| 60 class LauncherFaviconLoaderBrowsertest : public InProcessBrowserTest { | 62 class LauncherFaviconLoaderBrowsertest |
| 63 : public InProcessBrowserTest, |
| 64 public LauncherFaviconLoader::Delegate { |
| 61 public: | 65 public: |
| 62 LauncherFaviconLoaderBrowsertest() | 66 LauncherFaviconLoaderBrowsertest() : favicon_updated_(false) { |
| 63 : panel_browser_(NULL), | |
| 64 loader_(NULL), | |
| 65 contents_observer_(NULL) { | |
| 66 } | 67 } |
| 67 | 68 |
| 68 virtual ~LauncherFaviconLoaderBrowsertest() { | 69 virtual ~LauncherFaviconLoaderBrowsertest() { |
| 69 } | 70 } |
| 70 | 71 |
| 71 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 72 virtual void SetUpOnMainThread() { |
| 72 InProcessBrowserTest::SetUpCommandLine(command_line); | 73 WebContents* web_contents = |
| 73 command_line->AppendSwitch(ash::switches::kAshDisablePerAppLauncher); | 74 browser()->tab_strip_model()->GetActiveWebContents(); |
| 75 contents_observer_.reset(new ContentsObserver(web_contents)); |
| 76 favicon_loader_.reset(new LauncherFaviconLoader(this, web_contents)); |
| 77 } |
| 78 |
| 79 // LauncherFaviconLoader::Delegate overrides: |
| 80 virtual void FaviconUpdated() OVERRIDE { |
| 81 favicon_updated_ = true; |
| 74 } | 82 } |
| 75 | 83 |
| 76 protected: | 84 protected: |
| 77 void NavigateTo(const char* url) { | 85 bool NavigateTo(const char* url) { |
| 78 Browser* browser = GetPanelBrowser(); | |
| 79 std::string url_path = base::StringPrintf("files/ash/launcher/%s", url); | 86 std::string url_path = base::StringPrintf("files/ash/launcher/%s", url); |
| 80 ui_test_utils::NavigateToURL(browser, test_server()->GetURL(url_path)); | 87 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(url_path)); |
| 88 return true; |
| 81 } | 89 } |
| 82 | 90 |
| 83 Browser* GetPanelBrowser() { | 91 bool WaitForContentsLoaded() { |
| 84 if (!panel_browser_) { | 92 const int64 max_seconds = 10; |
| 85 panel_browser_ = new Browser(Browser::CreateParams::CreateForApp( | |
| 86 Browser::TYPE_PANEL, "Test Panel", gfx::Rect(), | |
| 87 browser()->profile(), browser()->host_desktop_type())); | |
| 88 EXPECT_TRUE(panel_browser_->is_type_panel()); | |
| 89 // Load initial web contents before setting the observer. | |
| 90 ui_test_utils::NavigateToURL(panel_browser_, GURL()); | |
| 91 EXPECT_FALSE(contents_observer_.get()); | |
| 92 contents_observer_.reset( | |
| 93 new ContentsObserver( | |
| 94 panel_browser_->tab_strip_model()->GetWebContentsAt(0))); | |
| 95 } | |
| 96 return panel_browser_; | |
| 97 } | |
| 98 | |
| 99 LauncherFaviconLoader* GetFaviconLoader() { | |
| 100 if (!loader_) { | |
| 101 Browser* browser = GetPanelBrowser(); | |
| 102 BrowserView* browser_view = static_cast<BrowserView*>(browser->window()); | |
| 103 BrowserLauncherItemController* launcher_item_controller = | |
| 104 browser_view->launcher_item_controller(); | |
| 105 if (!launcher_item_controller) | |
| 106 return NULL; | |
| 107 EXPECT_EQ(BrowserLauncherItemController::TYPE_EXTENSION_PANEL, | |
| 108 launcher_item_controller->type()); | |
| 109 loader_ = launcher_item_controller->favicon_loader(); | |
| 110 } | |
| 111 return loader_; | |
| 112 } | |
| 113 | |
| 114 void ResetDownloads() { | |
| 115 ASSERT_NE(static_cast<void*>(NULL), contents_observer_.get()); | |
| 116 contents_observer_->Reset(); | |
| 117 } | |
| 118 | |
| 119 bool WaitForFaviconDownloads() { | |
| 120 LauncherFaviconLoader* loader = GetFaviconLoader(); | |
| 121 if (!loader) | |
| 122 return false; | |
| 123 | |
| 124 const int64 max_seconds = 2; | |
| 125 base::Time start_time = base::Time::Now(); | 93 base::Time start_time = base::Time::Now(); |
| 126 while (!contents_observer_->got_favicons() || | 94 while (!(contents_observer_->loaded() && |
| 127 loader->HasPendingDownloads()) { | 95 contents_observer_->got_favicons())) { |
| 128 content::RunAllPendingInMessageLoop(); | 96 content::RunAllPendingInMessageLoop(); |
| 129 base::TimeDelta delta = base::Time::Now() - start_time; | 97 base::TimeDelta delta = base::Time::Now() - start_time; |
| 130 if (delta.InSeconds() >= max_seconds) { | 98 if (delta.InSeconds() >= max_seconds) { |
| 131 LOG(ERROR) << " WaitForFaviconDownlads timed out:" | 99 LOG(ERROR) << " WaitForContentsLoaded timed out."; |
| 132 << " Got Favicons:" << contents_observer_->got_favicons() | |
| 133 << " Pending Downloads: " << loader->HasPendingDownloads(); | |
| 134 return false; | 100 return false; |
| 135 } | 101 } |
| 136 } | 102 } |
| 137 return true; | 103 return true; |
| 138 } | 104 } |
| 139 | 105 |
| 106 bool WaitForFaviconUpdated() { |
| 107 const int64 max_seconds = 10; |
| 108 base::Time start_time = base::Time::Now(); |
| 109 while (favicon_loader_->HasPendingDownloads()) { |
| 110 content::RunAllPendingInMessageLoop(); |
| 111 base::TimeDelta delta = base::Time::Now() - start_time; |
| 112 if (delta.InSeconds() >= max_seconds) { |
| 113 LOG(ERROR) << " WaitForFaviconDownlads timed out."; |
| 114 return false; |
| 115 } |
| 116 } |
| 117 return true; |
| 118 } |
| 119 |
| 120 void ResetDownloads() { |
| 121 contents_observer_->Reset(); |
| 122 } |
| 123 |
| 124 bool favicon_updated_; |
| 125 scoped_ptr<ContentsObserver> contents_observer_; |
| 126 scoped_ptr<LauncherFaviconLoader> favicon_loader_; |
| 127 |
| 140 private: | 128 private: |
| 141 Browser* panel_browser_; | 129 DISALLOW_COPY_AND_ASSIGN(LauncherFaviconLoaderBrowsertest); |
| 142 LauncherFaviconLoader* loader_; | |
| 143 scoped_ptr<ContentsObserver> contents_observer_; | |
| 144 }; | 130 }; |
| 145 | 131 |
| 146 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, SmallLauncherIcon) { | 132 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, SmallLauncherIcon) { |
| 147 ASSERT_TRUE(test_server()->Start()); | 133 ASSERT_TRUE(test_server()->Start()); |
| 148 NavigateTo("launcher-smallfavicon.html"); | 134 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html")); |
| 149 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(); | 135 EXPECT_TRUE(WaitForContentsLoaded()); |
| 150 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | 136 EXPECT_TRUE(WaitForFaviconUpdated()); |
| 151 EXPECT_TRUE(WaitForFaviconDownloads()); | 137 |
| 152 // No large favicons specified, bitmap should be empty. | 138 // No large favicons specified, bitmap should be empty. |
| 153 EXPECT_TRUE(favicon_loader->GetFavicon().empty()); | 139 EXPECT_TRUE(favicon_loader_->GetFavicon().empty()); |
| 154 } | 140 } |
| 155 | 141 |
| 156 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, LargeLauncherIcon) { | 142 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, LargeLauncherIcon) { |
| 157 ASSERT_TRUE(test_server()->Start()); | 143 ASSERT_TRUE(test_server()->Start()); |
| 158 NavigateTo("launcher-largefavicon.html"); | 144 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html")); |
| 159 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(); | 145 EXPECT_TRUE(WaitForContentsLoaded()); |
| 160 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | 146 EXPECT_TRUE(WaitForFaviconUpdated()); |
| 161 EXPECT_TRUE(WaitForFaviconDownloads()); | 147 |
| 162 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | 148 EXPECT_FALSE(favicon_loader_->GetFavicon().empty()); |
| 163 EXPECT_EQ(128, favicon_loader->GetFavicon().height()); | 149 EXPECT_EQ(128, favicon_loader_->GetFavicon().height()); |
| 164 } | 150 } |
| 165 | 151 |
| 166 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ManyLauncherIcons) { | 152 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ManyLauncherIcons) { |
| 167 ASSERT_TRUE(test_server()->Start()); | 153 ASSERT_TRUE(test_server()->Start()); |
| 168 NavigateTo("launcher-manyfavicon.html"); | 154 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html")); |
| 169 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(); | 155 EXPECT_TRUE(WaitForContentsLoaded()); |
| 170 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | 156 EXPECT_TRUE(WaitForFaviconUpdated()); |
| 171 | 157 |
| 172 EXPECT_TRUE(WaitForFaviconDownloads()); | 158 EXPECT_FALSE(favicon_loader_->GetFavicon().empty()); |
| 173 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | |
| 174 // When multiple favicons are present, the correctly sized icon should be | 159 // When multiple favicons are present, the correctly sized icon should be |
| 175 // chosen. The icons are sized assuming ash::kLauncherPreferredSize < 128. | 160 // chosen. The icons are sized assuming ash::kLauncherPreferredSize < 128. |
| 176 EXPECT_GT(128, ash::kLauncherPreferredSize); | 161 EXPECT_GT(128, ash::kLauncherPreferredSize); |
| 177 EXPECT_EQ(48, favicon_loader->GetFavicon().height()); | 162 EXPECT_EQ(48, favicon_loader_->GetFavicon().height()); |
| 178 } | 163 } |
| 179 | 164 |
| 180 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ChangeLauncherIcons) { | 165 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ChangeLauncherIcons) { |
| 181 ASSERT_TRUE(test_server()->Start()); | 166 ASSERT_TRUE(test_server()->Start()); |
| 182 NavigateTo("launcher-manyfavicon.html"); | 167 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html")); |
| 183 LauncherFaviconLoader* favicon_loader = GetFaviconLoader(); | 168 EXPECT_TRUE(WaitForContentsLoaded()); |
| 184 ASSERT_NE(static_cast<LauncherFaviconLoader*>(NULL), favicon_loader); | 169 EXPECT_TRUE(WaitForFaviconUpdated()); |
| 185 | 170 |
| 186 EXPECT_TRUE(WaitForFaviconDownloads()); | 171 EXPECT_FALSE(favicon_loader_->GetFavicon().empty()); |
| 187 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | 172 EXPECT_EQ(48, favicon_loader_->GetFavicon().height()); |
| 188 EXPECT_EQ(48, favicon_loader->GetFavicon().height()); | |
| 189 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); | 173 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); |
| 190 | 174 |
| 191 NavigateTo("launcher-smallfavicon.html"); | 175 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html")); |
| 192 EXPECT_TRUE(WaitForFaviconDownloads()); | 176 ASSERT_TRUE(WaitForContentsLoaded()); |
| 193 EXPECT_TRUE(favicon_loader->GetFavicon().empty()); | 177 EXPECT_TRUE(WaitForFaviconUpdated()); |
| 178 |
| 179 EXPECT_TRUE(favicon_loader_->GetFavicon().empty()); |
| 194 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); | 180 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); |
| 195 | 181 |
| 196 NavigateTo("launcher-largefavicon.html"); | 182 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html")); |
| 197 EXPECT_TRUE(WaitForFaviconDownloads()); | 183 ASSERT_TRUE(WaitForContentsLoaded()); |
| 198 EXPECT_FALSE(favicon_loader->GetFavicon().empty()); | 184 EXPECT_TRUE(WaitForFaviconUpdated()); |
| 199 EXPECT_EQ(128, favicon_loader->GetFavicon().height()); | 185 |
| 186 EXPECT_FALSE(favicon_loader_->GetFavicon().empty()); |
| 187 EXPECT_EQ(128, favicon_loader_->GetFavicon().height()); |
| 200 } | 188 } |
| OLD | NEW |