Chromium Code Reviews| Index: chrome/browser/ui/toolbar/back_forward_menu_model_unittest.cc |
| diff --git a/chrome/browser/ui/toolbar/back_forward_menu_model_unittest.cc b/chrome/browser/ui/toolbar/back_forward_menu_model_unittest.cc |
| index 5e8d1d4ec392a9287b9e420439b21ffd23e36451..2240dd194eabc53928fdef65f9d712109841a6ea 100644 |
| --- a/chrome/browser/ui/toolbar/back_forward_menu_model_unittest.cc |
| +++ b/chrome/browser/ui/toolbar/back_forward_menu_model_unittest.cc |
| @@ -7,14 +7,49 @@ |
| #include "base/path_service.h" |
| #include "base/string_util.h" |
| #include "base/utf_string_conversions.h" |
| +#include "content/browser/browser_thread.h" |
|
sky
2011/03/18 17:54:11
Should be around line 16.
|
| +#include "chrome/browser/history/history.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/browser.h" |
| #include "chrome/common/url_constants.h" |
| +#include "chrome/test/testing_profile.h" |
| #include "content/browser/renderer_host/test_render_view_host.h" |
| #include "content/browser/tab_contents/navigation_controller.h" |
| #include "content/browser/tab_contents/navigation_entry.h" |
| #include "content/browser/tab_contents/tab_contents.h" |
| #include "content/browser/tab_contents/test_tab_contents.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/gfx/codec/png_codec.h" |
| + |
| +namespace { |
| + |
| +// Creates a bitmap of the specified color. |
| +SkBitmap CreateBitmap(SkColor color) { |
| + SkBitmap bitmap; |
| + bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
| + bitmap.allocPixels(); |
| + bitmap.eraseColor(color); |
| + return bitmap; |
| +} |
| + |
| +class FaviconDelegate : public ui::MenuModelDelegate { |
| + public: |
| + explicit FaviconDelegate() : was_called_(false) {} |
|
sky
2011/03/18 17:54:11
no explicit
|
| + |
| + void OnIconChanged(int model_index) { |
| + was_called_ = true; |
| + MessageLoop::current()->Quit(); |
| + } |
| + |
| + bool was_called() { return was_called_; } |
|
sky
2011/03/18 17:54:11
const
|
| + |
| + private: |
| + bool was_called_; |
| + DISALLOW_COPY_AND_ASSIGN(FaviconDelegate); |
|
sky
2011/03/18 17:54:11
newline between 48 and 49.
|
| +}; |
| + |
| +} // namespace |
| class BackFwdMenuModelTest : public RenderViewHostTestHarness { |
| public: |
| @@ -450,3 +485,66 @@ TEST_F(BackFwdMenuModelTest, EscapeLabel) { |
| EXPECT_EQ(ASCIIToUTF16("A &&&&&& B"), back_model->GetLabelAt(0)); |
| #endif // defined(OS_MACOSX) |
| } |
| + |
| +// Test asynchronous loading of favicon from history service. |
| +TEST_F(BackFwdMenuModelTest, FaviconLoadTest) { |
| + profile()->CreateHistoryService(true, false); |
| + profile()->CreateFaviconService(); |
| + Browser browser(Browser::TYPE_NORMAL, profile()); |
| + FaviconDelegate favicon_delegate; |
| + |
| + BackForwardMenuModel back_model( |
| + &browser, BackForwardMenuModel::BACKWARD_MENU); |
|
sky
2011/03/18 17:54:11
indented 4 spaces.
|
| + back_model.set_test_tab_contents(controller().tab_contents()); |
| + back_model.SetMenuModelDelegate(&favicon_delegate); |
| + |
| + SkBitmap new_icon(CreateBitmap(SK_ColorRED)); |
| + std::vector<unsigned char> icon_data; |
| + gfx::PNGCodec::EncodeBGRASkBitmap(new_icon, false, &icon_data); |
| + |
| + GURL url1 = GURL("http://www.a.com/1"); |
| + GURL url2 = GURL("http://www.a.com/2"); |
| + GURL url1_favicon("http://www.a.com/1/favicon.ico"); |
| + |
| + NavigateAndCommit(url1); |
| + // Navigate to a new URL so that url1 will be in the BackForwardMenuModel. |
| + NavigateAndCommit(url2); |
| + |
| + // Set the desired favicon for url1. |
| + profile()->GetHistoryService(Profile::EXPLICIT_ACCESS)->AddPage(url1, |
| + history::SOURCE_BROWSED); |
| + profile()->GetFaviconService(Profile::EXPLICIT_ACCESS)->SetFavicon(url1, |
| + url1_favicon, icon_data, history::FAVICON); |
|
sky
2011/03/18 17:54:11
indented 4.
|
| + |
| + // Will return the current icon (default) but start an anync call |
| + // to retrieve the favicon from the favicon service. |
| + SkBitmap default_icon; |
| + back_model.GetIconAt(0, &default_icon); |
| + |
| + // Make the favicon service run GetFavIconForURL, |
| + // FaviconDelegate.OnIconChanged will be called. |
| + MessageLoop::current()->Run(); |
| + |
| + // Verify that the callback executed. |
| + EXPECT_TRUE(favicon_delegate.was_called()); |
| + |
| + // Verify the bitmaps match. |
| + SkBitmap valid_icon; |
| + // This time we will get the new favicon returned. |
| + back_model.GetIconAt(0, &valid_icon); |
| + SkAutoLockPixels a(new_icon); |
| + SkAutoLockPixels b(valid_icon); |
| + SkAutoLockPixels c(default_icon); |
| + // Verify we did not get the default favicon. |
| + EXPECT_NE(0, memcmp(default_icon.getPixels(), valid_icon.getPixels(), |
| + default_icon.getSize())); |
| + // Verify we did get the expected favicon. |
| + EXPECT_EQ(0, memcmp(new_icon.getPixels(), valid_icon.getPixels(), |
| + new_icon.getSize())); |
| + |
| + // Make sure the browser deconstructor doesn't have problems. |
| + browser.CloseAllTabs(); |
| + // This is required to prevent the message loop from hanging. |
| + profile()->DestroyHistoryService(); |
| +} |
| + |