Chromium Code Reviews| 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::PostCallbackWithResult; | |
| 38 using testing::WithArg; | |
| 39 using testing::_; | |
| 40 | |
| 37 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; | 41 const char kDummyIconUrl[] = "http://www.example.com/touch_icon.png"; |
| 38 | 42 |
| 39 favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h) { | 43 favicon_base::FaviconRawBitmapResult CreateTestBitmap(int w, int h) { |
| 40 favicon_base::FaviconRawBitmapResult result; | 44 favicon_base::FaviconRawBitmapResult result; |
| 41 result.expired = false; | 45 result.expired = false; |
| 42 | 46 |
| 43 CGRect rect = CGRectMake(0, 0, w, h); | 47 CGRect rect = CGRectMake(0, 0, w, h); |
| 44 UIGraphicsBeginImageContext(rect.size); | 48 UIGraphicsBeginImageContext(rect.size); |
| 45 CGContextRef context = UIGraphicsGetCurrentContext(); | 49 CGContextRef context = UIGraphicsGetCurrentContext(); |
| 46 CGContextFillRect(context, rect); | 50 CGContextFillRect(context, rect); |
| 47 UIImage* favicon = UIGraphicsGetImageFromCurrentImageContext(); | 51 UIImage* favicon = UIGraphicsGetImageFromCurrentImageContext(); |
| 48 UIGraphicsEndImageContext(); | 52 UIGraphicsEndImageContext(); |
| 49 | 53 |
| 50 NSData* png = UIImagePNGRepresentation(favicon); | 54 NSData* png = UIImagePNGRepresentation(favicon); |
| 51 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes( | 55 scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes( |
| 52 static_cast<const unsigned char*>([png bytes]), [png length])); | 56 static_cast<const unsigned char*>([png bytes]), [png length])); |
| 53 | 57 |
| 54 result.bitmap_data = data; | 58 result.bitmap_data = data; |
| 55 result.pixel_size = gfx::Size(w, h); | 59 result.pixel_size = gfx::Size(w, h); |
| 56 result.icon_url = GURL(kDummyIconUrl); | 60 result.icon_url = GURL(kDummyIconUrl); |
| 57 result.icon_type = favicon_base::TOUCH_ICON; | 61 result.icon_type = favicon_base::TOUCH_ICON; |
| 58 CHECK(result.is_valid()); | 62 CHECK(result.is_valid()); |
| 59 return result; | 63 return result; |
| 60 } | 64 } |
| 61 | 65 |
| 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 { | 66 class SpotlightManagerTest : public testing::Test { |
| 98 protected: | 67 protected: |
| 99 SpotlightManagerTest() { | 68 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(); | 69 model_ = bookmarks::TestBookmarkClient::CreateModel(); |
| 104 mock_favicon_service_.reset(new MockFaviconService()); | 70 large_icon_service_.reset(new favicon::LargeIconService( |
| 105 large_icon_service_.reset( | 71 &mock_favicon_service_, base::ThreadTaskRunnerHandle::Get())); |
| 106 new TestLargeIconService(mock_favicon_service_.get())); | |
| 107 bookmarksSpotlightManager_ = [[BookmarksSpotlightManager alloc] | 72 bookmarksSpotlightManager_ = [[BookmarksSpotlightManager alloc] |
| 108 initWithLargeIconService:large_icon_service_.get() | 73 initWithLargeIconService:large_icon_service_.get() |
| 109 bookmarkModel:model_.get()]; | 74 bookmarkModel:model_.get()]; |
| 75 | |
| 76 EXPECT_CALL(mock_favicon_service_, | |
| 77 GetLargestRawFaviconForPageURL(_, _, _, _, _)) | |
| 78 .WillRepeatedly( | |
| 79 WithArg<3>(PostCallbackWithResult(CreateTestBitmap(24, 24)))); | |
|
pkotwicz
2017/02/16 04:07:27
This is ok because there aren't a lot of places wh
mastiz
2017/02/16 09:46:25
The upside is, only a single method can be impleme
| |
| 110 } | 80 } |
| 111 | 81 |
| 112 base::MessageLoop loop_; | 82 base::MessageLoop loop_; |
| 113 std::unique_ptr<MockFaviconService> mock_favicon_service_; | 83 testing::StrictMock<favicon::MockFaviconService> mock_favicon_service_; |
| 114 std::unique_ptr<TestLargeIconService> large_icon_service_; | 84 std::unique_ptr<favicon::LargeIconService> large_icon_service_; |
| 115 base::CancelableTaskTracker cancelable_task_tracker_; | 85 base::CancelableTaskTracker cancelable_task_tracker_; |
| 116 std::unique_ptr<bookmarks::BookmarkModel> model_; | 86 std::unique_ptr<bookmarks::BookmarkModel> model_; |
| 117 BookmarksSpotlightManager* bookmarksSpotlightManager_; | 87 BookmarksSpotlightManager* bookmarksSpotlightManager_; |
| 118 }; | 88 }; |
| 119 | 89 |
| 120 TEST_F(SpotlightManagerTest, testSpotlightID) { | 90 TEST_F(SpotlightManagerTest, testSpotlightID) { |
| 121 // Creating CSSearchableItem requires Spotlight to be available on the device. | 91 // Creating CSSearchableItem requires Spotlight to be available on the device. |
| 122 if (!spotlight::IsSpotlightAvailable()) | 92 if (!spotlight::IsSpotlightAvailable()) |
| 123 return; | 93 return; |
| 124 GURL url("http://url.com"); | 94 GURL url("http://url.com"); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 NSSet* spotlightManagerKeywords = | 168 NSSet* spotlightManagerKeywords = |
| 199 [NSSet setWithArray:[[item attributeSet] keywords]]; | 169 [NSSet setWithArray:[[item attributeSet] keywords]]; |
| 200 EXPECT_TRUE([spotlightManagerKeywords count] > 0); | 170 EXPECT_TRUE([spotlightManagerKeywords count] > 0); |
| 201 // Check static/hardcoded keywords exist | 171 // Check static/hardcoded keywords exist |
| 202 NSSet* hardCodedKeywordsSet = | 172 NSSet* hardCodedKeywordsSet = |
| 203 [NSSet setWithArray:ios::GetChromeBrowserProvider() | 173 [NSSet setWithArray:ios::GetChromeBrowserProvider() |
| 204 ->GetSpotlightProvider() | 174 ->GetSpotlightProvider() |
| 205 ->GetAdditionalKeywords()]; | 175 ->GetAdditionalKeywords()]; |
| 206 EXPECT_TRUE([hardCodedKeywordsSet isSubsetOfSet:spotlightManagerKeywords]); | 176 EXPECT_TRUE([hardCodedKeywordsSet isSubsetOfSet:spotlightManagerKeywords]); |
| 207 } | 177 } |
| OLD | NEW |