OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <memory> | 5 #include <memory> |
6 | 6 |
7 #import <CoreSpotlight/CoreSpotlight.h> | 7 #import <CoreSpotlight/CoreSpotlight.h> |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 | 9 |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
17 #include "components/bookmarks/test/bookmark_test_helpers.h" | 17 #include "components/bookmarks/test/bookmark_test_helpers.h" |
18 #include "components/bookmarks/test/test_bookmark_client.h" | 18 #include "components/bookmarks/test/test_bookmark_client.h" |
19 #include "components/favicon/core/favicon_client.h" | |
20 #include "components/favicon/core/favicon_service.h" | |
21 #include "components/favicon/core/large_icon_service.h" | 19 #include "components/favicon/core/large_icon_service.h" |
| 20 #include "components/favicon/core/test/mock_favicon_service.h" |
22 #include "components/favicon_base/fallback_icon_style.h" | 21 #include "components/favicon_base/fallback_icon_style.h" |
23 #import "ios/chrome/app/spotlight/bookmarks_spotlight_manager.h" | 22 #import "ios/chrome/app/spotlight/bookmarks_spotlight_manager.h" |
24 #import "ios/chrome/app/spotlight/spotlight_manager.h" | 23 #import "ios/chrome/app/spotlight/spotlight_manager.h" |
25 #import "ios/chrome/app/spotlight/spotlight_util.h" | 24 #import "ios/chrome/app/spotlight/spotlight_util.h" |
26 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 25 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
27 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 26 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
28 #include "ios/public/provider/chrome/browser/spotlight/spotlight_provider.h" | 27 #include "ios/public/provider/chrome/browser/spotlight/spotlight_provider.h" |
29 #import "net/base/mac/url_conversions.h" | 28 #import "net/base/mac/url_conversions.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
31 #include "testing/gtest_mac.h" | 31 #include "testing/gtest_mac.h" |
32 | 32 |
33 #if !defined(__has_feature) || !__has_feature(objc_arc) | 33 #if !defined(__has_feature) || !__has_feature(objc_arc) |
34 #error "This file requires ARC support." | 34 #error "This file requires ARC support." |
35 #endif | 35 #endif |
36 | 36 |
| 37 using favicon::PostReply; |
| 38 using testing::_; |
| 39 |
37 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; | 40 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; |
38 | 41 |
39 favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h) { | 42 favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h) { |
40 favicon_base::FaviconRawBitmapResult result; | 43 favicon_base::FaviconRawBitmapResult result; |
41 result.expired = false; | 44 result.expired = false; |
42 | 45 |
43 CGRect rect = CGRectMake(0, 0, w, h); | 46 CGRect rect = CGRectMake(0, 0, w, h); |
44 UIGraphicsBeginImageContext(rect.size); | 47 UIGraphicsBeginImageContext(rect.size); |
45 CGContextRef context = UIGraphicsGetCurrentContext(); | 48 CGContextRef context = UIGraphicsGetCurrentContext(); |
46 CGContextFillRect(context, rect); | 49 CGContextFillRect(context, rect); |
47 UIImage* favicon = UIGraphicsGetImageFromCurrentImageContext(); | 50 UIImage* favicon = UIGraphicsGetImageFromCurrentImageContext(); |
48 UIGraphicsEndImageContext(); | 51 UIGraphicsEndImageContext(); |
49 | 52 |
50 NSData* png = UIImagePNGRepresentation(favicon); | 53 NSData* png = UIImagePNGRepresentation(favicon); |
51 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes( | 54 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes( |
52 static_cast<const unsigned char*>([png bytes]), [png length])); | 55 static_cast<const unsigned char*>([png bytes]), [png length])); |
53 | 56 |
54 result.bitmap_data = data; | 57 result.bitmap_data = data; |
55 result.pixel_size = gfx::Size(w, h); | 58 result.pixel_size = gfx::Size(w, h); |
56 result.icon_url = GURL(kDummyIconUrl); | 59 result.icon_url = GURL(kDummyIconUrl); |
57 result.icon_type = favicon_base::TOUCH_ICON; | 60 result.icon_type = favicon_base::TOUCH_ICON; |
58 CHECK(result.is_valid()); | 61 CHECK(result.is_valid()); |
59 return result; | 62 return result; |
60 } | 63 } |
61 | 64 |
62 // A mock FaviconService that emits pre-programmed response. | |
63 class MockFaviconService : public favicon::FaviconService { | |
64 public: | |
65 MockFaviconService() : FaviconService(nullptr, nullptr) {} | |
66 | |
67 ~MockFaviconService() override {} | |
68 | |
69 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( | |
70 const GURL& page_url, | |
71 const std::vector<int>& icon_types, | |
72 int minimum_size_in_pixels, | |
73 const favicon_base::FaviconRawBitmapCallback& callback, | |
74 base::CancelableTaskTracker* tracker) override { | |
75 favicon_base::FaviconRawBitmapResult mock_result = CreateTestBitmap(24, 24); | |
76 return tracker->PostTask(base::ThreadTaskRunnerHandle::Get().get(), | |
77 FROM_HERE, base::Bind(callback, mock_result)); | |
78 } | |
79 | |
80 private: | |
81 DISALLOW_COPY_AND_ASSIGN(MockFaviconService); | |
82 }; | |
83 | |
84 // This class provides access to LargeIconService internals, using the current | |
85 // thread's task runner for testing. | |
86 class TestLargeIconService : public favicon::LargeIconService { | |
87 public: | |
88 explicit TestLargeIconService(MockFaviconService* mock_favicon_service) | |
89 : LargeIconService(mock_favicon_service, | |
90 base::ThreadTaskRunnerHandle::Get()) {} | |
91 ~TestLargeIconService() override {} | |
92 | |
93 private: | |
94 DISALLOW_COPY_AND_ASSIGN(TestLargeIconService); | |
95 }; | |
96 | |
97 class SpotlightManagerTest : public testing::Test { | 65 class SpotlightManagerTest : public testing::Test { |
98 protected: | 66 protected: |
99 SpotlightManagerTest() { | 67 SpotlightManagerTest() { |
100 mock_favicon_service_.reset(new MockFaviconService()); | |
101 large_icon_service_.reset( | |
102 new TestLargeIconService(mock_favicon_service_.get())); | |
103 model_ = bookmarks::TestBookmarkClient::CreateModel(); | 68 model_ = bookmarks::TestBookmarkClient::CreateModel(); |
104 mock_favicon_service_.reset(new MockFaviconService()); | 69 large_icon_service_.reset(new favicon::LargeIconService( |
105 large_icon_service_.reset( | 70 &mock_favicon_service_, base::ThreadTaskRunnerHandle::Get())); |
106 new TestLargeIconService(mock_favicon_service_.get())); | |
107 bookmarksSpotlightManager_ = [[BookmarksSpotlightManager alloc] | 71 bookmarksSpotlightManager_ = [[BookmarksSpotlightManager alloc] |
108 initWithLargeIconService:large_icon_service_.get() | 72 initWithLargeIconService:large_icon_service_.get() |
109 bookmarkModel:model_.get()]; | 73 bookmarkModel:model_.get()]; |
| 74 |
| 75 EXPECT_CALL(mock_favicon_service_, |
| 76 GetLargestRawFaviconForPageURL(_, _, _, _, _)) |
| 77 .WillRepeatedly(PostReply<5>(CreateTestBitmap(24, 24))); |
110 } | 78 } |
111 | 79 |
112 base::MessageLoop loop_; | 80 base::MessageLoop loop_; |
113 std::unique_ptr<MockFaviconService> mock_favicon_service_; | 81 testing::StrictMock<favicon::MockFaviconService> mock_favicon_service_; |
114 std::unique_ptr<TestLargeIconService> large_icon_service_; | 82 std::unique_ptr<favicon::LargeIconService> large_icon_service_; |
115 base::CancelableTaskTracker cancelable_task_tracker_; | 83 base::CancelableTaskTracker cancelable_task_tracker_; |
116 std::unique_ptr<bookmarks::BookmarkModel> model_; | 84 std::unique_ptr<bookmarks::BookmarkModel> model_; |
117 BookmarksSpotlightManager* bookmarksSpotlightManager_; | 85 BookmarksSpotlightManager* bookmarksSpotlightManager_; |
118 }; | 86 }; |
119 | 87 |
120 TEST_F(SpotlightManagerTest, testSpotlightID) { | 88 TEST_F(SpotlightManagerTest, testSpotlightID) { |
121 // Creating CSSearchableItem requires Spotlight to be available on the device. | 89 // Creating CSSearchableItem requires Spotlight to be available on the device. |
122 if (!spotlight::IsSpotlightAvailable()) | 90 if (!spotlight::IsSpotlightAvailable()) |
123 return; | 91 return; |
124 GURL url("http://url.com"); | 92 GURL url("http://url.com"); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 NSSet* spotlightManagerKeywords = | 166 NSSet* spotlightManagerKeywords = |
199 [NSSet setWithArray:[[item attributeSet] keywords]]; | 167 [NSSet setWithArray:[[item attributeSet] keywords]]; |
200 EXPECT_TRUE([spotlightManagerKeywords count] > 0); | 168 EXPECT_TRUE([spotlightManagerKeywords count] > 0); |
201 // Check static/hardcoded keywords exist | 169 // Check static/hardcoded keywords exist |
202 NSSet* hardCodedKeywordsSet = | 170 NSSet* hardCodedKeywordsSet = |
203 [NSSet setWithArray:ios::GetChromeBrowserProvider() | 171 [NSSet setWithArray:ios::GetChromeBrowserProvider() |
204 ->GetSpotlightProvider() | 172 ->GetSpotlightProvider() |
205 ->GetAdditionalKeywords()]; | 173 ->GetAdditionalKeywords()]; |
206 EXPECT_TRUE([hardCodedKeywordsSet isSubsetOfSet:spotlightManagerKeywords]); | 174 EXPECT_TRUE([hardCodedKeywordsSet isSubsetOfSet:spotlightManagerKeywords]); |
207 } | 175 } |
OLD | NEW |