| 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/mac/scoped_nsobject.h" | |
| 12 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
| 18 #include "components/bookmarks/test/bookmark_test_helpers.h" | 17 #include "components/bookmarks/test/bookmark_test_helpers.h" |
| 19 #include "components/bookmarks/test/test_bookmark_client.h" | 18 #include "components/bookmarks/test/test_bookmark_client.h" |
| 20 #include "components/favicon/core/favicon_client.h" | 19 #include "components/favicon/core/favicon_client.h" |
| 21 #include "components/favicon/core/favicon_service.h" | 20 #include "components/favicon/core/favicon_service.h" |
| 22 #include "components/favicon/core/large_icon_service.h" | 21 #include "components/favicon/core/large_icon_service.h" |
| 23 #include "components/favicon_base/fallback_icon_style.h" | 22 #include "components/favicon_base/fallback_icon_style.h" |
| 24 #import "ios/chrome/app/spotlight/bookmarks_spotlight_manager.h" | 23 #import "ios/chrome/app/spotlight/bookmarks_spotlight_manager.h" |
| 25 #import "ios/chrome/app/spotlight/spotlight_manager.h" | 24 #import "ios/chrome/app/spotlight/spotlight_manager.h" |
| 26 #import "ios/chrome/app/spotlight/spotlight_util.h" | 25 #import "ios/chrome/app/spotlight/spotlight_util.h" |
| 27 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 26 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 28 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 27 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 29 #include "ios/public/provider/chrome/browser/spotlight/spotlight_provider.h" | 28 #include "ios/public/provider/chrome/browser/spotlight/spotlight_provider.h" |
| 30 #import "net/base/mac/url_conversions.h" | 29 #import "net/base/mac/url_conversions.h" |
| 31 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 32 #include "testing/gtest_mac.h" | 31 #include "testing/gtest_mac.h" |
| 33 | 32 |
| 33 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 34 #error "This file requires ARC support." |
| 35 #endif |
| 36 |
| 34 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; | 37 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; |
| 35 | 38 |
| 36 favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h) { | 39 favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h) { |
| 37 favicon_base::FaviconRawBitmapResult result; | 40 favicon_base::FaviconRawBitmapResult result; |
| 38 result.expired = false; | 41 result.expired = false; |
| 39 | 42 |
| 40 CGRect rect = CGRectMake(0, 0, w, h); | 43 CGRect rect = CGRectMake(0, 0, w, h); |
| 41 UIGraphicsBeginImageContext(rect.size); | 44 UIGraphicsBeginImageContext(rect.size); |
| 42 CGContextRef context = UIGraphicsGetCurrentContext(); | 45 CGContextRef context = UIGraphicsGetCurrentContext(); |
| 43 CGContextFillRect(context, rect); | 46 CGContextFillRect(context, rect); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 class SpotlightManagerTest : public testing::Test { | 97 class SpotlightManagerTest : public testing::Test { |
| 95 protected: | 98 protected: |
| 96 SpotlightManagerTest() { | 99 SpotlightManagerTest() { |
| 97 mock_favicon_service_.reset(new MockFaviconService()); | 100 mock_favicon_service_.reset(new MockFaviconService()); |
| 98 large_icon_service_.reset( | 101 large_icon_service_.reset( |
| 99 new TestLargeIconService(mock_favicon_service_.get())); | 102 new TestLargeIconService(mock_favicon_service_.get())); |
| 100 model_ = bookmarks::TestBookmarkClient::CreateModel(); | 103 model_ = bookmarks::TestBookmarkClient::CreateModel(); |
| 101 mock_favicon_service_.reset(new MockFaviconService()); | 104 mock_favicon_service_.reset(new MockFaviconService()); |
| 102 large_icon_service_.reset( | 105 large_icon_service_.reset( |
| 103 new TestLargeIconService(mock_favicon_service_.get())); | 106 new TestLargeIconService(mock_favicon_service_.get())); |
| 104 bookmarksSpotlightManager_.reset([[BookmarksSpotlightManager alloc] | 107 bookmarksSpotlightManager_ = [[BookmarksSpotlightManager alloc] |
| 105 initWithLargeIconService:large_icon_service_.get() | 108 initWithLargeIconService:large_icon_service_.get() |
| 106 bookmarkModel:model_.get()]); | 109 bookmarkModel:model_.get()]; |
| 107 } | 110 } |
| 108 | 111 |
| 109 base::MessageLoop loop_; | 112 base::MessageLoop loop_; |
| 110 std::unique_ptr<MockFaviconService> mock_favicon_service_; | 113 std::unique_ptr<MockFaviconService> mock_favicon_service_; |
| 111 std::unique_ptr<TestLargeIconService> large_icon_service_; | 114 std::unique_ptr<TestLargeIconService> large_icon_service_; |
| 112 base::CancelableTaskTracker cancelable_task_tracker_; | 115 base::CancelableTaskTracker cancelable_task_tracker_; |
| 113 std::unique_ptr<bookmarks::BookmarkModel> model_; | 116 std::unique_ptr<bookmarks::BookmarkModel> model_; |
| 114 base::scoped_nsobject<BookmarksSpotlightManager> bookmarksSpotlightManager_; | 117 BookmarksSpotlightManager* bookmarksSpotlightManager_; |
| 115 }; | 118 }; |
| 116 | 119 |
| 117 TEST_F(SpotlightManagerTest, testSpotlightID) { | 120 TEST_F(SpotlightManagerTest, testSpotlightID) { |
| 118 // Creating CSSearchableItem requires Spotlight to be available on the device. | 121 // Creating CSSearchableItem requires Spotlight to be available on the device. |
| 119 if (!spotlight::IsSpotlightAvailable()) | 122 if (!spotlight::IsSpotlightAvailable()) |
| 120 return; | 123 return; |
| 121 GURL url("http://url.com"); | 124 GURL url("http://url.com"); |
| 122 NSString* spotlightId = | 125 NSString* spotlightId = |
| 123 [bookmarksSpotlightManager_ spotlightIDForURL:url title:@"title"]; | 126 [bookmarksSpotlightManager_ spotlightIDForURL:url title:@"title"]; |
| 124 NSString* expectedSpotlightId = | 127 NSString* expectedSpotlightId = |
| 125 [NSString stringWithFormat:@"%@.9c6b643df2a0c990", | 128 [NSString stringWithFormat:@"%@.9c6b643df2a0c990", |
| 126 spotlight::StringFromSpotlightDomain( | 129 spotlight::StringFromSpotlightDomain( |
| 127 spotlight::DOMAIN_BOOKMARKS)]; | 130 spotlight::DOMAIN_BOOKMARKS)]; |
| 128 EXPECT_NSEQ(spotlightId, expectedSpotlightId); | 131 EXPECT_NSEQ(spotlightId, expectedSpotlightId); |
| 129 } | 132 } |
| 130 | 133 |
| 131 TEST_F(SpotlightManagerTest, testParentKeywordsForNode) { | 134 TEST_F(SpotlightManagerTest, testParentKeywordsForNode) { |
| 132 const bookmarks::BookmarkNode* root = model_->bookmark_bar_node(); | 135 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"); | 136 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); | 137 bookmarks::test::AddNodesFromModelString(model_.get(), root, model_string); |
| 135 const bookmarks::BookmarkNode* eNode = | 138 const bookmarks::BookmarkNode* eNode = |
| 136 root->GetChild(3)->GetChild(0)->GetChild(0); | 139 root->GetChild(3)->GetChild(0)->GetChild(0); |
| 137 base::scoped_nsobject<NSMutableArray> keywords([[NSMutableArray alloc] init]); | 140 NSMutableArray* keywords = [[NSMutableArray alloc] init]; |
| 138 [bookmarksSpotlightManager_ getParentKeywordsForNode:eNode inArray:keywords]; | 141 [bookmarksSpotlightManager_ getParentKeywordsForNode:eNode inArray:keywords]; |
| 139 EXPECT_EQ([keywords count], 2u); | 142 EXPECT_EQ([keywords count], 2u); |
| 140 EXPECT_TRUE([[keywords objectAtIndex:0] isEqualToString:@"21"]); | 143 EXPECT_TRUE([[keywords objectAtIndex:0] isEqualToString:@"21"]); |
| 141 EXPECT_TRUE([[keywords objectAtIndex:1] isEqualToString:@"2"]); | 144 EXPECT_TRUE([[keywords objectAtIndex:1] isEqualToString:@"2"]); |
| 142 } | 145 } |
| 143 | 146 |
| 144 TEST_F(SpotlightManagerTest, testBookmarksCreateSpotlightItemsWithUrl) { | 147 TEST_F(SpotlightManagerTest, testBookmarksCreateSpotlightItemsWithUrl) { |
| 145 // Creating CSSearchableItem requires Spotlight to be available on the device. | 148 // Creating CSSearchableItem requires Spotlight to be available on the device. |
| 146 if (!spotlight::IsSpotlightAvailable()) | 149 if (!spotlight::IsSpotlightAvailable()) |
| 147 return; | 150 return; |
| 148 | 151 |
| 149 const bookmarks::BookmarkNode* root = model_->bookmark_bar_node(); | 152 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"); | 153 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); | 154 bookmarks::test::AddNodesFromModelString(model_.get(), root, model_string); |
| 152 const bookmarks::BookmarkNode* eNode = | 155 const bookmarks::BookmarkNode* eNode = |
| 153 root->GetChild(3)->GetChild(0)->GetChild(0); | 156 root->GetChild(3)->GetChild(0)->GetChild(0); |
| 154 | 157 |
| 155 NSString* spotlightID = [bookmarksSpotlightManager_ | 158 NSString* spotlightID = [bookmarksSpotlightManager_ |
| 156 spotlightIDForURL:eNode->url() | 159 spotlightIDForURL:eNode->url() |
| 157 title:base::SysUTF16ToNSString(eNode->GetTitle())]; | 160 title:base::SysUTF16ToNSString(eNode->GetTitle())]; |
| 158 base::scoped_nsobject<NSMutableArray> keywords([[NSMutableArray alloc] init]); | 161 NSMutableArray* keywords = [[NSMutableArray alloc] init]; |
| 159 [bookmarksSpotlightManager_ getParentKeywordsForNode:eNode inArray:keywords]; | 162 [bookmarksSpotlightManager_ getParentKeywordsForNode:eNode inArray:keywords]; |
| 160 NSArray* items = [bookmarksSpotlightManager_ | 163 NSArray* items = [bookmarksSpotlightManager_ |
| 161 spotlightItemsWithURL:eNode->url() | 164 spotlightItemsWithURL:eNode->url() |
| 162 favicon:nil | 165 favicon:nil |
| 163 defaultTitle:base::SysUTF16ToNSString(eNode->GetTitle())]; | 166 defaultTitle:base::SysUTF16ToNSString(eNode->GetTitle())]; |
| 164 EXPECT_TRUE([items count] == 1); | 167 EXPECT_TRUE([items count] == 1); |
| 165 CSSearchableItem* item = [items objectAtIndex:0]; | 168 CSSearchableItem* item = [items objectAtIndex:0]; |
| 166 EXPECT_NSEQ([item uniqueIdentifier], spotlightID); | 169 EXPECT_NSEQ([item uniqueIdentifier], spotlightID); |
| 167 EXPECT_NSEQ([[item attributeSet] title], @"e"); | 170 EXPECT_NSEQ([[item attributeSet] title], @"e"); |
| 168 EXPECT_NSEQ([[[item attributeSet] URL] absoluteString], @"http://e.com/"); | 171 EXPECT_NSEQ([[[item attributeSet] URL] absoluteString], @"http://e.com/"); |
| 169 [bookmarksSpotlightManager_ addKeywords:keywords.get() toSearchableItem:item]; | 172 [bookmarksSpotlightManager_ addKeywords:keywords toSearchableItem:item]; |
| 170 // We use the set intersection to verify that the item from the Spotlight | 173 // We use the set intersection to verify that the item from the Spotlight |
| 171 // manager | 174 // manager |
| 172 // contains all the newly added Keywords. | 175 // contains all the newly added Keywords. |
| 173 NSMutableSet* spotlightManagerKeywords = | 176 NSMutableSet* spotlightManagerKeywords = |
| 174 [NSMutableSet setWithArray:[[item attributeSet] keywords]]; | 177 [NSMutableSet setWithArray:[[item attributeSet] keywords]]; |
| 175 NSSet* testModelKeywords = [NSSet setWithArray:keywords]; | 178 NSSet* testModelKeywords = [NSSet setWithArray:keywords]; |
| 176 [spotlightManagerKeywords intersectSet:testModelKeywords]; | 179 [spotlightManagerKeywords intersectSet:testModelKeywords]; |
| 177 EXPECT_NSEQ(spotlightManagerKeywords, testModelKeywords); | 180 EXPECT_NSEQ(spotlightManagerKeywords, testModelKeywords); |
| 178 } | 181 } |
| 179 | 182 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 195 NSSet* spotlightManagerKeywords = | 198 NSSet* spotlightManagerKeywords = |
| 196 [NSSet setWithArray:[[item attributeSet] keywords]]; | 199 [NSSet setWithArray:[[item attributeSet] keywords]]; |
| 197 EXPECT_TRUE([spotlightManagerKeywords count] > 0); | 200 EXPECT_TRUE([spotlightManagerKeywords count] > 0); |
| 198 // Check static/hardcoded keywords exist | 201 // Check static/hardcoded keywords exist |
| 199 NSSet* hardCodedKeywordsSet = | 202 NSSet* hardCodedKeywordsSet = |
| 200 [NSSet setWithArray:ios::GetChromeBrowserProvider() | 203 [NSSet setWithArray:ios::GetChromeBrowserProvider() |
| 201 ->GetSpotlightProvider() | 204 ->GetSpotlightProvider() |
| 202 ->GetAdditionalKeywords()]; | 205 ->GetAdditionalKeywords()]; |
| 203 EXPECT_TRUE([hardCodedKeywordsSet isSubsetOfSet:spotlightManagerKeywords]); | 206 EXPECT_TRUE([hardCodedKeywordsSet isSubsetOfSet:spotlightManagerKeywords]); |
| 204 } | 207 } |
| OLD | NEW |