Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: ios/chrome/browser/ui/history/favicon_view_provider_unittest.mm

Issue 2698473004: Split FaviconService and FaviconServiceImpl. (Closed)
Patch Set: Revert requiring non-null service for FaviconHandler. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 #import "ios/chrome/browser/ui/history/favicon_view_provider.h" 5 #import "ios/chrome/browser/ui/history/favicon_view_provider.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/mac/scoped_nsobject.h" 12 #include "base/mac/scoped_nsobject.h"
13 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/sys_string_conversions.h" 15 #include "base/strings/sys_string_conversions.h"
16 #include "base/threading/thread_task_runner_handle.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" 17 #include "components/favicon/core/large_icon_service.h"
18 #include "components/favicon/core/test/mock_favicon_service.h"
20 #include "components/favicon_base/fallback_icon_style.h" 19 #include "components/favicon_base/fallback_icon_style.h"
21 #include "components/favicon_base/favicon_types.h" 20 #include "components/favicon_base/favicon_types.h"
22 #include "ios/chrome/browser/chrome_paths.h" 21 #include "ios/chrome/browser/chrome_paths.h"
23 #include "ios/web/public/test/test_web_thread.h" 22 #include "ios/web/public/test/test_web_thread.h"
24 #include "ios/web/public/test/test_web_thread_bundle.h" 23 #include "ios/web/public/test/test_web_thread_bundle.h"
25 #include "skia/ext/skia_utils_ios.h" 24 #include "skia/ext/skia_utils_ios.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest_mac.h" 26 #include "testing/gtest_mac.h"
27 #include "testing/platform_test.h" 27 #include "testing/platform_test.h"
28 #include "third_party/ocmock/OCMock/OCMock.h" 28 #include "third_party/ocmock/OCMock/OCMock.h"
29 #include "third_party/ocmock/gtest_support.h" 29 #include "third_party/ocmock/gtest_support.h"
30 #include "third_party/skia/include/core/SkColor.h" 30 #include "third_party/skia/include/core/SkColor.h"
31 #include "url/gurl.h" 31 #include "url/gurl.h"
32 32
33 @interface FaviconViewProvider (Testing) 33 @interface FaviconViewProvider (Testing)
34 @property(nonatomic, retain) UIImage* favicon; 34 @property(nonatomic, retain) UIImage* favicon;
35 @property(nonatomic, copy) NSString* fallbackText; 35 @property(nonatomic, copy) NSString* fallbackText;
36 @property(nonatomic, retain) UIColor* fallbackBackgroundColor; 36 @property(nonatomic, retain) UIColor* fallbackBackgroundColor;
37 @property(nonatomic, retain) UIColor* fallbackTextColor; 37 @property(nonatomic, retain) UIColor* fallbackTextColor;
38 @end 38 @end
39 39
40 namespace { 40 namespace {
41 41
42 using favicon::PostCallbackWithResult;
43 using testing::WithArg;
44 using testing::_;
45
42 // Dummy URL for the favicon case. 46 // Dummy URL for the favicon case.
43 const char kTestFaviconURL[] = "http://test/favicon"; 47 const char kTestFaviconURL[] = "http://test/favicon";
44 // Dummy URL for the fallback case. 48 // Dummy URL for the fallback case.
45 const char kTestFallbackURL[] = "http://test/fallback"; 49 const char kTestFallbackURL[] = "http://test/fallback";
46 // Dummy icon URL. 50 // Dummy icon URL.
47 const char kTestFaviconIconURL[] = "http://test/icons/favicon"; 51 const char kTestFaviconIconURL[] = "http://test/icons/favicon";
48 52
49 // Size of dummy favicon image. 53 // Size of dummy favicon image.
50 const CGFloat kTestFaviconSize = 57; 54 const CGFloat kTestFaviconSize = 57;
51 55
52 // Returns a dummy bitmap result for favicon test URL, and an empty result 56 favicon_base::FaviconRawBitmapResult CreateTestBitmap() {
53 // otherwise.
54 favicon_base::FaviconRawBitmapResult CreateTestBitmap(const GURL& url) {
55 favicon_base::FaviconRawBitmapResult result; 57 favicon_base::FaviconRawBitmapResult result;
56 if (url == GURL(kTestFaviconURL)) { 58 result.expired = false;
57 result.expired = false; 59 base::FilePath favicon_path;
58 base::FilePath favicon_path; 60 PathService::Get(ios::DIR_TEST_DATA, &favicon_path);
59 PathService::Get(ios::DIR_TEST_DATA, &favicon_path); 61 favicon_path =
60 favicon_path = 62 favicon_path.Append(FILE_PATH_LITERAL("favicon/test_favicon.png"));
61 favicon_path.Append(FILE_PATH_LITERAL("favicon/test_favicon.png")); 63 NSData* favicon_data = [NSData
62 NSData* favicon_data = [NSData 64 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())];
63 dataWithContentsOfFile:base::SysUTF8ToNSString(favicon_path.value())]; 65 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes(
64 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes( 66 static_cast<const unsigned char*>([favicon_data bytes]),
65 static_cast<const unsigned char*>([favicon_data bytes]), 67 [favicon_data length]));
66 [favicon_data length]));
67 68
68 result.bitmap_data = data; 69 result.bitmap_data = data;
69 CGFloat scaled_size = [UIScreen mainScreen].scale * kTestFaviconSize; 70 CGFloat scaled_size = [UIScreen mainScreen].scale * kTestFaviconSize;
70 result.pixel_size = gfx::Size(scaled_size, scaled_size); 71 result.pixel_size = gfx::Size(scaled_size, scaled_size);
71 result.icon_url = GURL(kTestFaviconIconURL); 72 result.icon_url = GURL(kTestFaviconIconURL);
72 result.icon_type = favicon_base::TOUCH_ICON; 73 result.icon_type = favicon_base::TOUCH_ICON;
73 CHECK(result.is_valid()); 74 CHECK(result.is_valid());
74 }
75 return result; 75 return result;
76 } 76 }
77 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 { 78 class FaviconViewProviderTest : public PlatformTest {
115 protected: 79 protected:
116 void SetUp() override { 80 void SetUp() override {
117 DCHECK_CURRENTLY_ON(web::WebThread::UI); 81 DCHECK_CURRENTLY_ON(web::WebThread::UI);
118 PlatformTest::SetUp(); 82 PlatformTest::SetUp();
119 mock_favicon_service_.reset(new MockFaviconService()); 83 large_icon_service_.reset(new favicon::LargeIconService(
120 large_icon_service_.reset( 84 &mock_favicon_service_, base::ThreadTaskRunnerHandle::Get()));
121 new TestLargeIconService(mock_favicon_service_.get())); 85
86 EXPECT_CALL(mock_favicon_service_, GetLargestRawFaviconForPageURL(
87 GURL(kTestFaviconURL), _, _, _, _))
88 .WillRepeatedly(WithArg<3>(PostCallbackWithResult(CreateTestBitmap())));
122 } 89 }
123 90
124 web::TestWebThreadBundle thread_bundle_; 91 web::TestWebThreadBundle thread_bundle_;
125 std::unique_ptr<MockFaviconService> mock_favicon_service_; 92 testing::StrictMock<favicon::MockFaviconService> mock_favicon_service_;
126 std::unique_ptr<TestLargeIconService> large_icon_service_; 93 std::unique_ptr<favicon::LargeIconService> large_icon_service_;
127 base::CancelableTaskTracker cancelable_task_tracker_; 94 base::CancelableTaskTracker cancelable_task_tracker_;
128 }; 95 };
129 96
130 // Tests that image is set when a favicon is returned from LargeIconService. 97 // Tests that image is set when a favicon is returned from LargeIconService.
131 TEST_F(FaviconViewProviderTest, Favicon) { 98 TEST_F(FaviconViewProviderTest, Favicon) {
132 id mock_delegate = 99 id mock_delegate =
133 [OCMockObject mockForProtocol:@protocol(FaviconViewProviderDelegate)]; 100 [OCMockObject mockForProtocol:@protocol(FaviconViewProviderDelegate)];
134 base::scoped_nsobject<FaviconViewProvider> viewProvider( 101 base::scoped_nsobject<FaviconViewProvider> viewProvider(
135 [[FaviconViewProvider alloc] initWithURL:GURL(kTestFaviconURL) 102 [[FaviconViewProvider alloc] initWithURL:GURL(kTestFaviconURL)
136 faviconSize:kTestFaviconSize 103 faviconSize:kTestFaviconSize
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 EXPECT_NSEQ(defaultText, viewProvider.fallbackText); 140 EXPECT_NSEQ(defaultText, viewProvider.fallbackText);
174 EXPECT_NSEQ(defaultTextColor, viewProvider.fallbackTextColor); 141 EXPECT_NSEQ(defaultTextColor, viewProvider.fallbackTextColor);
175 EXPECT_NSEQ(defaultBackgroundColor, viewProvider.fallbackBackgroundColor); 142 EXPECT_NSEQ(defaultBackgroundColor, viewProvider.fallbackBackgroundColor);
176 }; 143 };
177 [[[mock_delegate stub] andDo:confirmationBlock] 144 [[[mock_delegate stub] andDo:confirmationBlock]
178 faviconViewProviderFaviconDidLoad:item]; 145 faviconViewProviderFaviconDidLoad:item];
179 EXPECT_OCMOCK_VERIFY(mock_delegate); 146 EXPECT_OCMOCK_VERIFY(mock_delegate);
180 } 147 }
181 148
182 } // namespace 149 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698