This source file includes following definitions.
- NotifyDownloadStarting
- manager
- manager_observer
- item_observer
- received_item
- receiver_factory_
- SetUp
- GetMockDownload
- GetTestDelegate
- TEST_F
- TEST_F
- TEST_F
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/download/download_ui_controller.h"
#include "content/public/test/mock_download_item.h"
#include "content/public/test/mock_download_manager.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::MockDownloadItem;
using content::MockDownloadManager;
using testing::AnyNumber;
using testing::Assign;
using testing::Return;
using testing::ReturnRefOfCopy;
using testing::SaveArg;
using testing::_;
namespace {
class TestDelegate : public DownloadUIController::Delegate {
public:
explicit TestDelegate(base::WeakPtr<content::DownloadItem*> receiver);
virtual ~TestDelegate() {}
private:
virtual void NotifyDownloadStarting(content::DownloadItem* item) OVERRIDE;
base::WeakPtr<content::DownloadItem*> receiver_;
};
TestDelegate::TestDelegate(base::WeakPtr<content::DownloadItem*> receiver)
: receiver_(receiver) {
}
void TestDelegate::NotifyDownloadStarting(content::DownloadItem* item) {
if (receiver_.get())
*receiver_ = item;
}
class DownloadUIControllerTest : public testing::Test {
public:
DownloadUIControllerTest();
protected:
virtual void SetUp() OVERRIDE;
scoped_ptr<MockDownloadItem> GetMockDownload();
scoped_ptr<DownloadUIController::Delegate> GetTestDelegate();
MockDownloadManager* manager() { return manager_.get(); }
content::DownloadManager::Observer* manager_observer() {
return manager_observer_;
}
content::DownloadItem::Observer* item_observer() { return item_observer_; }
content::DownloadItem* received_item() { return received_item_; }
private:
scoped_ptr<MockDownloadManager> manager_;
content::DownloadManager::Observer* manager_observer_;
content::DownloadItem::Observer* item_observer_;
content::DownloadItem* received_item_;
base::WeakPtrFactory<content::DownloadItem*> receiver_factory_;
};
DownloadUIControllerTest::DownloadUIControllerTest()
: manager_observer_(NULL),
item_observer_(NULL),
received_item_(NULL),
receiver_factory_(&received_item_) {
}
void DownloadUIControllerTest::SetUp() {
manager_.reset(new testing::StrictMock<MockDownloadManager>());
EXPECT_CALL(*manager_, AddObserver(_))
.WillOnce(SaveArg<0>(&manager_observer_));
EXPECT_CALL(*manager_, RemoveObserver(_))
.WillOnce(Assign(&manager_observer_,
static_cast<content::DownloadManager::Observer*>(NULL)));
EXPECT_CALL(*manager_, GetAllDownloads(_));
}
scoped_ptr<MockDownloadItem> DownloadUIControllerTest::GetMockDownload() {
scoped_ptr<MockDownloadItem> item(
new testing::StrictMock<MockDownloadItem>());
EXPECT_CALL(*item, AddObserver(_))
.WillOnce(SaveArg<0>(&item_observer_));
EXPECT_CALL(*item, RemoveObserver(_))
.WillOnce(Assign(&item_observer_,
static_cast<content::DownloadItem::Observer*>(NULL)));
return item.Pass();
}
scoped_ptr<DownloadUIController::Delegate>
DownloadUIControllerTest::GetTestDelegate() {
scoped_ptr<DownloadUIController::Delegate> delegate(
new TestDelegate(receiver_factory_.GetWeakPtr()));
return delegate.Pass();
}
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyBasic) {
scoped_ptr<MockDownloadItem> item = GetMockDownload();
DownloadUIController controller(manager(), GetTestDelegate());
EXPECT_CALL(*item, GetTargetFilePath())
.WillOnce(ReturnRefOfCopy(base::FilePath()));
EXPECT_CALL(*item, GetState())
.WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));
ASSERT_TRUE(manager_observer());
manager_observer()->OnDownloadCreated(manager(), item.get());
EXPECT_FALSE(received_item());
ASSERT_TRUE(item_observer());
EXPECT_CALL(*item, GetTargetFilePath())
.WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
item_observer()->OnDownloadUpdated(item.get());
EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item());
}
TEST_F(DownloadUIControllerTest, DownloadUIController_NotifyReadyOnCreate) {
scoped_ptr<MockDownloadItem> item = GetMockDownload();
DownloadUIController controller(manager(), GetTestDelegate());
EXPECT_CALL(*item, GetTargetFilePath())
.WillOnce(ReturnRefOfCopy(base::FilePath(FILE_PATH_LITERAL("foo"))));
EXPECT_CALL(*item, GetState())
.WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));
ASSERT_TRUE(manager_observer());
manager_observer()->OnDownloadCreated(manager(), item.get());
EXPECT_EQ(static_cast<content::DownloadItem*>(item.get()), received_item());
}
TEST_F(DownloadUIControllerTest, DownloadUIController_NoNotifyHistory) {
scoped_ptr<MockDownloadItem> item = GetMockDownload();
DownloadUIController controller(manager(), GetTestDelegate());
EXPECT_CALL(*item, GetState())
.WillRepeatedly(Return(content::DownloadItem::COMPLETE));
ASSERT_TRUE(manager_observer());
manager_observer()->OnDownloadCreated(manager(), item.get());
EXPECT_FALSE(received_item());
item_observer()->OnDownloadUpdated(item.get());
EXPECT_FALSE(received_item());
}
}