| 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/history/history.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 14 #include "chrome/test/testing_profile.h" |
| 15 #include "content/browser/browser_thread.h" |
| 12 #include "content/browser/renderer_host/test_render_view_host.h" | 16 #include "content/browser/renderer_host/test_render_view_host.h" |
| 13 #include "content/browser/tab_contents/navigation_controller.h" | 17 #include "content/browser/tab_contents/navigation_controller.h" |
| 14 #include "content/browser/tab_contents/navigation_entry.h" | 18 #include "content/browser/tab_contents/navigation_entry.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" | 19 #include "content/browser/tab_contents/tab_contents.h" |
| 16 #include "content/browser/tab_contents/test_tab_contents.h" | 20 #include "content/browser/tab_contents/test_tab_contents.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/skia/include/core/SkBitmap.h" |
| 23 #include "ui/gfx/codec/png_codec.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 // Creates a bitmap of the specified color. |
| 28 SkBitmap CreateBitmap(SkColor color) { |
| 29 SkBitmap bitmap; |
| 30 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
| 31 bitmap.allocPixels(); |
| 32 bitmap.eraseColor(color); |
| 33 return bitmap; |
| 34 } |
| 35 |
| 36 class FaviconDelegate : public ui::MenuModelDelegate { |
| 37 public: |
| 38 FaviconDelegate() : was_called_(false) {} |
| 39 |
| 40 void OnIconChanged(int model_index) { |
| 41 was_called_ = true; |
| 42 MessageLoop::current()->Quit(); |
| 43 } |
| 44 |
| 45 bool was_called() const { return was_called_; } |
| 46 |
| 47 private: |
| 48 bool was_called_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(FaviconDelegate); |
| 51 }; |
| 52 |
| 53 } // namespace |
| 18 | 54 |
| 19 class BackFwdMenuModelTest : public RenderViewHostTestHarness { | 55 class BackFwdMenuModelTest : public RenderViewHostTestHarness { |
| 20 public: | 56 public: |
| 21 void ValidateModel(BackForwardMenuModel* model, int history_items, | 57 void ValidateModel(BackForwardMenuModel* model, int history_items, |
| 22 int chapter_stops) { | 58 int chapter_stops) { |
| 23 int h = std::min(BackForwardMenuModel::kMaxHistoryItems, history_items); | 59 int h = std::min(BackForwardMenuModel::kMaxHistoryItems, history_items); |
| 24 int c = std::min(BackForwardMenuModel::kMaxChapterStops, chapter_stops); | 60 int c = std::min(BackForwardMenuModel::kMaxChapterStops, chapter_stops); |
| 25 EXPECT_EQ(h, model->GetHistoryItemCount()); | 61 EXPECT_EQ(h, model->GetHistoryItemCount()); |
| 26 EXPECT_EQ(c, model->GetChapterStopCount(h)); | 62 EXPECT_EQ(c, model->GetChapterStopCount(h)); |
| 27 if (h > 0) | 63 if (h > 0) |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 EXPECT_EQ(ASCIIToUTF16("A & B"), back_model->GetLabelAt(2)); | 479 EXPECT_EQ(ASCIIToUTF16("A & B"), back_model->GetLabelAt(2)); |
| 444 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(1)); | 480 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(1)); |
| 445 EXPECT_EQ(ASCIIToUTF16("A &&& B"), back_model->GetLabelAt(0)); | 481 EXPECT_EQ(ASCIIToUTF16("A &&& B"), back_model->GetLabelAt(0)); |
| 446 #else | 482 #else |
| 447 EXPECT_EQ(ASCIIToUTF16("A B"), back_model->GetLabelAt(3)); | 483 EXPECT_EQ(ASCIIToUTF16("A B"), back_model->GetLabelAt(3)); |
| 448 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(2)); | 484 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(2)); |
| 449 EXPECT_EQ(ASCIIToUTF16("A &&&& B"), back_model->GetLabelAt(1)); | 485 EXPECT_EQ(ASCIIToUTF16("A &&&& B"), back_model->GetLabelAt(1)); |
| 450 EXPECT_EQ(ASCIIToUTF16("A &&&&&& B"), back_model->GetLabelAt(0)); | 486 EXPECT_EQ(ASCIIToUTF16("A &&&&&& B"), back_model->GetLabelAt(0)); |
| 451 #endif // defined(OS_MACOSX) | 487 #endif // defined(OS_MACOSX) |
| 452 } | 488 } |
| 489 |
| 490 // Test asynchronous loading of favicon from history service. |
| 491 TEST_F(BackFwdMenuModelTest, FaviconLoadTest) { |
| 492 profile()->CreateHistoryService(true, false); |
| 493 profile()->CreateFaviconService(); |
| 494 Browser browser(Browser::TYPE_NORMAL, profile()); |
| 495 FaviconDelegate favicon_delegate; |
| 496 |
| 497 BackForwardMenuModel back_model( |
| 498 &browser, BackForwardMenuModel::BACKWARD_MENU); |
| 499 back_model.set_test_tab_contents(controller().tab_contents()); |
| 500 back_model.SetMenuModelDelegate(&favicon_delegate); |
| 501 |
| 502 SkBitmap new_icon(CreateBitmap(SK_ColorRED)); |
| 503 std::vector<unsigned char> icon_data; |
| 504 gfx::PNGCodec::EncodeBGRASkBitmap(new_icon, false, &icon_data); |
| 505 |
| 506 GURL url1 = GURL("http://www.a.com/1"); |
| 507 GURL url2 = GURL("http://www.a.com/2"); |
| 508 GURL url1_favicon("http://www.a.com/1/favicon.ico"); |
| 509 |
| 510 NavigateAndCommit(url1); |
| 511 // Navigate to a new URL so that url1 will be in the BackForwardMenuModel. |
| 512 NavigateAndCommit(url2); |
| 513 |
| 514 // Set the desired favicon for url1. |
| 515 profile()->GetHistoryService(Profile::EXPLICIT_ACCESS)->AddPage(url1, |
| 516 history::SOURCE_BROWSED); |
| 517 profile()->GetFaviconService(Profile::EXPLICIT_ACCESS)->SetFavicon(url1, |
| 518 url1_favicon, icon_data, history::FAVICON); |
| 519 |
| 520 // Will return the current icon (default) but start an anync call |
| 521 // to retrieve the favicon from the favicon service. |
| 522 SkBitmap default_icon; |
| 523 back_model.GetIconAt(0, &default_icon); |
| 524 |
| 525 // Make the favicon service run GetFavIconForURL, |
| 526 // FaviconDelegate.OnIconChanged will be called. |
| 527 MessageLoop::current()->Run(); |
| 528 |
| 529 // Verify that the callback executed. |
| 530 EXPECT_TRUE(favicon_delegate.was_called()); |
| 531 |
| 532 // Verify the bitmaps match. |
| 533 SkBitmap valid_icon; |
| 534 // This time we will get the new favicon returned. |
| 535 back_model.GetIconAt(0, &valid_icon); |
| 536 SkAutoLockPixels a(new_icon); |
| 537 SkAutoLockPixels b(valid_icon); |
| 538 SkAutoLockPixels c(default_icon); |
| 539 // Verify we did not get the default favicon. |
| 540 EXPECT_NE(0, memcmp(default_icon.getPixels(), valid_icon.getPixels(), |
| 541 default_icon.getSize())); |
| 542 // Verify we did get the expected favicon. |
| 543 EXPECT_EQ(0, memcmp(new_icon.getPixels(), valid_icon.getPixels(), |
| 544 new_icon.getSize())); |
| 545 |
| 546 // Make sure the browser deconstructor doesn't have problems. |
| 547 browser.CloseAllTabs(); |
| 548 // This is required to prevent the message loop from hanging. |
| 549 profile()->DestroyHistoryService(); |
| 550 } |
| 551 |
| OLD | NEW |