OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <memory> |
| 6 |
| 7 #import <CoreSpotlight/CoreSpotlight.h> |
| 8 #import <Foundation/Foundation.h> |
| 9 |
| 10 #include "base/location.h" |
| 11 #include "base/mac/scoped_nsobject.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "components/bookmarks/browser/bookmark_model.h" |
| 18 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 19 #include "components/bookmarks/test/test_bookmark_client.h" |
| 20 #include "components/favicon/core/favicon_client.h" |
| 21 #include "components/favicon/core/favicon_service.h" |
| 22 #include "components/favicon/core/large_icon_service.h" |
| 23 #include "components/favicon_base/fallback_icon_style.h" |
| 24 #import "ios/chrome/app/spotlight/bookmarks_spotlight_manager.h" |
| 25 #import "ios/chrome/app/spotlight/spotlight_manager.h" |
| 26 #import "ios/chrome/app/spotlight/spotlight_util.h" |
| 27 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 28 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 29 #include "ios/public/provider/chrome/browser/spotlight/spotlight_provider.h" |
| 30 #import "net/base/mac/url_conversions.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "testing/gtest_mac.h" |
| 33 |
| 34 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; |
| 35 |
| 36 favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h) { |
| 37 favicon_base::FaviconRawBitmapResult result; |
| 38 result.expired = false; |
| 39 |
| 40 CGRect rect = CGRectMake(0, 0, w, h); |
| 41 UIGraphicsBeginImageContext(rect.size); |
| 42 CGContextRef context = UIGraphicsGetCurrentContext(); |
| 43 CGContextFillRect(context, rect); |
| 44 UIImage* favicon = UIGraphicsGetImageFromCurrentImageContext(); |
| 45 UIGraphicsEndImageContext(); |
| 46 |
| 47 NSData* png = UIImagePNGRepresentation(favicon); |
| 48 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes( |
| 49 static_cast<const unsigned char*>([png bytes]), [png length])); |
| 50 |
| 51 result.bitmap_data = data; |
| 52 result.pixel_size = gfx::Size(w, h); |
| 53 result.icon_url = GURL(kDummyIconUrl); |
| 54 result.icon_type = favicon_base::TOUCH_ICON; |
| 55 CHECK(result.is_valid()); |
| 56 return result; |
| 57 } |
| 58 |
| 59 // A mock FaviconService that emits pre-programmed response. |
| 60 class MockFaviconService : public favicon::FaviconService { |
| 61 public: |
| 62 MockFaviconService() : FaviconService(nullptr, nullptr) {} |
| 63 |
| 64 ~MockFaviconService() override {} |
| 65 |
| 66 base::CancelableTaskTracker::TaskId GetLargestRawFaviconForPageURL( |
| 67 const GURL& page_url, |
| 68 const std::vector<int>& icon_types, |
| 69 int minimum_size_in_pixels, |
| 70 const favicon_base::FaviconRawBitmapCallback& callback, |
| 71 base::CancelableTaskTracker* tracker) override { |
| 72 favicon_base::FaviconRawBitmapResult mock_result = CreateTestBitmap(24, 24); |
| 73 return tracker->PostTask(base::ThreadTaskRunnerHandle::Get().get(), |
| 74 FROM_HERE, base::Bind(callback, mock_result)); |
| 75 } |
| 76 |
| 77 private: |
| 78 DISALLOW_COPY_AND_ASSIGN(MockFaviconService); |
| 79 }; |
| 80 |
| 81 // This class provides access to LargeIconService internals, using the current |
| 82 // thread's task runner for testing. |
| 83 class TestLargeIconService : public favicon::LargeIconService { |
| 84 public: |
| 85 explicit TestLargeIconService(MockFaviconService* mock_favicon_service) |
| 86 : LargeIconService(mock_favicon_service, |
| 87 base::ThreadTaskRunnerHandle::Get()) {} |
| 88 ~TestLargeIconService() override {} |
| 89 |
| 90 private: |
| 91 DISALLOW_COPY_AND_ASSIGN(TestLargeIconService); |
| 92 }; |
| 93 |
| 94 class SpotlightManagerTest : public testing::Test { |
| 95 protected: |
| 96 SpotlightManagerTest() { |
| 97 mock_favicon_service_.reset(new MockFaviconService()); |
| 98 large_icon_service_.reset( |
| 99 new TestLargeIconService(mock_favicon_service_.get())); |
| 100 model_ = bookmarks::TestBookmarkClient::CreateModel(); |
| 101 mock_favicon_service_.reset(new MockFaviconService()); |
| 102 large_icon_service_.reset( |
| 103 new TestLargeIconService(mock_favicon_service_.get())); |
| 104 bookmarksSpotlightManager_.reset([[BookmarksSpotlightManager alloc] |
| 105 initWithLargeIconService:large_icon_service_.get() |
| 106 bookmarkModel:model_.get()]); |
| 107 } |
| 108 |
| 109 base::MessageLoop loop_; |
| 110 std::unique_ptr<MockFaviconService> mock_favicon_service_; |
| 111 std::unique_ptr<TestLargeIconService> large_icon_service_; |
| 112 base::CancelableTaskTracker cancelable_task_tracker_; |
| 113 std::unique_ptr<bookmarks::BookmarkModel> model_; |
| 114 base::scoped_nsobject<BookmarksSpotlightManager> bookmarksSpotlightManager_; |
| 115 }; |
| 116 |
| 117 TEST_F(SpotlightManagerTest, testSpotlightID) { |
| 118 // Creating CSSearchableItem requires Spotlight to be available on the device. |
| 119 if (!spotlight::IsSpotlightAvailable()) |
| 120 return; |
| 121 GURL url("http://url.com"); |
| 122 NSString* spotlightId = |
| 123 [bookmarksSpotlightManager_ spotlightIDForURL:url title:@"title"]; |
| 124 NSString* expectedSpotlightId = |
| 125 [NSString stringWithFormat:@"%@.9c6b643df2a0c990", |
| 126 spotlight::StringFromSpotlightDomain( |
| 127 spotlight::DOMAIN_BOOKMARKS)]; |
| 128 EXPECT_NSEQ(spotlightId, expectedSpotlightId); |
| 129 } |
| 130 |
| 131 TEST_F(SpotlightManagerTest, testParentKeywordsForNode) { |
| 132 const bookmarks::BookmarkNode* root = model_->bookmark_bar_node(); |
| 133 static const std::string model_string("a 1:[ b c ] d 2:[ 21:[ e ] f g ] h"); |
| 134 bookmarks::test::AddNodesFromModelString(model_.get(), root, model_string); |
| 135 const bookmarks::BookmarkNode* eNode = |
| 136 root->GetChild(3)->GetChild(0)->GetChild(0); |
| 137 base::scoped_nsobject<NSMutableArray> keywords([[NSMutableArray alloc] init]); |
| 138 [bookmarksSpotlightManager_ getParentKeywordsForNode:eNode inArray:keywords]; |
| 139 EXPECT_EQ([keywords count], 2u); |
| 140 EXPECT_TRUE([[keywords objectAtIndex:0] isEqualToString:@"21"]); |
| 141 EXPECT_TRUE([[keywords objectAtIndex:1] isEqualToString:@"2"]); |
| 142 } |
| 143 |
| 144 TEST_F(SpotlightManagerTest, testBookmarksCreateSpotlightItemsWithUrl) { |
| 145 // Creating CSSearchableItem requires Spotlight to be available on the device. |
| 146 if (!spotlight::IsSpotlightAvailable()) |
| 147 return; |
| 148 |
| 149 const bookmarks::BookmarkNode* root = model_->bookmark_bar_node(); |
| 150 static const std::string model_string("a 1:[ b c ] d 2:[ 21:[ e ] f g ] h"); |
| 151 bookmarks::test::AddNodesFromModelString(model_.get(), root, model_string); |
| 152 const bookmarks::BookmarkNode* eNode = |
| 153 root->GetChild(3)->GetChild(0)->GetChild(0); |
| 154 |
| 155 NSString* spotlightID = [bookmarksSpotlightManager_ |
| 156 spotlightIDForURL:eNode->url() |
| 157 title:base::SysUTF16ToNSString(eNode->GetTitle())]; |
| 158 base::scoped_nsobject<NSMutableArray> keywords([[NSMutableArray alloc] init]); |
| 159 [bookmarksSpotlightManager_ getParentKeywordsForNode:eNode inArray:keywords]; |
| 160 NSArray* items = [bookmarksSpotlightManager_ |
| 161 spotlightItemsWithURL:eNode->url() |
| 162 favicon:nil |
| 163 defaultTitle:base::SysUTF16ToNSString(eNode->GetTitle())]; |
| 164 EXPECT_TRUE([items count] == 1); |
| 165 CSSearchableItem* item = [items objectAtIndex:0]; |
| 166 EXPECT_NSEQ([item uniqueIdentifier], spotlightID); |
| 167 EXPECT_NSEQ([[item attributeSet] title], @"e"); |
| 168 EXPECT_NSEQ([[[item attributeSet] URL] absoluteString], @"http://e.com/"); |
| 169 [bookmarksSpotlightManager_ addKeywords:keywords.get() toSearchableItem:item]; |
| 170 // We use the set intersection to verify that the item from the Spotlight |
| 171 // manager |
| 172 // contains all the newly added Keywords. |
| 173 NSMutableSet* spotlightManagerKeywords = |
| 174 [NSMutableSet setWithArray:[[item attributeSet] keywords]]; |
| 175 NSSet* testModelKeywords = [NSSet setWithArray:keywords]; |
| 176 [spotlightManagerKeywords intersectSet:testModelKeywords]; |
| 177 EXPECT_NSEQ(spotlightManagerKeywords, testModelKeywords); |
| 178 } |
| 179 |
| 180 TEST_F(SpotlightManagerTest, testDefaultKeywordsExist) { |
| 181 // Creating CSSearchableItem requires Spotlight to be available on the device. |
| 182 if (!spotlight::IsSpotlightAvailable()) |
| 183 return; |
| 184 |
| 185 const bookmarks::BookmarkNode* root = model_->bookmark_bar_node(); |
| 186 static const std::string model_string("a 1:[ b c ] d 2:[ 21:[ e ] f g ] h"); |
| 187 bookmarks::test::AddNodesFromModelString(model_.get(), root, model_string); |
| 188 const bookmarks::BookmarkNode* aNode = root->GetChild(0); |
| 189 NSArray* items = [bookmarksSpotlightManager_ |
| 190 spotlightItemsWithURL:aNode->url() |
| 191 favicon:nil |
| 192 defaultTitle:base::SysUTF16ToNSString(aNode->GetTitle())]; |
| 193 EXPECT_TRUE([items count] == 1); |
| 194 CSSearchableItem* item = [items objectAtIndex:0]; |
| 195 NSSet* spotlightManagerKeywords = |
| 196 [NSSet setWithArray:[[item attributeSet] keywords]]; |
| 197 EXPECT_TRUE([spotlightManagerKeywords count] > 0); |
| 198 // Check static/hardcoded keywords exist |
| 199 NSSet* hardCodedKeywordsSet = |
| 200 [NSSet setWithArray:ios::GetChromeBrowserProvider() |
| 201 ->GetSpotlightProvider() |
| 202 ->GetAdditionalKeywords()]; |
| 203 EXPECT_TRUE([hardCodedKeywordsSet isSubsetOfSet:spotlightManagerKeywords]); |
| 204 } |
OLD | NEW |