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.
5 #include "chrome/browser/download/download_commands.h"
9 #include "content/public/test/mock_download_item.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using content::DownloadItem
;
14 using ::testing::Mock
;
15 using ::testing::NiceMock
;
16 using ::testing::Return
;
17 using ::testing::ReturnRefOfCopy
;
22 // Default target path for a mock download item in DownloadItemModelTest.
23 const base::FilePath::CharType kDefaultTargetFilePath
[] =
24 FILE_PATH_LITERAL("/foo/bar/foo.bar");
26 // Default URL for a mock download item in DownloadCommandsTest.
27 const char kDefaultURL
[] = "http://example.com/foo.bar";
29 class DownloadCommandsTest
: public testing::Test
{
31 DownloadCommandsTest() : commands_(&item_
) {}
33 virtual ~DownloadCommandsTest() {
37 // Sets up defaults for the download item.
38 void SetUp() override
{
39 ON_CALL(item_
, GetMimeType()).WillByDefault(Return("text/html"));
40 ON_CALL(item_
, CanShowInFolder()).WillByDefault(Return(true));
41 ON_CALL(item_
, CanOpenDownload()).WillByDefault(Return(true));
42 ON_CALL(item_
, IsDone()).WillByDefault(Return(false));
43 ON_CALL(item_
, GetOpenWhenComplete()).WillByDefault(Return(false));
44 ON_CALL(item_
, GetState())
45 .WillByDefault(Return(DownloadItem::IN_PROGRESS
));
46 ON_CALL(item_
, IsPaused()).WillByDefault(Return(false));
47 ON_CALL(item_
, ShouldOpenFileBasedOnExtension())
48 .WillByDefault(Return(false));
50 ON_CALL(item_
, GetTargetDisposition())
52 Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE
));
53 ON_CALL(item_
, GetMimeType()).WillByDefault(Return("text/html"));
54 ON_CALL(item_
, GetURL())
55 .WillByDefault(ReturnRefOfCopy(GURL(kDefaultURL
)));
56 ON_CALL(item_
, GetTargetFilePath())
57 .WillByDefault(ReturnRefOfCopy(base::FilePath(kDefaultTargetFilePath
)));
60 content::MockDownloadItem
& item() {
65 bool IsCommandEnabled(DownloadCommands::Command command
) {
66 return commands().IsCommandEnabled(command
);
69 bool IsCommandChecked(DownloadCommands::Command command
) {
70 return commands().IsCommandChecked(command
);
74 DownloadCommands
& commands() {
79 NiceMock
<content::MockDownloadItem
> item_
;
80 DownloadCommands commands_
;
85 TEST_F(DownloadCommandsTest
, InProgress
) {
86 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::SHOW_IN_FOLDER
));
87 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::CANCEL
));
88 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::PAUSE
));
89 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME
));
90 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::ALWAYS_OPEN_TYPE
));
91 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::OPEN_WHEN_COMPLETE
));
93 EXPECT_FALSE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE
));
94 EXPECT_FALSE(IsCommandChecked(DownloadCommands::ALWAYS_OPEN_TYPE
));
97 TEST_F(DownloadCommandsTest
, OpenWhenCompleteEnabled
) {
98 ON_CALL(item(), GetOpenWhenComplete()).WillByDefault(Return(true));
100 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::OPEN_WHEN_COMPLETE
));
101 EXPECT_TRUE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE
));
104 TEST_F(DownloadCommandsTest
, Finished
) {
105 ON_CALL(item(), IsDone()).WillByDefault(Return(true));
107 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::CANCEL
));
108 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE
));
109 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME
));
112 TEST_F(DownloadCommandsTest
, PausedResumable
) {
113 ON_CALL(item(), IsPaused()).WillByDefault(Return(true));
114 ON_CALL(item(), CanResume()).WillByDefault(Return(true));
116 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE
));
117 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::RESUME
));
120 TEST_F(DownloadCommandsTest
, PausedUnresumable
) {
121 ON_CALL(item(), IsPaused()).WillByDefault(Return(true));
122 ON_CALL(item(), CanResume()).WillByDefault(Return(false));
124 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE
));
125 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME
));
128 TEST_F(DownloadCommandsTest
, DoOpenWhenComplete
) {
129 // Open when complete.
130 EXPECT_CALL(item(), OpenDownload()).Times(1);
131 commands().ExecuteCommand(DownloadCommands::OPEN_WHEN_COMPLETE
);
134 TEST_F(DownloadCommandsTest
, DoShowInFolder
) {
135 // Open when complete.
136 EXPECT_CALL(item(), ShowDownloadInShell()).Times(1);
137 commands().ExecuteCommand(DownloadCommands::SHOW_IN_FOLDER
);
140 TEST_F(DownloadCommandsTest
, DoCancel
) {
142 EXPECT_CALL(item(), Cancel(true)).Times(1);
143 commands().ExecuteCommand(DownloadCommands::CANCEL
);
146 TEST_F(DownloadCommandsTest
, DoPause
) {
148 EXPECT_CALL(item(), Pause()).Times(1);
149 commands().ExecuteCommand(DownloadCommands::PAUSE
);
152 TEST_F(DownloadCommandsTest
, DoResume
) {
154 EXPECT_CALL(item(), Resume()).Times(1);
155 commands().ExecuteCommand(DownloadCommands::RESUME
);