OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/history/favicon_view_provider.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/location.h" |
| 12 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/path_service.h" |
| 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/sys_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "components/favicon/core/favicon_client.h" |
| 18 #include "components/favicon/core/favicon_service.h" |
| 19 #include "components/favicon/core/large_icon_service.h" |
| 20 #include "components/favicon_base/fallback_icon_style.h" |
| 21 #include "components/favicon_base/favicon_types.h" |
| 22 #include "ios/chrome/browser/chrome_paths.h" |
| 23 #include "ios/web/public/test/test_web_thread.h" |
| 24 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 25 #include "skia/ext/skia_utils_ios.h" |
| 26 #include "testing/gtest_mac.h" |
| 27 #include "testing/platform_test.h" |
| 28 #include "third_party/ocmock/OCMock/OCMock.h" |
| 29 #include "third_party/ocmock/gtest_support.h" |
| 30 #include "third_party/skia/include/core/SkColor.h" |
| 31 #include "url/gurl.h" |
| 32 |
| 33 @interface FaviconViewProvider (Testing) |
| 34 @property(nonatomic, retain) UIImage* favicon; |
| 35 @property(nonatomic, copy) NSString* fallbackText; |
| 36 @property(nonatomic, retain) UIColor* fallbackBackgroundColor; |
| 37 @property(nonatomic, retain) UIColor* fallbackTextColor; |
| 38 @end |
| 39 |
| 40 namespace { |
| 41 |
| 42 // Dummy URL for the favicon case. |
| 43 const char kTestFaviconURL[] = "http://test/favicon"; |
| 44 // Dummy URL for the fallback case. |
| 45 const char kTestFallbackURL[] = "http://test/fallback"; |
| 46 // Dummy icon URL. |
| 47 const char kTestFaviconIconURL[] = "http://test/icons/favicon"; |
| 48 |
| 49 // Size of dummy favicon image. |
| 50 const CGFloat kTestFaviconSize = 57; |
| 51 |
| 52 // Returns a dummy bitmap result for favicon test URL, and an empty result |
| 53 // otherwise. |
| 54 favicon_base::FaviconRawBitmapResult CreateTestBitmap(const GURL& url) { |
| 55 favicon_base::FaviconRawBitmapResult result; |
| 56 if (url == GURL(kTestFaviconURL)) { |
| 57 result.expired = false; |
| 58 base::FilePath favicon_path; |
| 59 PathService::Get(ios::DIR_TEST_DATA, &favicon_path); |
| 60 favicon_path = |
| 61 favicon_path.Append(FILE_PATH_LITERAL("favicon/test_favicon.png")); |
| 62 NSData* favicon_data = [NSData |
| 63 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; |
| 64 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes( |
| 65 static_cast<const unsigned char*>([favicon_data bytes]), |
| 66 [favicon_data length])); |
| 67 |
| 68 result.bitmap_data = data; |
| 69 CGFloat scaled_size = [UIScreen mainScreen].scale * kTestFaviconSize; |
| 70 result.pixel_size = gfx::Size(scaled_size, scaled_size); |
| 71 result.icon_url = GURL(kTestFaviconIconURL); |
| 72 result.icon_type = favicon_base::TOUCH_ICON; |
| 73 CHECK(result.is_valid()); |
| 74 } |
| 75 return result; |
| 76 } |
| 77 |
| 78 // A mock FaviconService that emits pre-programmed response. |
| 79 class MockFaviconService : public favicon::FaviconService { |
| 80 public: |
| 81 MockFaviconService() : FaviconService(nullptr, nullptr) {} |
| 82 |
| 83 ~MockFaviconService() override {} |
| 84 |
| 85 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( |
| 86 const GURL& page_url, |
| 87 const std::vector<int>& icon_types, |
| 88 int minimum_size_in_pixels, |
| 89 const favicon_base::FaviconRawBitmapCallback& callback, |
| 90 base::CancelableTaskTracker* tracker) override { |
| 91 favicon_base::FaviconRawBitmapResult mock_result = |
| 92 CreateTestBitmap(page_url); |
| 93 return tracker->PostTask(base::ThreadTaskRunnerHandle::Get().get(), |
| 94 FROM_HERE, base::Bind(callback, mock_result)); |
| 95 } |
| 96 |
| 97 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(MockFaviconService); |
| 99 }; |
| 100 |
| 101 // This class provides access to LargeIconService internals, using the current |
| 102 // thread's task runner for testing. |
| 103 class TestLargeIconService : public favicon::LargeIconService { |
| 104 public: |
| 105 explicit TestLargeIconService(MockFaviconService* mock_favicon_service) |
| 106 : LargeIconService(mock_favicon_service, |
| 107 base::ThreadTaskRunnerHandle::Get()) {} |
| 108 ~TestLargeIconService() override {} |
| 109 |
| 110 private: |
| 111 DISALLOW_COPY_AND_ASSIGN(TestLargeIconService); |
| 112 }; |
| 113 |
| 114 class FaviconViewProviderTest : public PlatformTest { |
| 115 protected: |
| 116 void SetUp() override { |
| 117 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 118 PlatformTest::SetUp(); |
| 119 mock_favicon_service_.reset(new MockFaviconService()); |
| 120 large_icon_service_.reset( |
| 121 new TestLargeIconService(mock_favicon_service_.get())); |
| 122 } |
| 123 |
| 124 web::TestWebThreadBundle thread_bundle_; |
| 125 std::unique_ptr<MockFaviconService> mock_favicon_service_; |
| 126 std::unique_ptr<TestLargeIconService> large_icon_service_; |
| 127 base::CancelableTaskTracker cancelable_task_tracker_; |
| 128 }; |
| 129 |
| 130 // Tests that image is set when a favicon is returned from LargeIconService. |
| 131 TEST_F(FaviconViewProviderTest, Favicon) { |
| 132 id mock_delegate = |
| 133 [OCMockObject mockForProtocol:@protocol(FaviconViewProviderDelegate)]; |
| 134 base::scoped_nsobject<FaviconViewProvider> viewProvider( |
| 135 [[FaviconViewProvider alloc] initWithURL:GURL(kTestFaviconURL) |
| 136 faviconSize:kTestFaviconSize |
| 137 minFaviconSize:kTestFaviconSize |
| 138 largeIconService:large_icon_service_.get() |
| 139 delegate:mock_delegate]); |
| 140 void (^confirmationBlock)(NSInvocation*) = ^(NSInvocation* invocation) { |
| 141 FaviconViewProvider* viewProvider; |
| 142 [invocation getArgument:&viewProvider atIndex:2]; |
| 143 EXPECT_NSNE(nil, viewProvider.favicon); |
| 144 }; |
| 145 [[[mock_delegate stub] andDo:confirmationBlock] |
| 146 faviconViewProviderFaviconDidLoad:viewProvider]; |
| 147 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 148 } |
| 149 |
| 150 // Tests that fallback data is set when no favicon is returned from |
| 151 // LargeIconService. |
| 152 TEST_F(FaviconViewProviderTest, FallbackIcon) { |
| 153 id mock_delegate = |
| 154 [OCMockObject mockForProtocol:@protocol(FaviconViewProviderDelegate)]; |
| 155 base::scoped_nsobject<FaviconViewProvider> item([[FaviconViewProvider alloc] |
| 156 initWithURL:GURL(kTestFallbackURL) |
| 157 faviconSize:kTestFaviconSize |
| 158 minFaviconSize:kTestFaviconSize |
| 159 largeIconService:large_icon_service_.get() |
| 160 delegate:mock_delegate]); |
| 161 |
| 162 // Confirm that fallback text and color have been set before delegate call. |
| 163 void (^confirmationBlock)(NSInvocation*) = ^(NSInvocation* invocation) { |
| 164 FaviconViewProvider* viewProvider; |
| 165 [invocation getArgument:&viewProvider atIndex:2]; |
| 166 // Fallback text is the first letter of the URL. |
| 167 NSString* defaultText = @"T"; |
| 168 // Default colors are defined in |
| 169 // components/favicon_base/fallback_icon_style.h. |
| 170 UIColor* defaultTextColor = skia::UIColorFromSkColor(SK_ColorWHITE); |
| 171 UIColor* defaultBackgroundColor = |
| 172 skia::UIColorFromSkColor(SkColorSetRGB(0x78, 0x78, 0x78)); |
| 173 EXPECT_NSEQ(defaultText, viewProvider.fallbackText); |
| 174 EXPECT_NSEQ(defaultTextColor, viewProvider.fallbackTextColor); |
| 175 EXPECT_NSEQ(defaultBackgroundColor, viewProvider.fallbackBackgroundColor); |
| 176 }; |
| 177 [[[mock_delegate stub] andDo:confirmationBlock] |
| 178 faviconViewProviderFaviconDidLoad:item]; |
| 179 EXPECT_OCMOCK_VERIFY(mock_delegate); |
| 180 } |
| 181 |
| 182 } // namespace |
OLD | NEW |