diff options
author | thestig <thestig@chromium.org> | 2014-09-24 16:03:14 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-24 23:03:32 +0000 |
commit | da4b14f0affb40a6c4d3452a148f2383a48eefce (patch) | |
tree | 4dfeb90910e5517248f52b8f57060ea7f1bb37f7 /components/suggestions | |
parent | d6a8369bbd241f7e6179f3c6a94728f8f985cdbd (diff) | |
download | chromium_src-da4b14f0affb40a6c4d3452a148f2383a48eefce.zip chromium_src-da4b14f0affb40a6c4d3452a148f2383a48eefce.tar.gz chromium_src-da4b14f0affb40a6c4d3452a148f2383a48eefce.tar.bz2 |
Revert of [Suggestions] Move ImageManager to the component (patchset #6 id:220001 of https://codereview.chromium.org/543753002/)
Reason for revert:
One of the new unit tests failed with uninit memory access: http://build.chromium.org/p/chromium.memory.fyi/builders/Chromium%20OS%20%28valgrind%29%282%29/builds/43137/
Original issue's description:
> [Suggestions] Move ImageManager to the component
>
> Keeps the fetcher functionality as ImageFetcherImpl in chrome/browser/search/suggestions.
>
> BUG=387751
> TBR=blundell
> TEST=ImageManagerTest,ImageFetcherBrowserTest
>
> Committed: https://crrev.com/5a25f30080dd57d200679546c970d70f2247def9
> Cr-Commit-Position: refs/heads/master@{#296503}
TBR=huangs@chromium.org,blundell@chromium.org,mathp@chromium.org
NOTREECHECKS=true
NOTRY=true
BUG=387751
Review URL: https://codereview.chromium.org/600203002
Cr-Commit-Position: refs/heads/master@{#296553}
Diffstat (limited to 'components/suggestions')
-rw-r--r-- | components/suggestions/BUILD.gn | 3 | ||||
-rw-r--r-- | components/suggestions/DEPS | 1 | ||||
-rw-r--r-- | components/suggestions/image_fetcher.h | 34 | ||||
-rw-r--r-- | components/suggestions/image_fetcher_delegate.h | 29 | ||||
-rw-r--r-- | components/suggestions/image_manager.cc | 228 | ||||
-rw-r--r-- | components/suggestions/image_manager.h | 135 | ||||
-rw-r--r-- | components/suggestions/image_manager_unittest.cc | 193 |
7 files changed, 10 insertions, 613 deletions
diff --git a/components/suggestions/BUILD.gn b/components/suggestions/BUILD.gn index 12c9576..d505c40 100644 --- a/components/suggestions/BUILD.gn +++ b/components/suggestions/BUILD.gn @@ -6,9 +6,6 @@ static_library("suggestions") { sources = [ "blacklist_store.cc", "blacklist_store.h", - "image_fetcher.h", - "image_fetcher_delegate.h", - "image_manager.cc", "image_manager.h", "suggestions_pref_names.cc", "suggestions_pref_names.h", diff --git a/components/suggestions/DEPS b/components/suggestions/DEPS index 557fff6..e3304f3 100644 --- a/components/suggestions/DEPS +++ b/components/suggestions/DEPS @@ -1,6 +1,5 @@ include_rules = [ "+components/keyed_service/core", - "+components/leveldb_proto", "+components/pref_registry", "+components/variations", "+net", diff --git a/components/suggestions/image_fetcher.h b/components/suggestions/image_fetcher.h deleted file mode 100644 index 52fcff4..0000000 --- a/components/suggestions/image_fetcher.h +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_SUGGESTIONS_IMAGE_FETCHER_H_ -#define COMPONENTS_SUGGESTIONS_IMAGE_FETCHER_H_ - -#include "base/callback.h" -#include "components/suggestions/image_fetcher_delegate.h" -#include "url/gurl.h" - -class SkBitmap; - -namespace suggestions { - -// A class used to fetch server images. -class ImageFetcher { - public: - ImageFetcher() {} - virtual ~ImageFetcher() {} - - virtual void SetImageFetcherDelegate(ImageFetcherDelegate* delegate) = 0; - - virtual void StartOrQueueNetworkRequest( - const GURL& url, const GURL& image_url, - base::Callback<void(const GURL&, const SkBitmap*)> callback) = 0; - - private: - DISALLOW_COPY_AND_ASSIGN(ImageFetcher); -}; - -} // namespace suggestions - -#endif // COMPONENTS_SUGGESTIONS_IMAGE_FETCHER_H_ diff --git a/components/suggestions/image_fetcher_delegate.h b/components/suggestions/image_fetcher_delegate.h deleted file mode 100644 index f2335ef..0000000 --- a/components/suggestions/image_fetcher_delegate.h +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef COMPONENTS_SUGGESTIONS_IMAGE_FETCHER_DELEGATE_H_ -#define COMPONENTS_SUGGESTIONS_IMAGE_FETCHER_DELEGATE_H_ - -class GURL; -class SkBitmap; - -namespace suggestions { - -class ImageFetcherDelegate { - public: - ImageFetcherDelegate() {} - - // Called when an image was fetched. |url| represents the website for which - // the image was fetched. |bitmap| is deleted once out of scope. - virtual void OnImageFetched(const GURL& url, const SkBitmap* bitmap) = 0; - - protected: - virtual ~ImageFetcherDelegate() {} - - DISALLOW_COPY_AND_ASSIGN(ImageFetcherDelegate); -}; - -} // namespace suggestions - -#endif // COMPONENTS_SUGGESTIONS_IMAGE_FETCHER_DELEGATE_H_ diff --git a/components/suggestions/image_manager.cc b/components/suggestions/image_manager.cc deleted file mode 100644 index 197c11f..0000000 --- a/components/suggestions/image_manager.cc +++ /dev/null @@ -1,228 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "components/suggestions/image_manager.h" - -#include "base/bind.h" -#include "components/suggestions/image_fetcher.h" -#include "ui/gfx/codec/jpeg_codec.h" - -using leveldb_proto::ProtoDatabase; - -namespace { - -// From JPEG-encoded bytes to SkBitmap. -SkBitmap* DecodeImage(const std::vector<unsigned char>& encoded_data) { - return gfx::JPEGCodec::Decode(&encoded_data[0], encoded_data.size()); -} - -} // namespace - -namespace suggestions { - -ImageManager::ImageManager() : weak_ptr_factory_(this) {} - -ImageManager::ImageManager(scoped_ptr<ImageFetcher> image_fetcher, - scoped_ptr<ProtoDatabase<ImageData> > database, - const base::FilePath& database_dir) - : image_fetcher_(image_fetcher.Pass()), - database_(database.Pass()), - database_ready_(false), - weak_ptr_factory_(this) { - image_fetcher_->SetImageFetcherDelegate(this); - database_->Init(database_dir, base::Bind(&ImageManager::OnDatabaseInit, - weak_ptr_factory_.GetWeakPtr())); -} - -ImageManager::~ImageManager() {} - -ImageManager::ImageCacheRequest::ImageCacheRequest() {} - -ImageManager::ImageCacheRequest::~ImageCacheRequest() {} - -void ImageManager::Initialize(const SuggestionsProfile& suggestions) { - image_url_map_.clear(); - for (int i = 0; i < suggestions.suggestions_size(); ++i) { - const ChromeSuggestion& suggestion = suggestions.suggestions(i); - if (suggestion.has_thumbnail()) { - image_url_map_[GURL(suggestion.url())] = GURL(suggestion.thumbnail()); - } - } -} - -void ImageManager::GetImageForURL( - const GURL& url, - base::Callback<void(const GURL&, const SkBitmap*)> callback) { - DCHECK(thread_checker_.CalledOnValidThread()); - // If |url| is not found in |image_url_map_|, then invoke |callback| with - // NULL since there is no associated image for this |url|. - GURL image_url; - if (!GetImageURL(url, &image_url)) { - callback.Run(url, NULL); - return; - } - - // |database_| can be NULL if something went wrong in initialization. - if (database_.get() && !database_ready_) { - // Once database is initialized, it will serve pending requests from either - // cache or network. - QueueCacheRequest(url, image_url, callback); - return; - } - - ServeFromCacheOrNetwork(url, image_url, callback); -} - -void ImageManager::OnImageFetched(const GURL& url, const SkBitmap* bitmap) { - SaveImage(url, *bitmap); -} - -bool ImageManager::GetImageURL(const GURL& url, GURL* image_url) { - std::map<GURL, GURL>::iterator it = image_url_map_.find(url); - if (it == image_url_map_.end()) return false; // Not found. - *image_url = it->second; - return true; -} - -void ImageManager::QueueCacheRequest( - const GURL& url, const GURL& image_url, - base::Callback<void(const GURL&, const SkBitmap*)> callback) { - // To be served when the database has loaded. - ImageCacheRequestMap::iterator it = pending_cache_requests_.find(url); - if (it == pending_cache_requests_.end()) { - ImageCacheRequest request; - request.url = url; - request.image_url = image_url; - request.callbacks.push_back(callback); - pending_cache_requests_[url] = request; - } else { - // Request already queued for this url. - it->second.callbacks.push_back(callback); - } -} - -void ImageManager::ServeFromCacheOrNetwork( - const GURL& url, const GURL& image_url, - base::Callback<void(const GURL&, const SkBitmap*)> callback) { - // If there is a image available in memory, return it. - if (!ServeFromCache(url, callback)) { - image_fetcher_->StartOrQueueNetworkRequest(url, image_url, callback); - } -} - -bool ImageManager::ServeFromCache( - const GURL& url, - base::Callback<void(const GURL&, const SkBitmap*)> callback) { - SkBitmap* bitmap = GetBitmapFromCache(url); - if (bitmap) { - callback.Run(url, bitmap); - return true; - } - return false; -} - -SkBitmap* ImageManager::GetBitmapFromCache(const GURL& url) { - ImageMap::iterator image_iter = image_map_.find(url.spec()); - if (image_iter != image_map_.end()) { - return &image_iter->second; - } - return NULL; -} - -void ImageManager::SaveImage(const GURL& url, const SkBitmap& bitmap) { - // Update the image map. - image_map_.insert(std::make_pair(url.spec(), bitmap)); - - if (!database_ready_) return; - - // Attempt to save a JPEG representation to the database. If not successful, - // the fetched bitmap will still be inserted in the cache, above. - std::vector<unsigned char> encoded_data; - if (EncodeImage(bitmap, &encoded_data)) { - // Save the resulting bitmap to the database. - ImageData data; - data.set_url(url.spec()); - data.set_data(std::string(encoded_data.begin(), encoded_data.end())); - scoped_ptr<ProtoDatabase<ImageData>::KeyEntryVector> entries_to_save( - new ProtoDatabase<ImageData>::KeyEntryVector()); - scoped_ptr<std::vector<std::string> > keys_to_remove( - new std::vector<std::string>()); - entries_to_save->push_back(std::make_pair(data.url(), data)); - database_->UpdateEntries(entries_to_save.Pass(), keys_to_remove.Pass(), - base::Bind(&ImageManager::OnDatabaseSave, - weak_ptr_factory_.GetWeakPtr())); - } -} - -void ImageManager::OnDatabaseInit(bool success) { - if (!success) { - DVLOG(1) << "Image database init failed."; - database_.reset(); - ServePendingCacheRequests(); - return; - } - database_->LoadEntries(base::Bind(&ImageManager::OnDatabaseLoad, - weak_ptr_factory_.GetWeakPtr())); -} - -void ImageManager::OnDatabaseLoad(bool success, - scoped_ptr<ImageDataVector> entries) { - if (!success) { - DVLOG(1) << "Image database load failed."; - database_.reset(); - ServePendingCacheRequests(); - return; - } - database_ready_ = true; - - LoadEntriesInCache(entries.Pass()); - ServePendingCacheRequests(); -} - -void ImageManager::OnDatabaseSave(bool success) { - if (!success) { - DVLOG(1) << "Image database save failed."; - database_.reset(); - database_ready_ = false; - } -} - -void ImageManager::LoadEntriesInCache(scoped_ptr<ImageDataVector> entries) { - for (ImageDataVector::iterator it = entries->begin(); it != entries->end(); - ++it) { - std::vector<unsigned char> encoded_data(it->data().begin(), - it->data().end()); - - scoped_ptr<SkBitmap> bitmap(DecodeImage(encoded_data)); - if (bitmap.get()) { - image_map_.insert(std::make_pair(it->url(), *bitmap)); - } - } -} - -void ImageManager::ServePendingCacheRequests() { - for (ImageCacheRequestMap::iterator it = pending_cache_requests_.begin(); - it != pending_cache_requests_.end(); ++it) { - const ImageCacheRequest& request = it->second; - for (CallbackVector::const_iterator callback_it = request.callbacks.begin(); - callback_it != request.callbacks.end(); ++callback_it) { - ServeFromCacheOrNetwork(request.url, request.image_url, *callback_it); - } - } -} - -// static -bool ImageManager::EncodeImage(const SkBitmap& bitmap, - std::vector<unsigned char>* dest) { - SkAutoLockPixels bitmap_lock(bitmap); - if (!bitmap.readyToDraw() || bitmap.isNull()) { - return false; - } - return gfx::JPEGCodec::Encode( - reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), - gfx::JPEGCodec::FORMAT_SkBitmap, bitmap.width(), bitmap.height(), - bitmap.width() * bitmap.bytesPerPixel(), 100, dest); -} - -} // namespace suggestions diff --git a/components/suggestions/image_manager.h b/components/suggestions/image_manager.h index e538a6b..8c09fdf 100644 --- a/components/suggestions/image_manager.h +++ b/components/suggestions/image_manager.h @@ -5,146 +5,31 @@ #ifndef COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ #define COMPONENTS_SUGGESTIONS_IMAGE_MANAGER_H_ -#include <map> -#include <string> -#include <vector> - #include "base/basictypes.h" #include "base/callback.h" -#include "base/containers/hash_tables.h" -#include "base/memory/scoped_ptr.h" -#include "base/memory/weak_ptr.h" -#include "base/threading/thread_checker.h" -#include "components/leveldb_proto/proto_database.h" -#include "components/suggestions/image_fetcher_delegate.h" #include "components/suggestions/proto/suggestions.pb.h" #include "ui/gfx/image/image_skia.h" #include "url/gurl.h" -namespace net { -class URLRequestContextGetter; -} - namespace suggestions { -class ImageData; -class ImageFetcher; -class SuggestionsProfile; - -// A class used to fetch server images asynchronously and manage the caching -// layer (both in memory and on disk). -class ImageManager : public ImageFetcherDelegate { +// An interface to retrieve images related to a specific URL. +class ImageManager { public: - typedef std::vector<ImageData> ImageDataVector; - - ImageManager(scoped_ptr<ImageFetcher> image_fetcher, - scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database, - const base::FilePath& database_dir); - virtual ~ImageManager(); + ImageManager() {} + virtual ~ImageManager() {} - virtual void Initialize(const SuggestionsProfile& suggestions); + // (Re)Initializes states using data received from a SuggestionService. We're + // not doing this in the constructor because an instance may be long-lived. + virtual void Initialize(const SuggestionsProfile& suggestions) = 0; - // Should be called from the UI thread. + // Retrieves stored image for website |url| asynchronously. Calls |callback| + // with Bitmap pointer if found, and NULL otherwise. virtual void GetImageForURL( const GURL& url, - base::Callback<void(const GURL&, const SkBitmap*)> callback); - - protected: - // Perform additional tasks when an image has been fetched. - virtual void OnImageFetched(const GURL& url, const SkBitmap* bitmap) OVERRIDE; + base::Callback<void(const GURL&, const SkBitmap*)> callback) = 0; private: - friend class MockImageManager; - friend class ImageManagerTest; - FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, InitializeTest); - FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, GetImageForURLNetworkCacheHit); - FRIEND_TEST_ALL_PREFIXES(ImageManagerTest, - GetImageForURLNetworkCacheNotInitialized); - - // Used for testing. - ImageManager(); - - typedef std::vector<base::Callback<void(const GURL&, const SkBitmap*)> > - CallbackVector; - typedef base::hash_map<std::string, SkBitmap> ImageMap; - - // State related to an image fetch (associated website url, image_url, - // pending callbacks). - struct ImageCacheRequest { - ImageCacheRequest(); - ~ImageCacheRequest(); - - GURL url; - GURL image_url; - // Queue for pending callbacks, which may accumulate while the request is in - // flight. - CallbackVector callbacks; - }; - - typedef std::map<const GURL, ImageCacheRequest> ImageCacheRequestMap; - - // Looks up image URL for |url|. If found, writes the result to |image_url| - // and returns true. Otherwise just returns false. - bool GetImageURL(const GURL& url, GURL* image_url); - - void QueueCacheRequest( - const GURL& url, const GURL& image_url, - base::Callback<void(const GURL&, const SkBitmap*)> callback); - - void ServeFromCacheOrNetwork( - const GURL& url, const GURL& image_url, - base::Callback<void(const GURL&, const SkBitmap*)> callback); - - // Will return false if no bitmap was found corresponding to |url|, else - // return true and call |callback| with the found bitmap. - bool ServeFromCache( - const GURL& url, - base::Callback<void(const GURL&, const SkBitmap*)> callback); - - // Returns null if the |url| had no entry in the cache. - SkBitmap* GetBitmapFromCache(const GURL& url); - - // Save the image bitmap in the cache and in the database. - void SaveImage(const GURL& url, const SkBitmap& bitmap); - - // Database callback methods. - // Will initiate loading the entries. - void OnDatabaseInit(bool success); - // Will transfer the loaded |entries| in memory (|image_map_|). - void OnDatabaseLoad(bool success, scoped_ptr<ImageDataVector> entries); - void OnDatabaseSave(bool success); - - // Take entries from the database and put them in the local cache. - void LoadEntriesInCache(scoped_ptr<ImageDataVector> entries); - - void ServePendingCacheRequests(); - - // From SkBitmap to the vector of JPEG-encoded bytes, |dst|. Visible only for - // testing. - static bool EncodeImage(const SkBitmap& bitmap, - std::vector<unsigned char>* dest); - - // Map from URL to image URL. Should be kept up to date when a new - // SuggestionsProfile is available. - std::map<GURL, GURL> image_url_map_; - - // Map from website URL to request information, used for pending cache - // requests while the database hasn't loaded. - ImageCacheRequestMap pending_cache_requests_; - - // Holding the bitmaps in memory, keyed by website URL string. - ImageMap image_map_; - - scoped_ptr<ImageFetcher> image_fetcher_; - - scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> > database_; - - bool database_ready_; - - base::WeakPtrFactory<ImageManager> weak_ptr_factory_; - - base::ThreadChecker thread_checker_; - DISALLOW_COPY_AND_ASSIGN(ImageManager); }; diff --git a/components/suggestions/image_manager_unittest.cc b/components/suggestions/image_manager_unittest.cc deleted file mode 100644 index 757b810..0000000 --- a/components/suggestions/image_manager_unittest.cc +++ /dev/null @@ -1,193 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include <string> - -#include "base/files/file_path.h" -#include "base/run_loop.h" -#include "components/leveldb_proto/proto_database.h" -#include "components/leveldb_proto/testing/fake_db.h" -#include "components/suggestions/image_fetcher.h" -#include "components/suggestions/image_fetcher_delegate.h" -#include "components/suggestions/image_manager.h" -#include "components/suggestions/proto/suggestions.pb.h" -#include "testing/gmock/include/gmock/gmock.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "ui/gfx/image/image_skia.h" -#include "url/gurl.h" - -using ::testing::Return; -using ::testing::StrictMock; -using ::testing::_; - -namespace suggestions { - -const char kTestUrl1[] = "http://go.com/"; -const char kTestUrl2[] = "http://goal.com/"; -const char kTestImagePath[] = "files/image_decoding/droids.png"; -const char kInvalidImagePath[] = "files/DOESNOTEXIST"; - -using leveldb_proto::test::FakeDB; -using suggestions::ImageData; -using suggestions::ImageManager; - -typedef base::hash_map<std::string, ImageData> EntryMap; - -void AddEntry(const ImageData& d, EntryMap* map) { (*map)[d.url()] = d; } - -class MockImageFetcher : public suggestions::ImageFetcher { - public: - MockImageFetcher() {} - virtual ~MockImageFetcher() {} - MOCK_METHOD3(StartOrQueueNetworkRequest, - void(const GURL&, const GURL&, - base::Callback<void(const GURL&, const SkBitmap*)>)); - MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); -}; - -class ImageManagerTest : public testing::Test { - public: - ImageManagerTest() - : mock_image_fetcher_(NULL), - num_callback_null_called_(0), - num_callback_valid_called_(0) {} - - virtual void SetUp() OVERRIDE { - fake_db_ = new FakeDB<ImageData>(&db_model_); - image_manager_.reset(CreateImageManager(fake_db_)); - } - - virtual void TearDown() OVERRIDE { - fake_db_ = NULL; - db_model_.clear(); - image_manager_.reset(); - } - - void InitializeDefaultImageMapAndDatabase(ImageManager* image_manager, - FakeDB<ImageData>* fake_db) { - CHECK(image_manager); - CHECK(fake_db); - - suggestions::SuggestionsProfile suggestions_profile; - suggestions::ChromeSuggestion* suggestion = - suggestions_profile.add_suggestions(); - suggestion->set_url(kTestUrl1); - suggestion->set_thumbnail(kTestImagePath); - - image_manager->Initialize(suggestions_profile); - - // Initialize empty database. - fake_db->InitCallback(true); - fake_db->LoadCallback(true); - } - - ImageData GetSampleImageData(const std::string& url) { - // Create test bitmap. - SkBitmap bm; - bm.allocN32Pixels(2, 2); - ImageData data; - data.set_url(url); - std::vector<unsigned char> encoded; - EXPECT_TRUE(ImageManager::EncodeImage(bm, &encoded)); - data.set_data(std::string(encoded.begin(), encoded.end())); - return data; - } - - void OnImageAvailable(base::RunLoop* loop, const GURL& url, - const SkBitmap* bitmap) { - if (bitmap) { - num_callback_valid_called_++; - } else { - num_callback_null_called_++; - } - loop->Quit(); - } - - ImageManager* CreateImageManager(FakeDB<ImageData>* fake_db) { - mock_image_fetcher_ = new StrictMock<MockImageFetcher>(); - EXPECT_CALL(*mock_image_fetcher_, SetImageFetcherDelegate(_)); - return new ImageManager( - scoped_ptr<ImageFetcher>(mock_image_fetcher_), - scoped_ptr<leveldb_proto::ProtoDatabase<ImageData> >(fake_db), - FakeDB<ImageData>::DirectoryForTestDB()); - } - - EntryMap db_model_; - // Owned by the ImageManager under test. - FakeDB<ImageData>* fake_db_; - - MockImageFetcher* mock_image_fetcher_; - - int num_callback_null_called_; - int num_callback_valid_called_; - // Under test. - scoped_ptr<ImageManager> image_manager_; -}; - -TEST_F(ImageManagerTest, InitializeTest) { - SuggestionsProfile suggestions_profile; - ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); - suggestion->set_url(kTestUrl1); - suggestion->set_thumbnail(kTestImagePath); - - image_manager_->Initialize(suggestions_profile); - - GURL output; - EXPECT_TRUE(image_manager_->GetImageURL(GURL(kTestUrl1), &output)); - EXPECT_EQ(GURL(kTestImagePath), output); - - EXPECT_FALSE(image_manager_->GetImageURL(GURL("http://b.com"), &output)); -} - -TEST_F(ImageManagerTest, GetImageForURLNetwork) { - InitializeDefaultImageMapAndDatabase(image_manager_.get(), fake_db_); - - // We expect the fetcher to go to network and call the callback. - EXPECT_CALL(*mock_image_fetcher_, StartOrQueueNetworkRequest(_, _, _)); - - // Fetch existing URL. - base::RunLoop run_loop; - image_manager_->GetImageForURL(GURL(kTestUrl1), - base::Bind(&ImageManagerTest::OnImageAvailable, - base::Unretained(this), &run_loop)); - - // Will not go to network and use the fetcher since URL is invalid. - // Fetch non-existing URL. - image_manager_->GetImageForURL(GURL(kTestUrl2), - base::Bind(&ImageManagerTest::OnImageAvailable, - base::Unretained(this), &run_loop)); - run_loop.Run(); - - EXPECT_EQ(1, num_callback_null_called_); -} - -TEST_F(ImageManagerTest, GetImageForURLNetworkCacheHit) { - SuggestionsProfile suggestions_profile; - ChromeSuggestion* suggestion = suggestions_profile.add_suggestions(); - suggestion->set_url(kTestUrl1); - // The URL we set is invalid, to show that it will fail from network. - suggestion->set_thumbnail(kInvalidImagePath); - - // Create the ImageManager with an added entry in the database. - AddEntry(GetSampleImageData(kTestUrl1), &db_model_); - FakeDB<ImageData>* fake_db = new FakeDB<ImageData>(&db_model_); - image_manager_.reset(CreateImageManager(fake_db)); - image_manager_->Initialize(suggestions_profile); - fake_db->InitCallback(true); - fake_db->LoadCallback(true); - // Expect something in the cache. - SkBitmap* bitmap = image_manager_->GetBitmapFromCache(GURL(kTestUrl1)); - EXPECT_FALSE(bitmap->isNull()); - - base::RunLoop run_loop; - image_manager_->GetImageForURL(GURL(kTestUrl1), - base::Bind(&ImageManagerTest::OnImageAvailable, - base::Unretained(this), &run_loop)); - run_loop.Run(); - - EXPECT_EQ(0, num_callback_null_called_); - EXPECT_EQ(1, num_callback_valid_called_); -} - -} // namespace suggestions |