Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h" | 5 #include "chrome/browser/ui/toolbar/back_forward_menu_model.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_thread.h" | |
| 11 #include "chrome/browser/history/history.h" | |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | 13 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 12 #include "chrome/browser/tab_contents/navigation_controller.h" | 14 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 13 #include "chrome/browser/tab_contents/navigation_entry.h" | 15 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" | 16 #include "chrome/browser/tab_contents/tab_contents.h" |
| 15 #include "chrome/browser/tab_contents/test_tab_contents.h" | 17 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 18 #include "chrome/browser/ui/browser.h" | |
| 16 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 20 #include "chrome/test/testing_profile.h" | |
| 21 #include "gfx/codec/png_codec.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "third_party/skia/include/core/SkBitmap.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 static const int kIconWidth = 16; | |
| 28 static const int kIconHeight = 16; | |
| 29 | |
| 30 void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) { | |
|
sky
2011/01/06 17:21:56
See the code in top_sites_unittest CreateBitmap th
| |
| 31 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); | |
| 32 bmp->allocPixels(); | |
| 33 | |
| 34 uint32_t* src_data = bmp->getAddr32(0, 0); | |
| 35 for (int i = 0; i < w * h; i++) { | |
| 36 src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 class FaviconDelegate : public menus::MenuModel::Delegate { | |
|
sky
2011/01/06 17:21:56
Keep this in the anonymous namespace.
| |
| 43 public: | |
| 44 explicit FaviconDelegate(MessageLoop* loop) : loop_(loop) { | |
|
sky
2011/01/06 17:21:56
Does this really need to cache the loop? Can't it
| |
| 45 DCHECK(loop); | |
| 46 } | |
| 47 | |
| 48 void OnIconChanged(int model_index) { | |
| 49 loop_->Quit(); | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 MessageLoop* loop_; | |
| 54 DISALLOW_COPY_AND_ASSIGN(FaviconDelegate); | |
| 55 }; | |
| 18 | 56 |
| 19 class BackFwdMenuModelTest : public RenderViewHostTestHarness { | 57 class BackFwdMenuModelTest : public RenderViewHostTestHarness { |
| 20 public: | 58 public: |
| 21 void ValidateModel(BackForwardMenuModel* model, int history_items, | 59 void ValidateModel(BackForwardMenuModel* model, int history_items, |
| 22 int chapter_stops) { | 60 int chapter_stops) { |
| 23 int h = std::min(BackForwardMenuModel::kMaxHistoryItems, history_items); | 61 int h = std::min(BackForwardMenuModel::kMaxHistoryItems, history_items); |
| 24 int c = std::min(BackForwardMenuModel::kMaxChapterStops, chapter_stops); | 62 int c = std::min(BackForwardMenuModel::kMaxChapterStops, chapter_stops); |
| 25 EXPECT_EQ(h, model->GetHistoryItemCount()); | 63 EXPECT_EQ(h, model->GetHistoryItemCount()); |
| 26 EXPECT_EQ(c, model->GetChapterStopCount(h)); | 64 EXPECT_EQ(c, model->GetChapterStopCount(h)); |
| 27 if (h > 0) | 65 if (h > 0) |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 413 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(0, true)); | 451 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(0, true)); |
| 414 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(1, true)); | 452 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(1, true)); |
| 415 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(2, true)); | 453 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(2, true)); |
| 416 EXPECT_EQ(4, back_model->GetIndexOfNextChapterStop(3, true)); | 454 EXPECT_EQ(4, back_model->GetIndexOfNextChapterStop(3, true)); |
| 417 // And try backwards as well. | 455 // And try backwards as well. |
| 418 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(4, false)); | 456 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(4, false)); |
| 419 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(3, false)); | 457 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(3, false)); |
| 420 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(2, false)); | 458 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(2, false)); |
| 421 EXPECT_EQ(-1, back_model->GetIndexOfNextChapterStop(1, false)); | 459 EXPECT_EQ(-1, back_model->GetIndexOfNextChapterStop(1, false)); |
| 422 } | 460 } |
| 461 | |
| 462 TEST_F(BackFwdMenuModelTest, FaviconLoadTest) { | |
|
sky
2011/01/06 17:21:56
Description?
| |
| 463 profile()->CreateHistoryService(true, false); | |
| 464 profile()->CreateFaviconService(); | |
| 465 Browser browser_(Browser::TYPE_NORMAL, profile()); | |
|
sky
2011/01/06 17:21:56
browser_ -> browser
| |
| 466 FaviconDelegate favicon_delegate(MessageLoop::current()); | |
| 467 | |
| 468 scoped_ptr<BackForwardMenuModel> back_model(new BackForwardMenuModel( | |
| 469 &browser_, BackForwardMenuModel::BACKWARD_MENU)); | |
| 470 back_model->set_test_tab_contents(controller().tab_contents()); | |
| 471 back_model->SetDelegate(&favicon_delegate); | |
| 472 | |
| 473 SkBitmap new_icon; | |
| 474 MakeTestSkBitmap(kIconWidth, kIconHeight, &new_icon); | |
| 475 std::vector<unsigned char> icon_data; | |
| 476 gfx::PNGCodec::EncodeBGRASkBitmap(new_icon, false, &icon_data); | |
| 477 | |
| 478 GURL url1 = GURL("http://www.a.com/1"); | |
| 479 GURL url2 = GURL("http://www.a.com/2"); | |
| 480 GURL url1_favicon("http://www.a.com/1/favicon.ico"); | |
| 481 | |
| 482 NavigateAndCommit(url1); | |
| 483 // Navigate to a new URL so that url1 will be in the BackForwardMenuModel. | |
| 484 NavigateAndCommit(url2); | |
| 485 | |
| 486 // Set the desired favicon for url1. | |
| 487 profile()->GetHistoryService(Profile::EXPLICIT_ACCESS)->AddPage(url1, | |
| 488 history::SOURCE_BROWSED); | |
| 489 profile()->GetFaviconService(Profile::EXPLICIT_ACCESS)->SetFavicon(url1, | |
| 490 url1_favicon, icon_data); | |
| 491 MessageLoop::current()->RunAllPending(); | |
|
sky
2011/01/06 17:21:56
Why the RunAllPending here?
| |
| 492 | |
| 493 // Will return the current icon (default) but start an anync call | |
| 494 // to retrieve the favicon from the favicon service. | |
| 495 SkBitmap default_icon; | |
| 496 back_model->GetIconAt(0, &default_icon); | |
| 497 | |
| 498 // Make the favicon service run GetFavIconForURL, | |
| 499 // FaviconDelegate.OnIconChanged will be called. | |
| 500 MessageLoop::current()->Run(); | |
|
sky
2011/01/06 17:21:56
Shouldn't have an EXPECT that the delegate was cal
| |
| 501 | |
| 502 SkBitmap valid_icon; | |
| 503 // This time we will get the new favicon returned. | |
| 504 back_model->GetIconAt(0, &valid_icon); | |
| 505 SkAutoLockPixels a(new_icon); | |
| 506 SkAutoLockPixels b(valid_icon); | |
| 507 SkAutoLockPixels c(default_icon); | |
| 508 // Verify we did not get the default favicon. | |
| 509 EXPECT_NE(0, memcmp(default_icon.getPixels(), valid_icon.getPixels(), | |
| 510 default_icon.getSize())); | |
| 511 // Verify we did get the expected favicon. | |
| 512 EXPECT_EQ(0, memcmp(new_icon.getPixels(), valid_icon.getPixels(), | |
| 513 new_icon.getSize())); | |
| 514 browser_.CloseAllTabs(); | |
| 515 // This is required to prevent the message loop from hanging. | |
| 516 profile()->DestroyHistoryService(); | |
| 517 } | |
| 518 | |
| OLD | NEW |