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 "base/strings/stringprintf.h"
10 #include "content/public/test/mock_download_item.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using content::DownloadItem
;
15 using ::testing::Mock
;
16 using ::testing::NiceMock
;
17 using ::testing::Return
;
18 using ::testing::ReturnRefOfCopy
;
23 // Default target path for a mock download item in DownloadItemModelTest.
24 const base::FilePath::CharType kDefaultTargetFilePath
[] =
25 FILE_PATH_LITERAL("/foo/bar/foo.bar");
27 // Default URL for a mock download item in DownloadCommandsTest.
28 const char kDefaultURL
[] = "http://example.com/foo.bar";
30 class DownloadCommandsTest
: public testing::Test
{
32 DownloadCommandsTest() : commands_(&item_
) {}
34 virtual ~DownloadCommandsTest() {
38 // Sets up defaults for the download item.
39 void SetUp() override
{
40 ON_CALL(item_
, GetMimeType()).WillByDefault(Return("text/html"));
41 ON_CALL(item_
, CanShowInFolder()).WillByDefault(Return(true));
42 ON_CALL(item_
, CanOpenDownload()).WillByDefault(Return(true));
43 ON_CALL(item_
, IsDone()).WillByDefault(Return(false));
44 ON_CALL(item_
, GetOpenWhenComplete()).WillByDefault(Return(false));
45 ON_CALL(item_
, GetState())
46 .WillByDefault(Return(DownloadItem::IN_PROGRESS
));
47 ON_CALL(item_
, IsPaused()).WillByDefault(Return(false));
48 ON_CALL(item_
, ShouldOpenFileBasedOnExtension())
49 .WillByDefault(Return(false));
51 ON_CALL(item_
, GetTargetDisposition())
53 Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE
));
54 ON_CALL(item_
, GetMimeType()).WillByDefault(Return("text/html"));
55 ON_CALL(item_
, GetURL())
56 .WillByDefault(ReturnRefOfCopy(GURL(kDefaultURL
)));
57 ON_CALL(item_
, GetTargetFilePath())
58 .WillByDefault(ReturnRefOfCopy(base::FilePath(kDefaultTargetFilePath
)));
61 content::MockDownloadItem
& item() {
66 bool IsCommandEnabled(DownloadCommands::Command command
) {
67 return commands().IsCommandEnabled(command
);
70 bool IsCommandChecked(DownloadCommands::Command command
) {
71 return commands().IsCommandChecked(command
);
75 DownloadCommands
& commands() {
80 NiceMock
<content::MockDownloadItem
> item_
;
81 DownloadCommands commands_
;
86 TEST_F(DownloadCommandsTest
, InProgress
) {
87 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::SHOW_IN_FOLDER
));
88 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::CANCEL
));
89 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::PAUSE
));
90 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME
));
91 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::ALWAYS_OPEN_TYPE
));
92 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::OPEN_WHEN_COMPLETE
));
94 EXPECT_FALSE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE
));
95 EXPECT_FALSE(IsCommandChecked(DownloadCommands::ALWAYS_OPEN_TYPE
));
98 TEST_F(DownloadCommandsTest
, OpenWhenCompleteEnabled
) {
99 ON_CALL(item(), GetOpenWhenComplete()).WillByDefault(Return(true));
101 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::OPEN_WHEN_COMPLETE
));
102 EXPECT_TRUE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE
));
105 TEST_F(DownloadCommandsTest
, Finished
) {
106 ON_CALL(item(), IsDone()).WillByDefault(Return(true));
108 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::CANCEL
));
109 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE
));
110 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME
));
113 TEST_F(DownloadCommandsTest
, PausedResumable
) {
114 ON_CALL(item(), IsPaused()).WillByDefault(Return(true));
115 ON_CALL(item(), CanResume()).WillByDefault(Return(true));
117 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE
));
118 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::RESUME
));
121 TEST_F(DownloadCommandsTest
, PausedUnresumable
) {
122 ON_CALL(item(), IsPaused()).WillByDefault(Return(true));
123 ON_CALL(item(), CanResume()).WillByDefault(Return(false));
125 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE
));
126 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME
));
129 TEST_F(DownloadCommandsTest
, DoOpenWhenComplete
) {
130 // Open when complete.
131 EXPECT_CALL(item(), OpenDownload()).Times(1);
132 commands().ExecuteCommand(DownloadCommands::OPEN_WHEN_COMPLETE
);
135 TEST_F(DownloadCommandsTest
, DoShowInFolder
) {
136 // Open when complete.
137 EXPECT_CALL(item(), ShowDownloadInShell()).Times(1);
138 commands().ExecuteCommand(DownloadCommands::SHOW_IN_FOLDER
);
141 TEST_F(DownloadCommandsTest
, DoCancel
) {
143 EXPECT_CALL(item(), Cancel(true)).Times(1);
144 commands().ExecuteCommand(DownloadCommands::CANCEL
);
147 TEST_F(DownloadCommandsTest
, DoPause
) {
149 EXPECT_CALL(item(), Pause()).Times(1);
150 commands().ExecuteCommand(DownloadCommands::PAUSE
);
153 TEST_F(DownloadCommandsTest
, DoResume
) {
155 EXPECT_CALL(item(), Resume()).Times(1);
156 commands().ExecuteCommand(DownloadCommands::RESUME
);
159 TEST_F(DownloadCommandsTest
,
160 GetLearnMoreURLForInterruptedDownload_ContainsContext
) {
161 EXPECT_CALL(item(), GetLastReason())
163 Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED
));
164 GURL learn_more_url
= commands().GetLearnMoreURLForInterruptedDownload();
165 std::string name_value_pair
= base::StringPrintf(
166 "ctx=%d", content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED
);
167 EXPECT_LT(0u, learn_more_url
.query().find(name_value_pair
))
168 << learn_more_url
.spec();