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

Side by Side Diff: chrome/browser/ui/toolbar/back_forward_menu_model_favicon_unittest.cc

Issue 2928005: Retrieve favicons from history as NavigationEntries are converted from TabNav... Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 12 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/toolbar/back_forward_menu_model.h"
6
7 #include "base/message_loop.h"
8 #include "base/path_service.h"
9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/browser_thread.h"
12 #include "chrome/browser/history/history.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/browser/renderer_host/site_instance.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/tab_contents/navigation_controller.h"
17 #include "chrome/browser/tab_contents/navigation_entry.h"
18 #include "chrome/browser/tab_contents/tab_contents.h"
19 #include "chrome/browser/tab_contents/test_tab_contents.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/test/browser_with_test_window_test.h"
22 #include "chrome/test/testing_profile.h"
23 #include "gfx/codec/png_codec.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26
27 namespace {
28
29 static const int kIconWidth = 16;
30 static const int kIconHeight = 16;
31
32 void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) {
33 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
34 bmp->allocPixels();
35
36 uint32_t* src_data = bmp->getAddr32(0, 0);
37 for (int i = 0; i < w * h; i++) {
38 src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240);
39 }
40 }
41
42 } // namespace
43
44 class FaviconDelegate : public menus::MenuModel::Delegate {
45 public:
46 explicit FaviconDelegate(MessageLoop* loop) : loop_(loop) {
47 DCHECK(loop);
48 }
49
50 void OnIconChanged(int model_index) {
51 loop_->Quit();
52 }
53
54 private:
55 MessageLoop* loop_;
56 DISALLOW_COPY_AND_ASSIGN(FaviconDelegate);
57 };
58
59 class BackFwdMenuModelFaviconTest : public BrowserWithTestWindowTest {
60 };
61
62 TEST_F(BackFwdMenuModelFaviconTest, FaviconLoadTest) {
63 profile()->CreateHistoryService(true, false);
64 profile()->CreateFaviconService();
65
66 GURL url1 = GURL("http://www.a.com/1");
67 GURL url2 = GURL("http://www.a.com/2");
68 GURL url1_favicon("http://www.a.com/1/favicon.ico");
69
70 AddTab(browser(), url1);
71
72 NavigationController& controller =
73 browser()->GetTabContentsAt(0)->controller();
74
75 FaviconDelegate favicon_delegate(MessageLoop::current());
76
77 scoped_ptr<BackForwardMenuModel> back_model(new BackForwardMenuModel(
78 browser(), BackForwardMenuModel::BACKWARD_MENU));
79 back_model->set_test_tab_contents(controller.tab_contents());
80 back_model->SetDelegate(&favicon_delegate);
81
82 scoped_ptr<BackForwardMenuModel> forward_model(new BackForwardMenuModel(
83 browser(), BackForwardMenuModel::FORWARD_MENU));
84 forward_model->set_test_tab_contents(controller.tab_contents());
85 forward_model->SetDelegate(&favicon_delegate);
86
87 SkBitmap new_icon;
88 MakeTestSkBitmap(kIconWidth, kIconHeight, &new_icon);
89 std::vector<unsigned char> icon_data;
90 gfx::PNGCodec::EncodeBGRASkBitmap(new_icon, false, &icon_data);
91
92 // NavigateAndCommitActiveTab(url1);
93 // Navigate to a new URL so that url1 will be in the BackForwardMenuModel.
94 NavigateAndCommitActiveTab(url2);
95
96 // Set the desired favicon for url1.
97 profile()->GetHistoryService(Profile::EXPLICIT_ACCESS)->AddPage(url1,
98 history::SOURCE_BROWSED);
99 profile()->GetFaviconService(Profile::EXPLICIT_ACCESS)->SetFavicon(url1,
100 url1_favicon, icon_data);
101 MessageLoop::current()->RunAllPending();
102
103 // Will return the current icon (default) but start an anync call
104 // to retrieve the favicon from the favicon service.
105 SkBitmap default_icon;
106 back_model->GetIconAt(0, &default_icon);
107
108 // Make the favicon service run GetFavIconForURL,
109 // FaviconDelegate.OnIconChanged will be called.
110 MessageLoop::current()->Run();
111
112 SkBitmap valid_icon;
113 // This time we will get the new favicon returned.
114 back_model->GetIconAt(0, &valid_icon);
115 SkAutoLockPixels a(new_icon);
116 SkAutoLockPixels b(valid_icon);
117 SkAutoLockPixels c(default_icon);
118 // Verify we did not get the default favicon.
119 EXPECT_NE(0, memcmp(default_icon.getPixels(), valid_icon.getPixels(),
120 default_icon.getSize()));
121 // Verify we did get the expected favicon.
122 EXPECT_EQ(0, memcmp(new_icon.getPixels(), valid_icon.getPixels(),
123 new_icon.getSize()));
124 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698