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" | |
| 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) { | |
|
sky
2011/02/22 18:28:28
This shouldn't be indented.
| |
| 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::MenuModel::Delegate { | |
| 37 public: | |
| 38 explicit 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() { return was_called_; } | |
| 46 | |
| 47 private: | |
| 48 bool was_called_; | |
| 49 DISALLOW_COPY_AND_ASSIGN(FaviconDelegate); | |
| 50 }; | |
| 51 | |
| 52 } // namespace | |
| 18 | 53 |
| 19 class BackFwdMenuModelTest : public RenderViewHostTestHarness { | 54 class BackFwdMenuModelTest : public RenderViewHostTestHarness { |
| 20 public: | 55 public: |
| 21 void ValidateModel(BackForwardMenuModel* model, int history_items, | 56 void ValidateModel(BackForwardMenuModel* model, int history_items, |
| 22 int chapter_stops) { | 57 int chapter_stops) { |
| 23 int h = std::min(BackForwardMenuModel::kMaxHistoryItems, history_items); | 58 int h = std::min(BackForwardMenuModel::kMaxHistoryItems, history_items); |
| 24 int c = std::min(BackForwardMenuModel::kMaxChapterStops, chapter_stops); | 59 int c = std::min(BackForwardMenuModel::kMaxChapterStops, chapter_stops); |
| 25 EXPECT_EQ(h, model->GetHistoryItemCount()); | 60 EXPECT_EQ(h, model->GetHistoryItemCount()); |
| 26 EXPECT_EQ(c, model->GetChapterStopCount(h)); | 61 EXPECT_EQ(c, model->GetChapterStopCount(h)); |
| 27 if (h > 0) | 62 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)); | 448 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(0, true)); |
| 414 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(1, true)); | 449 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(1, true)); |
| 415 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(2, true)); | 450 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(2, true)); |
| 416 EXPECT_EQ(4, back_model->GetIndexOfNextChapterStop(3, true)); | 451 EXPECT_EQ(4, back_model->GetIndexOfNextChapterStop(3, true)); |
| 417 // And try backwards as well. | 452 // And try backwards as well. |
| 418 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(4, false)); | 453 EXPECT_EQ(3, back_model->GetIndexOfNextChapterStop(4, false)); |
| 419 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(3, false)); | 454 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(3, false)); |
| 420 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(2, false)); | 455 EXPECT_EQ(1, back_model->GetIndexOfNextChapterStop(2, false)); |
| 421 EXPECT_EQ(-1, back_model->GetIndexOfNextChapterStop(1, false)); | 456 EXPECT_EQ(-1, back_model->GetIndexOfNextChapterStop(1, false)); |
| 422 } | 457 } |
| 458 | |
| 459 // Test asynchronous loading of favicon from history service. | |
| 460 TEST_F(BackFwdMenuModelTest, FaviconLoadTest) { | |
| 461 profile()->CreateHistoryService(true, false); | |
| 462 profile()->CreateFaviconService(); | |
| 463 Browser browser(Browser::TYPE_NORMAL, profile()); | |
| 464 FaviconDelegate favicon_delegate; | |
| 465 | |
| 466 scoped_ptr<BackForwardMenuModel> back_model(new BackForwardMenuModel( | |
|
sky
2011/02/22 18:28:28
How come you put this in a scoped_ptr instead of o
dill
2011/02/24 15:00:28
Leftover from experimentation, back to stack.
On
| |
| 467 &browser, BackForwardMenuModel::BACKWARD_MENU)); | |
| 468 back_model->set_test_tab_contents(controller().tab_contents()); | |
| 469 back_model->SetDelegate(&favicon_delegate); | |
| 470 | |
| 471 SkBitmap new_icon(CreateBitmap(SK_ColorRED)); | |
| 472 std::vector<unsigned char> icon_data; | |
| 473 gfx::PNGCodec::EncodeBGRASkBitmap(new_icon, false, &icon_data); | |
| 474 | |
| 475 GURL url1 = GURL("http://www.a.com/1"); | |
| 476 GURL url2 = GURL("http://www.a.com/2"); | |
| 477 GURL url1_favicon("http://www.a.com/1/favicon.ico"); | |
| 478 | |
| 479 NavigateAndCommit(url1); | |
| 480 // Navigate to a new URL so that url1 will be in the BackForwardMenuModel. | |
| 481 NavigateAndCommit(url2); | |
| 482 | |
| 483 // Set the desired favicon for url1. | |
| 484 profile()->GetHistoryService(Profile::EXPLICIT_ACCESS)->AddPage(url1, | |
| 485 history::SOURCE_BROWSED); | |
| 486 profile()->GetFaviconService(Profile::EXPLICIT_ACCESS)->SetFavicon(url1, | |
| 487 url1_favicon, icon_data); | |
| 488 | |
| 489 // Will return the current icon (default) but start an anync call | |
| 490 // to retrieve the favicon from the favicon service. | |
| 491 SkBitmap default_icon; | |
| 492 back_model->GetIconAt(0, &default_icon); | |
| 493 | |
| 494 // Make the favicon service run GetFavIconForURL, | |
| 495 // FaviconDelegate.OnIconChanged will be called. | |
| 496 MessageLoop::current()->Run(); | |
| 497 | |
| 498 // Verify that the callback executed. | |
| 499 EXPECT_TRUE(favicon_delegate.was_called()); | |
| 500 | |
| 501 // Verify the bitmaps match. | |
| 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 | |
| 515 // Make sure the browser deconstructor doesn't have problems. | |
| 516 browser.CloseAllTabs(); | |
| 517 // This is required to prevent the message loop from hanging. | |
| 518 profile()->DestroyHistoryService(); | |
| 519 } | |
| 520 | |
| OLD | NEW |