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 "content/browser/browser_thread.h" | |
sky
2011/03/18 17:54:11
Should be around line 16.
| |
11 #include "chrome/browser/history/history.h" | |
10 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
13 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
15 #include "chrome/test/testing_profile.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 explicit FaviconDelegate() : was_called_(false) {} | |
sky
2011/03/18 17:54:11
no explicit
| |
39 | |
40 void OnIconChanged(int model_index) { | |
41 was_called_ = true; | |
42 MessageLoop::current()->Quit(); | |
43 } | |
44 | |
45 bool was_called() { return was_called_; } | |
sky
2011/03/18 17:54:11
const
| |
46 | |
47 private: | |
48 bool was_called_; | |
49 DISALLOW_COPY_AND_ASSIGN(FaviconDelegate); | |
sky
2011/03/18 17:54:11
newline between 48 and 49.
| |
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 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 EXPECT_EQ(ASCIIToUTF16("A & B"), back_model->GetLabelAt(2)); | 478 EXPECT_EQ(ASCIIToUTF16("A & B"), back_model->GetLabelAt(2)); |
444 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(1)); | 479 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(1)); |
445 EXPECT_EQ(ASCIIToUTF16("A &&& B"), back_model->GetLabelAt(0)); | 480 EXPECT_EQ(ASCIIToUTF16("A &&& B"), back_model->GetLabelAt(0)); |
446 #else | 481 #else |
447 EXPECT_EQ(ASCIIToUTF16("A B"), back_model->GetLabelAt(3)); | 482 EXPECT_EQ(ASCIIToUTF16("A B"), back_model->GetLabelAt(3)); |
448 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(2)); | 483 EXPECT_EQ(ASCIIToUTF16("A && B"), back_model->GetLabelAt(2)); |
449 EXPECT_EQ(ASCIIToUTF16("A &&&& B"), back_model->GetLabelAt(1)); | 484 EXPECT_EQ(ASCIIToUTF16("A &&&& B"), back_model->GetLabelAt(1)); |
450 EXPECT_EQ(ASCIIToUTF16("A &&&&&& B"), back_model->GetLabelAt(0)); | 485 EXPECT_EQ(ASCIIToUTF16("A &&&&&& B"), back_model->GetLabelAt(0)); |
451 #endif // defined(OS_MACOSX) | 486 #endif // defined(OS_MACOSX) |
452 } | 487 } |
488 | |
489 // Test asynchronous loading of favicon from history service. | |
490 TEST_F(BackFwdMenuModelTest, FaviconLoadTest) { | |
491 profile()->CreateHistoryService(true, false); | |
492 profile()->CreateFaviconService(); | |
493 Browser browser(Browser::TYPE_NORMAL, profile()); | |
494 FaviconDelegate favicon_delegate; | |
495 | |
496 BackForwardMenuModel back_model( | |
497 &browser, BackForwardMenuModel::BACKWARD_MENU); | |
sky
2011/03/18 17:54:11
indented 4 spaces.
| |
498 back_model.set_test_tab_contents(controller().tab_contents()); | |
499 back_model.SetMenuModelDelegate(&favicon_delegate); | |
500 | |
501 SkBitmap new_icon(CreateBitmap(SK_ColorRED)); | |
502 std::vector<unsigned char> icon_data; | |
503 gfx::PNGCodec::EncodeBGRASkBitmap(new_icon, false, &icon_data); | |
504 | |
505 GURL url1 = GURL("http://www.a.com/1"); | |
506 GURL url2 = GURL("http://www.a.com/2"); | |
507 GURL url1_favicon("http://www.a.com/1/favicon.ico"); | |
508 | |
509 NavigateAndCommit(url1); | |
510 // Navigate to a new URL so that url1 will be in the BackForwardMenuModel. | |
511 NavigateAndCommit(url2); | |
512 | |
513 // Set the desired favicon for url1. | |
514 profile()->GetHistoryService(Profile::EXPLICIT_ACCESS)->AddPage(url1, | |
515 history::SOURCE_BROWSED); | |
516 profile()->GetFaviconService(Profile::EXPLICIT_ACCESS)->SetFavicon(url1, | |
517 url1_favicon, icon_data, history::FAVICON); | |
sky
2011/03/18 17:54:11
indented 4.
| |
518 | |
519 // Will return the current icon (default) but start an anync call | |
520 // to retrieve the favicon from the favicon service. | |
521 SkBitmap default_icon; | |
522 back_model.GetIconAt(0, &default_icon); | |
523 | |
524 // Make the favicon service run GetFavIconForURL, | |
525 // FaviconDelegate.OnIconChanged will be called. | |
526 MessageLoop::current()->Run(); | |
527 | |
528 // Verify that the callback executed. | |
529 EXPECT_TRUE(favicon_delegate.was_called()); | |
530 | |
531 // Verify the bitmaps match. | |
532 SkBitmap valid_icon; | |
533 // This time we will get the new favicon returned. | |
534 back_model.GetIconAt(0, &valid_icon); | |
535 SkAutoLockPixels a(new_icon); | |
536 SkAutoLockPixels b(valid_icon); | |
537 SkAutoLockPixels c(default_icon); | |
538 // Verify we did not get the default favicon. | |
539 EXPECT_NE(0, memcmp(default_icon.getPixels(), valid_icon.getPixels(), | |
540 default_icon.getSize())); | |
541 // Verify we did get the expected favicon. | |
542 EXPECT_EQ(0, memcmp(new_icon.getPixels(), valid_icon.getPixels(), | |
543 new_icon.getSize())); | |
544 | |
545 // Make sure the browser deconstructor doesn't have problems. | |
546 browser.CloseAllTabs(); | |
547 // This is required to prevent the message loop from hanging. | |
548 profile()->DestroyHistoryService(); | |
549 } | |
550 | |
OLD | NEW |