OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/download/download_commands.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 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" |
| 12 |
| 13 using content::DownloadItem; |
| 14 using ::testing::Mock; |
| 15 using ::testing::NiceMock; |
| 16 using ::testing::Return; |
| 17 using ::testing::ReturnRefOfCopy; |
| 18 using ::testing::_; |
| 19 |
| 20 namespace { |
| 21 |
| 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"); |
| 25 |
| 26 // Default URL for a mock download item in DownloadCommandsTest. |
| 27 const char kDefaultURL[] = "http://example.com/foo.bar"; |
| 28 |
| 29 class DownloadCommandsTest : public testing::Test { |
| 30 public: |
| 31 DownloadCommandsTest() : commands_(&item_) {} |
| 32 |
| 33 virtual ~DownloadCommandsTest() { |
| 34 } |
| 35 |
| 36 protected: |
| 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)); |
| 49 |
| 50 ON_CALL(item_, GetTargetDisposition()) |
| 51 .WillByDefault( |
| 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))); |
| 58 } |
| 59 |
| 60 content::MockDownloadItem& item() { |
| 61 return item_; |
| 62 } |
| 63 |
| 64 |
| 65 bool IsCommandEnabled(DownloadCommands::Command command) { |
| 66 return commands().IsCommandEnabled(command); |
| 67 } |
| 68 |
| 69 bool IsCommandChecked(DownloadCommands::Command command) { |
| 70 return commands().IsCommandChecked(command); |
| 71 } |
| 72 |
| 73 |
| 74 DownloadCommands& commands() { |
| 75 return commands_; |
| 76 } |
| 77 |
| 78 private: |
| 79 NiceMock<content::MockDownloadItem> item_; |
| 80 DownloadCommands commands_; |
| 81 }; |
| 82 |
| 83 } // namespace |
| 84 |
| 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)); |
| 92 |
| 93 EXPECT_FALSE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE)); |
| 94 EXPECT_FALSE(IsCommandChecked(DownloadCommands::ALWAYS_OPEN_TYPE)); |
| 95 } |
| 96 |
| 97 TEST_F(DownloadCommandsTest, OpenWhenCompleteEnabled) { |
| 98 ON_CALL(item(), GetOpenWhenComplete()).WillByDefault(Return(true)); |
| 99 |
| 100 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::OPEN_WHEN_COMPLETE)); |
| 101 EXPECT_TRUE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE)); |
| 102 } |
| 103 |
| 104 TEST_F(DownloadCommandsTest, Finished) { |
| 105 ON_CALL(item(), IsDone()).WillByDefault(Return(true)); |
| 106 |
| 107 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::CANCEL)); |
| 108 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE)); |
| 109 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME)); |
| 110 } |
| 111 |
| 112 TEST_F(DownloadCommandsTest, PausedResumable) { |
| 113 ON_CALL(item(), IsPaused()).WillByDefault(Return(true)); |
| 114 ON_CALL(item(), CanResume()).WillByDefault(Return(true)); |
| 115 |
| 116 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE)); |
| 117 EXPECT_TRUE(IsCommandEnabled(DownloadCommands::RESUME)); |
| 118 } |
| 119 |
| 120 TEST_F(DownloadCommandsTest, PausedUnresumable) { |
| 121 ON_CALL(item(), IsPaused()).WillByDefault(Return(true)); |
| 122 ON_CALL(item(), CanResume()).WillByDefault(Return(false)); |
| 123 |
| 124 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE)); |
| 125 EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME)); |
| 126 } |
| 127 |
| 128 TEST_F(DownloadCommandsTest, DoOpenWhenComplete) { |
| 129 // Open when complete. |
| 130 EXPECT_CALL(item(), OpenDownload()).Times(1); |
| 131 commands().ExecuteCommand(DownloadCommands::OPEN_WHEN_COMPLETE); |
| 132 } |
| 133 |
| 134 TEST_F(DownloadCommandsTest, DoShowInFolder) { |
| 135 // Open when complete. |
| 136 EXPECT_CALL(item(), ShowDownloadInShell()).Times(1); |
| 137 commands().ExecuteCommand(DownloadCommands::SHOW_IN_FOLDER); |
| 138 } |
| 139 |
| 140 TEST_F(DownloadCommandsTest, DoCancel) { |
| 141 // Cancel. |
| 142 EXPECT_CALL(item(), Cancel(true)).Times(1); |
| 143 commands().ExecuteCommand(DownloadCommands::CANCEL); |
| 144 } |
| 145 |
| 146 TEST_F(DownloadCommandsTest, DoPause) { |
| 147 // Pause. |
| 148 EXPECT_CALL(item(), Pause()).Times(1); |
| 149 commands().ExecuteCommand(DownloadCommands::PAUSE); |
| 150 } |
| 151 |
| 152 TEST_F(DownloadCommandsTest, DoResume) { |
| 153 // Resume. |
| 154 EXPECT_CALL(item(), Resume()).Times(1); |
| 155 commands().ExecuteCommand(DownloadCommands::RESUME); |
| 156 } |
OLD | NEW |