Index: chrome/browser/ui/toolbar/back_forward_menu_model_favicon_unittest.cc |
=================================================================== |
--- chrome/browser/ui/toolbar/back_forward_menu_model_favicon_unittest.cc (revision 0) |
+++ chrome/browser/ui/toolbar/back_forward_menu_model_favicon_unittest.cc (revision 0) |
@@ -0,0 +1,124 @@ |
+// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/toolbar/back_forward_menu_model.h" |
+ |
+#include "base/message_loop.h" |
+#include "base/path_service.h" |
+#include "base/string_util.h" |
+#include "base/utf_string_conversions.h" |
+#include "chrome/browser/browser_thread.h" |
+#include "chrome/browser/history/history.h" |
+#include "chrome/browser/profiles/profile_manager.h" |
+#include "chrome/browser/renderer_host/site_instance.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/tab_contents/navigation_controller.h" |
+#include "chrome/browser/tab_contents/navigation_entry.h" |
+#include "chrome/browser/tab_contents/tab_contents.h" |
+#include "chrome/browser/tab_contents/test_tab_contents.h" |
+#include "chrome/common/url_constants.h" |
+#include "chrome/test/browser_with_test_window_test.h" |
+#include "chrome/test/testing_profile.h" |
+#include "gfx/codec/png_codec.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+ |
+namespace { |
+ |
+static const int kIconWidth = 16; |
+static const int kIconHeight = 16; |
+ |
+void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) { |
+ bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
+ bmp->allocPixels(); |
+ |
+ uint32_t* src_data = bmp->getAddr32(0, 0); |
+ for (int i = 0; i < w * h; i++) { |
+ src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240); |
+ } |
+} |
+ |
+} // namespace |
+ |
+class FaviconDelegate : public menus::MenuModel::Delegate { |
+ public: |
+ explicit FaviconDelegate(MessageLoop* loop) : loop_(loop) { |
+ DCHECK(loop); |
+ } |
+ |
+ void OnIconChanged(int model_index) { |
+ loop_->Quit(); |
+ } |
+ |
+ private: |
+ MessageLoop* loop_; |
+ DISALLOW_COPY_AND_ASSIGN(FaviconDelegate); |
+}; |
+ |
+class BackFwdMenuModelFaviconTest : public BrowserWithTestWindowTest { |
+}; |
+ |
+TEST_F(BackFwdMenuModelFaviconTest, FaviconLoadTest) { |
+ profile()->CreateHistoryService(true, false); |
+ profile()->CreateFaviconService(); |
+ |
+ 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"); |
+ |
+ AddTab(browser(), url1); |
+ |
+ NavigationController& controller = |
+ browser()->GetTabContentsAt(0)->controller(); |
+ |
+ FaviconDelegate favicon_delegate(MessageLoop::current()); |
+ |
+ scoped_ptr<BackForwardMenuModel> back_model(new BackForwardMenuModel( |
+ browser(), BackForwardMenuModel::BACKWARD_MENU)); |
+ back_model->set_test_tab_contents(controller.tab_contents()); |
+ back_model->SetDelegate(&favicon_delegate); |
+ |
+ scoped_ptr<BackForwardMenuModel> forward_model(new BackForwardMenuModel( |
+ browser(), BackForwardMenuModel::FORWARD_MENU)); |
+ forward_model->set_test_tab_contents(controller.tab_contents()); |
+ forward_model->SetDelegate(&favicon_delegate); |
+ |
+ SkBitmap new_icon; |
+ MakeTestSkBitmap(kIconWidth, kIconHeight, &new_icon); |
+ std::vector<unsigned char> icon_data; |
+ gfx::PNGCodec::EncodeBGRASkBitmap(new_icon, false, &icon_data); |
+ |
+ // NavigateAndCommitActiveTab(url1); |
+ // Navigate to a new URL so that url1 will be in the BackForwardMenuModel. |
+ NavigateAndCommitActiveTab(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); |
+ MessageLoop::current()->RunAllPending(); |
+ |
+ // 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(); |
+ |
+ 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())); |
+} |