| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "chrome/browser/favicon/favicon_handler.h" | 6 #include "chrome/browser/favicon/favicon_handler.h" |
| 7 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 8 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 9 #include "content/public/browser/invalidate_type.h" | 9 #include "content/public/browser/invalidate_type.h" |
| 10 #include "content/public/browser/navigation_entry.h" | 10 #include "content/public/browser/navigation_entry.h" |
| (...skipping 805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 816 | 816 |
| 817 // No download request. | 817 // No download request. |
| 818 EXPECT_FALSE(download_handler->HasDownload()); | 818 EXPECT_FALSE(download_handler->HasDownload()); |
| 819 } | 819 } |
| 820 | 820 |
| 821 TEST_F(FaviconHandlerTest, MultipleFavicon) { | 821 TEST_F(FaviconHandlerTest, MultipleFavicon) { |
| 822 const GURL page_url("http://www.google.com"); | 822 const GURL page_url("http://www.google.com"); |
| 823 const GURL icon_url("http://www.google.com/favicon"); | 823 const GURL icon_url("http://www.google.com/favicon"); |
| 824 const GURL icon_url_small("http://www.google.com/favicon_small"); | 824 const GURL icon_url_small("http://www.google.com/favicon_small"); |
| 825 const GURL icon_url_large("http://www.google.com/favicon_large"); | 825 const GURL icon_url_large("http://www.google.com/favicon_large"); |
| 826 const GURL icon_url_preferred("http://www.google.com/favicon_preferred"); | 826 const GURL icon_url_preferred1("http://www.google.com/favicon_preferred1"); |
| 827 const GURL icon_url_preferred2("http://www.google.com/favicon_preferred2"); |
| 827 | 828 |
| 828 TestFaviconHandlerDelegate delegate(contents()); | 829 TestFaviconHandlerDelegate delegate(contents()); |
| 829 Profile* profile = Profile::FromBrowserContext( | 830 Profile* profile = Profile::FromBrowserContext( |
| 830 contents()->GetBrowserContext()); | 831 contents()->GetBrowserContext()); |
| 831 TestFaviconHandler helper(page_url, profile, | 832 TestFaviconHandler handler(page_url, profile, |
| 832 &delegate, FaviconHandler::FAVICON); | 833 &delegate, FaviconHandler::FAVICON); |
| 833 | 834 |
| 834 helper.FetchFavicon(page_url); | 835 handler.FetchFavicon(page_url); |
| 835 HistoryRequestHandler* history_handler = helper.history_handler(); | 836 HistoryRequestHandler* history_handler = handler.history_handler(); |
| 836 | 837 |
| 837 // Set valid icon data. | 838 // Set valid icon data. |
| 838 history_handler->favicon_data_.known_icon = true; | 839 history_handler->favicon_data_.known_icon = true; |
| 839 history_handler->favicon_data_.icon_type = history::FAVICON; | 840 history_handler->favicon_data_.icon_type = history::FAVICON; |
| 840 history_handler->favicon_data_.expired = false; | 841 history_handler->favicon_data_.expired = false; |
| 841 history_handler->favicon_data_.icon_url = icon_url; | 842 history_handler->favicon_data_.icon_url = icon_url; |
| 842 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); | 843 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); |
| 843 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); | 844 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); |
| 844 history_handler->favicon_data_.image_data = data; | 845 history_handler->favicon_data_.image_data = data; |
| 845 | 846 |
| 846 // Send history response. | 847 // Send history response. |
| 847 history_handler->InvokeCallback(); | 848 history_handler->InvokeCallback(); |
| 848 | 849 |
| 849 // Simulates update with the different favicon url. | 850 // Simulates update with the different favicon url. |
| 850 std::vector<FaviconURL> urls; | 851 std::vector<FaviconURL> urls; |
| 851 // Note: the code will stop making requests when an icon matching the | 852 // Note: the code will stop making requests when an icon matching the |
| 852 // preferred size is found, so icon_url_preferred must be last. | 853 // preferred size is found, so icon_url_preferred must be last. |
| 853 urls.push_back(FaviconURL(icon_url_small, FaviconURL::FAVICON)); | 854 urls.push_back(FaviconURL(icon_url_small, FaviconURL::FAVICON)); |
| 854 urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON)); | 855 urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON)); |
| 855 urls.push_back(FaviconURL(icon_url_preferred, FaviconURL::FAVICON)); | 856 urls.push_back(FaviconURL(icon_url_preferred1, FaviconURL::FAVICON)); |
| 856 helper.OnUpdateFaviconURL(0, urls); | 857 urls.push_back(FaviconURL(icon_url_preferred2, FaviconURL::FAVICON)); |
| 858 handler.OnUpdateFaviconURL(0, urls); |
| 859 EXPECT_EQ(4U, handler.image_urls().size()); |
| 857 | 860 |
| 858 DownloadHandler* download_handler = helper.download_handler(); | 861 DownloadHandler* download_handler = handler.download_handler(); |
| 859 | 862 |
| 860 // Download the first icon (set not in history). | 863 // Download the first icon (set not in history). |
| 861 helper.history_handler()->favicon_data_.known_icon = false; | 864 handler.history_handler()->favicon_data_.known_icon = false; |
| 862 helper.history_handler()->InvokeCallback(); | 865 handler.history_handler()->InvokeCallback(); |
| 863 ASSERT_TRUE(download_handler->HasDownload()); | 866 ASSERT_TRUE(download_handler->HasDownload()); |
| 864 EXPECT_EQ(icon_url_small, download_handler->GetImageUrl()); | 867 EXPECT_EQ(icon_url_small, download_handler->GetImageUrl()); |
| 865 download_handler->SetImageSize(gfx::kFaviconSize / 2); | 868 download_handler->SetImageSize(gfx::kFaviconSize / 2); |
| 866 download_handler->InvokeCallback(); | 869 download_handler->InvokeCallback(); |
| 870 EXPECT_EQ(3U, handler.image_urls().size()); |
| 867 | 871 |
| 868 // Download the second icon (set not in history). | 872 // Download the second icon (set not in history). |
| 869 helper.history_handler()->favicon_data_.known_icon = false; | 873 handler.history_handler()->favicon_data_.known_icon = false; |
| 870 helper.history_handler()->InvokeCallback(); | 874 handler.history_handler()->InvokeCallback(); |
| 871 ASSERT_TRUE(download_handler->HasDownload()); | 875 ASSERT_TRUE(download_handler->HasDownload()); |
| 872 EXPECT_EQ(icon_url_large, download_handler->GetImageUrl()); | 876 EXPECT_EQ(icon_url_large, download_handler->GetImageUrl()); |
| 873 download_handler->SetImageSize(gfx::kFaviconSize * 2); | 877 download_handler->SetImageSize(gfx::kFaviconSize * 2); |
| 874 download_handler->InvokeCallback(); | 878 download_handler->InvokeCallback(); |
| 879 EXPECT_EQ(2U, handler.image_urls().size()); |
| 875 | 880 |
| 876 // Download the third icon (set not in history). | 881 // Download the third icon (set not in history). |
| 877 helper.history_handler()->favicon_data_.known_icon = false; | 882 handler.history_handler()->favicon_data_.known_icon = false; |
| 878 helper.history_handler()->InvokeCallback(); | 883 handler.history_handler()->InvokeCallback(); |
| 879 ASSERT_TRUE(download_handler->HasDownload()); | 884 ASSERT_TRUE(download_handler->HasDownload()); |
| 880 EXPECT_EQ(icon_url_preferred, download_handler->GetImageUrl()); | 885 EXPECT_EQ(icon_url_preferred1, download_handler->GetImageUrl()); |
| 881 download_handler->SetImageSize(gfx::kFaviconSize); | 886 download_handler->SetImageSize(gfx::kFaviconSize); |
| 882 download_handler->InvokeCallback(); | 887 download_handler->InvokeCallback(); |
| 888 // Verify that this was detected as an exact match and image_urls_ is cleared. |
| 889 EXPECT_EQ(0U, handler.image_urls().size()); |
| 883 | 890 |
| 884 // Verify correct icon size chosen. | 891 // Verify correct icon size chosen. |
| 885 EXPECT_EQ(icon_url_preferred, helper.GetEntry()->GetFavicon().url); | 892 EXPECT_EQ(icon_url_preferred1, handler.GetEntry()->GetFavicon().url); |
| 886 EXPECT_TRUE(helper.GetEntry()->GetFavicon().valid); | 893 EXPECT_TRUE(handler.GetEntry()->GetFavicon().valid); |
| 887 EXPECT_FALSE(helper.GetEntry()->GetFavicon().bitmap.empty()); | 894 EXPECT_FALSE(handler.GetEntry()->GetFavicon().bitmap.empty()); |
| 888 EXPECT_EQ(gfx::kFaviconSize, helper.GetEntry()->GetFavicon().bitmap.width()); | 895 EXPECT_EQ(gfx::kFaviconSize, handler.GetEntry()->GetFavicon().bitmap.width()); |
| 896 } |
| 897 |
| 898 TEST_F(FaviconHandlerTest, FirstFavicon) { |
| 899 const GURL page_url("http://www.google.com"); |
| 900 const GURL icon_url("http://www.google.com/favicon"); |
| 901 const GURL icon_url_preferred1("http://www.google.com/favicon_preferred1"); |
| 902 const GURL icon_url_large("http://www.google.com/favicon_large"); |
| 903 |
| 904 TestFaviconHandlerDelegate delegate(contents()); |
| 905 Profile* profile = Profile::FromBrowserContext( |
| 906 contents()->GetBrowserContext()); |
| 907 TestFaviconHandler handler(page_url, profile, |
| 908 &delegate, FaviconHandler::FAVICON); |
| 909 |
| 910 handler.FetchFavicon(page_url); |
| 911 HistoryRequestHandler* history_handler = handler.history_handler(); |
| 912 |
| 913 // Set valid icon data. |
| 914 history_handler->favicon_data_.known_icon = true; |
| 915 history_handler->favicon_data_.icon_type = history::FAVICON; |
| 916 history_handler->favicon_data_.expired = false; |
| 917 history_handler->favicon_data_.icon_url = icon_url; |
| 918 scoped_refptr<RefCountedBytes> data = new RefCountedBytes(); |
| 919 FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data()); |
| 920 history_handler->favicon_data_.image_data = data; |
| 921 |
| 922 // Send history response. |
| 923 history_handler->InvokeCallback(); |
| 924 |
| 925 // Simulates update with the different favicon url. |
| 926 std::vector<FaviconURL> urls; |
| 927 // Note: the code will stop making requests when an icon matching the |
| 928 // preferred size is found, so icon_url_preferred must be last. |
| 929 urls.push_back(FaviconURL(icon_url_preferred1, FaviconURL::FAVICON)); |
| 930 urls.push_back(FaviconURL(icon_url_large, FaviconURL::FAVICON)); |
| 931 handler.OnUpdateFaviconURL(0, urls); |
| 932 EXPECT_EQ(2U, handler.image_urls().size()); |
| 933 |
| 934 DownloadHandler* download_handler = handler.download_handler(); |
| 935 |
| 936 // Download the first icon (set not in history). |
| 937 handler.history_handler()->favicon_data_.known_icon = false; |
| 938 handler.history_handler()->InvokeCallback(); |
| 939 ASSERT_TRUE(download_handler->HasDownload()); |
| 940 EXPECT_EQ(icon_url_preferred1, download_handler->GetImageUrl()); |
| 941 download_handler->SetImageSize(gfx::kFaviconSize); |
| 942 download_handler->InvokeCallback(); |
| 943 // Verify that this was detected as an exact match and image_urls_ is cleared. |
| 944 EXPECT_EQ(0U, handler.image_urls().size()); |
| 945 |
| 946 // Verify correct icon size chosen. |
| 947 EXPECT_EQ(icon_url_preferred1, handler.GetEntry()->GetFavicon().url); |
| 948 EXPECT_TRUE(handler.GetEntry()->GetFavicon().valid); |
| 949 EXPECT_FALSE(handler.GetEntry()->GetFavicon().bitmap.empty()); |
| 950 EXPECT_EQ(gfx::kFaviconSize, handler.GetEntry()->GetFavicon().bitmap.width()); |
| 889 } | 951 } |
| 890 | 952 |
| 891 } // namespace. | 953 } // namespace. |
| OLD | NEW |