Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / ash / launcher / launcher_favicon_loader_browsertest.cc
blob85a93a29edfb0c7e09712098f5a4ff0283a29887
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;
21 namespace {
23 // Observer class to determine when favicons have completed loading.
24 class ContentsObserver : public WebContentsObserver {
25 public:
26 explicit ContentsObserver(WebContents* web_contents)
27 : WebContentsObserver(web_contents),
28 loaded_(false),
29 got_favicons_(false) {
32 ~ContentsObserver() override {}
34 void Reset() {
35 got_favicons_ = false;
38 bool loaded() const { return loaded_; }
39 bool got_favicons() const { return got_favicons_; }
41 // WebContentsObserver overrides.
42 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
43 const GURL& validated_url) override {
44 loaded_ = true;
47 void DidUpdateFaviconURL(
48 const std::vector<content::FaviconURL>& candidates) override {
49 if (!candidates.empty())
50 got_favicons_ = true;
53 private:
54 bool loaded_;
55 bool got_favicons_;
58 } // namespace
60 class LauncherFaviconLoaderBrowsertest
61 : public InProcessBrowserTest,
62 public LauncherFaviconLoader::Delegate {
63 public:
64 LauncherFaviconLoaderBrowsertest() : favicon_updated_(false) {
67 ~LauncherFaviconLoaderBrowsertest() override {}
69 void SetUpOnMainThread() override {
70 WebContents* web_contents =
71 browser()->tab_strip_model()->GetActiveWebContents();
72 contents_observer_.reset(new ContentsObserver(web_contents));
73 favicon_loader_.reset(new LauncherFaviconLoader(this, web_contents));
76 // LauncherFaviconLoader::Delegate overrides:
77 void FaviconUpdated() override { favicon_updated_ = true; }
79 protected:
80 bool NavigateTo(const char* url) {
81 std::string url_path = base::StringPrintf("files/ash/launcher/%s", url);
82 ui_test_utils::NavigateToURL(browser(), test_server()->GetURL(url_path));
83 return true;
86 bool WaitForContentsLoaded() {
87 const int64 max_seconds = 10;
88 base::Time start_time = base::Time::Now();
89 while (!(contents_observer_->loaded() &&
90 contents_observer_->got_favicons())) {
91 content::RunAllPendingInMessageLoop();
92 base::TimeDelta delta = base::Time::Now() - start_time;
93 if (delta.InSeconds() >= max_seconds) {
94 LOG(ERROR) << " WaitForContentsLoaded timed out.";
95 return false;
98 return true;
101 bool WaitForFaviconUpdated() {
102 const int64 max_seconds = 10;
103 base::Time start_time = base::Time::Now();
104 while (favicon_loader_->HasPendingDownloads()) {
105 content::RunAllPendingInMessageLoop();
106 base::TimeDelta delta = base::Time::Now() - start_time;
107 if (delta.InSeconds() >= max_seconds) {
108 LOG(ERROR) << " WaitForFaviconDownlads timed out.";
109 return false;
112 return true;
115 void ResetDownloads() {
116 contents_observer_->Reset();
119 bool favicon_updated_;
120 scoped_ptr<ContentsObserver> contents_observer_;
121 scoped_ptr<LauncherFaviconLoader> favicon_loader_;
123 private:
124 DISALLOW_COPY_AND_ASSIGN(LauncherFaviconLoaderBrowsertest);
127 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, SmallLauncherIcon) {
128 ASSERT_TRUE(test_server()->Start());
129 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html"));
130 EXPECT_TRUE(WaitForContentsLoaded());
131 EXPECT_TRUE(WaitForFaviconUpdated());
133 // No large favicons specified, bitmap should be empty.
134 EXPECT_TRUE(favicon_loader_->GetFavicon().empty());
137 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, LargeLauncherIcon) {
138 ASSERT_TRUE(test_server()->Start());
139 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html"));
140 EXPECT_TRUE(WaitForContentsLoaded());
141 EXPECT_TRUE(WaitForFaviconUpdated());
143 EXPECT_FALSE(favicon_loader_->GetFavicon().empty());
144 EXPECT_EQ(128, favicon_loader_->GetFavicon().height());
147 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ManyLauncherIcons) {
148 ASSERT_TRUE(test_server()->Start());
149 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html"));
150 EXPECT_TRUE(WaitForContentsLoaded());
151 EXPECT_TRUE(WaitForFaviconUpdated());
153 EXPECT_FALSE(favicon_loader_->GetFavicon().empty());
154 // When multiple favicons are present, the correctly sized icon should be
155 // chosen. The icons are sized assuming ash::kShelfSize < 128.
156 EXPECT_GT(128, ash::kShelfSize);
157 EXPECT_EQ(48, favicon_loader_->GetFavicon().height());
160 IN_PROC_BROWSER_TEST_F(LauncherFaviconLoaderBrowsertest, ChangeLauncherIcons) {
161 ASSERT_TRUE(test_server()->Start());
162 ASSERT_TRUE(NavigateTo("launcher-manyfavicon.html"));
163 EXPECT_TRUE(WaitForContentsLoaded());
164 EXPECT_TRUE(WaitForFaviconUpdated());
166 EXPECT_FALSE(favicon_loader_->GetFavicon().empty());
167 EXPECT_EQ(48, favicon_loader_->GetFavicon().height());
168 ASSERT_NO_FATAL_FAILURE(ResetDownloads());
170 ASSERT_TRUE(NavigateTo("launcher-smallfavicon.html"));
171 ASSERT_TRUE(WaitForContentsLoaded());
172 EXPECT_TRUE(WaitForFaviconUpdated());
174 EXPECT_TRUE(favicon_loader_->GetFavicon().empty());
175 ASSERT_NO_FATAL_FAILURE(ResetDownloads());
177 ASSERT_TRUE(NavigateTo("launcher-largefavicon.html"));
178 ASSERT_TRUE(WaitForContentsLoaded());
179 EXPECT_TRUE(WaitForFaviconUpdated());
181 EXPECT_FALSE(favicon_loader_->GetFavicon().empty());
182 EXPECT_EQ(128, favicon_loader_->GetFavicon().height());