Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1022)

Side by Side Diff: chrome/browser/ui/web_applications/hosted_app_tab_helper_unittest.cc

Issue 64853004: Use high resolution icons where possible for streamlined hosted app icons. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@browser_experiment_create_app_from_page
Patch Set: rework, add tests Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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/web_applications/hosted_app_tab_helper.h"
6
7 #include "base/command_line.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/crx_installer.h"
12 #include "chrome/browser/extensions/extension_system.h"
13 #include "chrome/browser/extensions/test_extension_service.h"
14 #include "chrome/browser/extensions/test_extension_system.h"
15 #include "chrome/common/extensions/extension.h"
16 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
17 #include "chrome/common/web_application_info.h"
18 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/notification_source.h"
25 #include "content/public/common/favicon_url.h"
26 #include "content/public/test/test_renderer_host.h"
27 #include "content/public/test/test_utils.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "third_party/skia/include/core/SkBitmap.h"
30
31 using content::RenderViewHostTester;
32
33 namespace {
34
35 // Creates valid SkBitmaps of the dimensions found in |sizes| and pushes them
36 // into |bitmaps|.
37 void CreateTestBitmaps(const std::vector<gfx::Size>& sizes,
38 std::vector<SkBitmap>* bitmaps) {
39 bitmaps->clear();
tapted 2013/11/13 07:44:53 It's pretty common just to return std::vectors for
calamity 2013/11/15 04:25:50 Done.
40 for (size_t i = 0; i < sizes.size(); ++i) {
41 bitmaps->push_back(SkBitmap());
42 SkBitmap& bitmap = bitmaps->back();
43 bitmap.setConfig(SkBitmap::kARGB_8888_Config,
44 sizes[i].width(),
tapted 2013/11/13 07:44:53 nit: outdent 1 space (and next line)
calamity 2013/11/15 04:25:50 Done.
45 sizes[i].height());
46 bitmap.allocPixels();
47 bitmap.eraseColor(SK_ColorRED);
48 }
49 }
50
51 class TestHostedAppTabHelper : public HostedAppTabHelper{
tapted 2013/11/13 07:44:53 nit: space before {
calamity 2013/11/15 04:25:50 Done.
52 public:
53 explicit TestHostedAppTabHelper(content::WebContents* web_contents)
54 : HostedAppTabHelper(web_contents),
55 id_counter_(0) {
56 }
57 virtual ~TestHostedAppTabHelper() {}
58
59 virtual int DownloadImage(const GURL& url) OVERRIDE {
60 return id_counter_++;
61 }
62
63 int id_counter_;
tapted 2013/11/13 07:44:53 nit: private: before
calamity 2013/11/15 04:25:50 Done.
64 };
tapted 2013/11/13 07:44:53 nit: DISALLOW_COPY_AND_ASSIGN(..)
calamity 2013/11/15 04:25:50 Done.
65
66 class HostedAppTabHelperTest : public ChromeRenderViewHostTestHarness,
67 public content::NotificationObserver {
68 protected:
69 virtual void SetUp() OVERRIDE {
70 ChromeRenderViewHostTestHarness::SetUp();
71
72 web_app_info_.app_url = GURL("http://www.google.com");
73 web_app_info_.title = UTF8ToUTF16("Hosted App");
tapted 2013/11/13 07:44:53 nit: I usually see ASCIIToUTF16 for string literal
calamity 2013/11/15 04:25:50 Done.
74 extension_ = NULL;
tapted 2013/11/13 07:44:53 maybe do this in the constructor with an initializ
calamity 2013/11/15 04:25:50 Done.
75
76 // A real extension service is needed for the CrxInstaller.
77 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
78 static_cast<extensions::TestExtensionSystem*>(
79 extensions::ExtensionSystem::Get(profile()))->
80 CreateExtensionService(
81 CommandLine::ForCurrentProcess(),
82 temp_dir_.path(),
83 false);
84
85 HostedAppTabHelper::CreateForWebContents(web_contents());
86
87 // Listen for notifications which indicate the hosted app installation has
88 // finished.
89 registrar_.Add(this,
90 chrome::NOTIFICATION_CRX_INSTALLER_DONE,
91 content::Source<extensions::CrxInstaller>(NULL));
92
93 registrar_.Add(this,
94 chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR,
95 content::NotificationService::AllSources());
96 }
97
98
tapted 2013/11/13 07:44:53 nit: extra blank line
calamity 2013/11/15 04:25:50 Done.
99 void Observe(int type,
100 const content::NotificationSource& source,
101 const content::NotificationDetails& details) {
tapted 2013/11/13 07:44:53 OVERRIDE?, and // Overridden from content::Notific
calamity 2013/11/15 04:25:50 Done.
102 if (type == chrome::NOTIFICATION_CRX_INSTALLER_DONE)
103 extension_ = content::Details<const extensions::Extension>(details).ptr();
104
105 base::MessageLoop::current()->Quit();
106 }
107
tapted 2013/11/13 07:44:53 private: from here?
calamity 2013/11/15 04:25:50 Done.
tapted 2013/11/17 23:28:50 optional: members on test harnesses can be protect
108 const extensions::Extension* extension_;
109 WebApplicationInfo web_app_info_;
110 content::NotificationRegistrar registrar_;
111 base::ScopedTempDir temp_dir_;
112 };
tapted 2013/11/13 07:44:53 nit: DISALLOW_COPY_AND_ASSIGN(..)
calamity 2013/11/15 04:25:50 Done.
113
114 } // namespace
115
116 TEST_F(HostedAppTabHelperTest, DownloadWindowIcon) {
117 TestHostedAppTabHelper helper(web_contents());
118 // Make the helper fetch icons immediately.
119 helper.SetDelegate(NULL, true);
120
121 std::vector<content::FaviconURL> favicon_urls;
122 favicon_urls.push_back(
123 content::FaviconURL(GURL("http://www.google.com/favicon.ico"),
124 content::FaviconURL::FAVICON));
125
126 // Updating the favicon should result in the download being initiated.
127 helper.DidUpdateFaviconURL(0, favicon_urls);
128
129 EXPECT_EQ(1, helper.pending_requests());
130
131 std::vector<gfx::Size> sizes;
tapted 2013/11/13 07:44:53 nit: I'd make a vector of size 1 like std::vector
calamity 2013/11/15 04:25:50 Done.
132 sizes.push_back(gfx::Size(32, 32));
133 std::vector<SkBitmap> bitmaps;
134 CreateTestBitmaps(sizes, &bitmaps);
135 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, bitmaps, sizes);
136 EXPECT_EQ(0, helper.pending_requests());
137
138 EXPECT_EQ(1, helper.image_family().size());
tapted 2013/11/13 07:44:53 I'm surprised you don't need to make the integer l
calamity 2013/11/15 04:25:50 Done.
139 EXPECT_EQ(gfx::Size(32, 32), helper.image_family().GetBest(0, 0)->Size());
140 }
141
142 TEST_F(HostedAppTabHelperTest, CreateHostedApp) {
143 TestHostedAppTabHelper helper(web_contents());
144
145 std::vector<content::FaviconURL> favicon_urls;
146 favicon_urls.push_back(
147 content::FaviconURL(GURL("http://www.google.com/favicon.ico"),
148 content::FaviconURL::FAVICON));
149
150 // Download should not be initiated because |fetch_icons_immediately| is not
151 // set.
152 helper.DidUpdateFaviconURL(0, favicon_urls);
153 EXPECT_EQ(0, helper.pending_requests());
154
155 // Create hosted app which will initiate download.
156 helper.CreateHostedApp(web_app_info_);
157 EXPECT_EQ(1, helper.pending_requests());
158
159 std::vector<gfx::Size> sizes;
160 sizes.push_back(gfx::Size(32, 32));
161 std::vector<SkBitmap> bitmaps;
162 CreateTestBitmaps(sizes, &bitmaps);
163 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, bitmaps, sizes);
164
165 // Create and install the hosted app.
166 content::RunMessageLoop();
167
168 EXPECT_TRUE(extension_);
169 EXPECT_TRUE(extension_->from_bookmark());
170
171 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension_);
172 EXPECT_EQ(1, icons.map().size());
173 EXPECT_FALSE(icons.Get(32, ExtensionIconSet::MATCH_EXACTLY).empty());
174 }
175
176 TEST_F(HostedAppTabHelperTest, CreateHostedAppBeforeFaviconURLUpdate) {
177 TestHostedAppTabHelper helper(web_contents());
178
179 web_app_info_.icons.push_back(WebApplicationInfo::IconInfo());
180 web_app_info_.icons[0].url = GURL("http://www.google.com/favicon2.ico");
181
182 // Initiate creation of hosted app before favicon URLs are loaded.
183 helper.CreateHostedApp(web_app_info_);
184
185 std::vector<content::FaviconURL> favicon_urls;
186 favicon_urls.push_back(
187 content::FaviconURL(GURL("http://www.google.com/favicon.ico"),
tapted 2013/11/13 07:44:53 maybe include favicon2.ico as well to get coverage
calamity 2013/11/15 04:25:50 Done.
188 content::FaviconURL::FAVICON));
189 helper.DidUpdateFaviconURL(0, favicon_urls);
190
191 std::vector<gfx::Size> sizes_1;
192 std::vector<SkBitmap> bitmaps_1;
193 sizes_1.push_back(gfx::Size(16, 16));
194 CreateTestBitmaps(sizes_1, &bitmaps_1);
195 helper.DidDownloadFavicon(0, 200, favicon_urls[0].icon_url, bitmaps_1,
196 sizes_1);
197
198 std::vector<gfx::Size> sizes_2;
199 std::vector<SkBitmap> bitmaps_2;
200 sizes_2.push_back(gfx::Size(32, 32));
201 sizes_2.push_back(gfx::Size(64, 64));
202 CreateTestBitmaps(sizes_2, &bitmaps_2);
203 helper.DidDownloadFavicon(1, 200, web_app_info_.icons[0].url, bitmaps_2,
204 sizes_2);
205
206 // Create and install the hosted app.
207 content::RunMessageLoop();
208
209 EXPECT_TRUE(extension_);
210 EXPECT_TRUE(extension_->from_bookmark());
211
212 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension_);
213 EXPECT_EQ(3, icons.map().size());
214 EXPECT_FALSE(icons.Get(16, ExtensionIconSet::MATCH_EXACTLY).empty());
215 EXPECT_FALSE(icons.Get(32, ExtensionIconSet::MATCH_EXACTLY).empty());
216 EXPECT_FALSE(icons.Get(64, ExtensionIconSet::MATCH_EXACTLY).empty());
217 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698