| 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 "chrome/browser/ui/ash/launcher/launcher_favicon_loader.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "ash/shelf/shelf_constants.h" | |
| 12 #include "base/macros.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "chrome/browser/ui/browser.h" | |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.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_observer.h" | |
| 20 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | |
| 22 | |
| 23 using content::WebContents; | |
| 24 using content::WebContentsObserver; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // Observer class to determine when favicons have completed loading. | |
| 29 class ContentsObserver : public WebContentsObserver { | |
| 30 public: | |
| 31 explicit ContentsObserver(WebContents* web_contents) | |
| 32 : WebContentsObserver(web_contents), | |
| 33 loaded_(false), | |
| 34 got_favicons_(false) { | |
| 35 } | |
| 36 | |
| 37 ~ContentsObserver() override {} | |
| 38 | |
| 39 void Reset() { | |
| 40 got_favicons_ = false; | |
| 41 } | |
| 42 | |
| 43 bool loaded() const { return loaded_; } | |
| 44 bool got_favicons() const { return got_favicons_; } | |
| 45 | |
| 46 // WebContentsObserver overrides. | |
| 47 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | |
| 48 const GURL& validated_url) override { | |
| 49 loaded_ = true; | |
| 50 } | |
| 51 | |
| 52 void DidUpdateFaviconURL( | |
| 53 const std::vector<content::FaviconURL>& candidates) override { | |
| 54 if (!candidates.empty()) | |
| 55 got_favicons_ = true; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 bool loaded_; | |
| 60 bool got_favicons_; | |
| 61 }; | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 class LauncherFaviconLoaderBrowsertest | |
| 66 : public InProcessBrowserTest, | |
| 67 public LauncherFaviconLoader::Delegate { | |
| 68 public: | |
| 69 LauncherFaviconLoaderBrowsertest() : favicon_updated_(false) { | |
| 70 } | |
| 71 | |
| 72 ~LauncherFaviconLoaderBrowsertest() override {} | |
| 73 | |
| 74 void SetUpOnMainThread() override { | |
| 75 WebContents* web_contents = | |
| 76 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 77 contents_observer_.reset(new ContentsObserver(web_contents)); | |
| 78 favicon_loader_.reset(new LauncherFaviconLoader(this, web_contents)); | |
| 79 } | |
| 80 | |
| 81 // LauncherFaviconLoader::Delegate overrides: | |
| 82 void FaviconUpdated() override { favicon_updated_ = true; } | |
| 83 | |
| 84 protected: | |
| 85 bool NavigateTo(const char* url) { | |
| 86 std::string url_path = base::StringPrintf("/ash/launcher/%s", url); | |
| 87 ui_test_utils::NavigateToURL(browser(), | |
| 88 embedded_test_server()->GetURL(url_path)); | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool WaitForContentsLoaded() { | |
| 93 const int64_t 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."; | |
| 101 return false; | |
| 102 } | |
| 103 } | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 bool WaitForFaviconUpdated() { | |
| 108 const int64_t 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."; | |
| 115 return false; | |
| 116 } | |
| 117 } | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 void ResetDownloads() { | |
| 122 contents_observer_->Reset(); | |
| 123 } | |
| 124 | |
| 125 bool favicon_updated_; | |
| 126 std::unique_ptr<ContentsObserver> contents_observer_; | |
| 127 std::unique_ptr<LauncherFaviconLoader> favicon_loader_; | |
| 128 | |
| 129 private: | |
| 130 DISALLOW_COPY_AND_ASSIGN(LauncherFaviconLoaderBrowsertest); | |
| 131 }; | |
| 132 | |
| 133 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, SmallLauncherIcon) { | |
| 134 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 135 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html")); | |
| 136 EXPECT_TRUE(WaitForContentsLoaded()); | |
| 137 EXPECT_TRUE(WaitForFaviconUpdated()); | |
| 138 | |
| 139 // No large favicons specified, bitmap should be empty. | |
| 140 EXPECT_TRUE(favicon_loader_->GetFavicon().empty()); | |
| 141 } | |
| 142 | |
| 143 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, LargeLauncherIcon) { | |
| 144 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 145 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html")); | |
| 146 EXPECT_TRUE(WaitForContentsLoaded()); | |
| 147 EXPECT_TRUE(WaitForFaviconUpdated()); | |
| 148 | |
| 149 EXPECT_FALSE(favicon_loader_->GetFavicon().empty()); | |
| 150 EXPECT_EQ(128, favicon_loader_->GetFavicon().height()); | |
| 151 } | |
| 152 | |
| 153 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ManyLauncherIcons) { | |
| 154 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 155 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html")); | |
| 156 EXPECT_TRUE(WaitForContentsLoaded()); | |
| 157 EXPECT_TRUE(WaitForFaviconUpdated()); | |
| 158 | |
| 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::kShelfSize < 128. | |
| 162 EXPECT_GT(128, ash::kShelfSize); | |
| 163 EXPECT_EQ(48, favicon_loader_->GetFavicon().height()); | |
| 164 } | |
| 165 | |
| 166 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ChangeLauncherIcons) { | |
| 167 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 168 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html")); | |
| 169 EXPECT_TRUE(WaitForContentsLoaded()); | |
| 170 EXPECT_TRUE(WaitForFaviconUpdated()); | |
| 171 | |
| 172 EXPECT_FALSE(favicon_loader_->GetFavicon().empty()); | |
| 173 EXPECT_EQ(48, favicon_loader_->GetFavicon().height()); | |
| 174 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); | |
| 175 | |
| 176 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html")); | |
| 177 ASSERT_TRUE(WaitForContentsLoaded()); | |
| 178 EXPECT_TRUE(WaitForFaviconUpdated()); | |
| 179 | |
| 180 EXPECT_TRUE(favicon_loader_->GetFavicon().empty()); | |
| 181 ASSERT_NO_FATAL_FAILURE(ResetDownloads()); | |
| 182 | |
| 183 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html")); | |
| 184 ASSERT_TRUE(WaitForContentsLoaded()); | |
| 185 EXPECT_TRUE(WaitForFaviconUpdated()); | |
| 186 | |
| 187 EXPECT_FALSE(favicon_loader_->GetFavicon().empty()); | |
| 188 EXPECT_EQ(128, favicon_loader_->GetFavicon().height()); | |
| 189 } | |
| OLD | NEW |