download_commands_unittest.cc 5.72 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 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 "chrome/browser/download/download_commands.h"

#include <vector>

9
#include "base/strings/stringprintf.h"
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include "content/public/test/mock_download_item.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using content::DownloadItem;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::ReturnRefOfCopy;
using ::testing::_;

namespace {

// Default target path for a mock download item in DownloadItemModelTest.
const base::FilePath::CharType kDefaultTargetFilePath[] =
    FILE_PATH_LITERAL("/foo/bar/foo.bar");

// Default URL for a mock download item in DownloadCommandsTest.
const char kDefaultURL[] = "http://example.com/foo.bar";

class DownloadCommandsTest : public testing::Test {
 public:
  DownloadCommandsTest() : commands_(&item_) {}

  virtual ~DownloadCommandsTest() {
  }

 protected:
  // Sets up defaults for the download item.
  void SetUp() override {
    ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html"));
    ON_CALL(item_, CanShowInFolder()).WillByDefault(Return(true));
    ON_CALL(item_, CanOpenDownload()).WillByDefault(Return(true));
    ON_CALL(item_, IsDone()).WillByDefault(Return(false));
    ON_CALL(item_, GetOpenWhenComplete()).WillByDefault(Return(false));
    ON_CALL(item_, GetState())
        .WillByDefault(Return(DownloadItem::IN_PROGRESS));
    ON_CALL(item_, IsPaused()).WillByDefault(Return(false));
    ON_CALL(item_, ShouldOpenFileBasedOnExtension())
        .WillByDefault(Return(false));

    ON_CALL(item_, GetTargetDisposition())
        .WillByDefault(
            Return(DownloadItem::TARGET_DISPOSITION_OVERWRITE));
    ON_CALL(item_, GetMimeType()).WillByDefault(Return("text/html"));
    ON_CALL(item_, GetURL())
        .WillByDefault(ReturnRefOfCopy(GURL(kDefaultURL)));
    ON_CALL(item_, GetTargetFilePath())
        .WillByDefault(ReturnRefOfCopy(base::FilePath(kDefaultTargetFilePath)));
  }

  content::MockDownloadItem& item() {
    return item_;
  }


  bool IsCommandEnabled(DownloadCommands::Command command) {
    return commands().IsCommandEnabled(command);
  }

  bool IsCommandChecked(DownloadCommands::Command command) {
    return commands().IsCommandChecked(command);
  }


  DownloadCommands& commands() {
    return commands_;
  }

 private:
  NiceMock<content::MockDownloadItem> item_;
  DownloadCommands commands_;
};

}  // namespace

TEST_F(DownloadCommandsTest, InProgress) {
  EXPECT_TRUE(IsCommandEnabled(DownloadCommands::SHOW_IN_FOLDER));
  EXPECT_TRUE(IsCommandEnabled(DownloadCommands::CANCEL));
  EXPECT_TRUE(IsCommandEnabled(DownloadCommands::PAUSE));
  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME));
  EXPECT_TRUE(IsCommandEnabled(DownloadCommands::ALWAYS_OPEN_TYPE));
  EXPECT_TRUE(IsCommandEnabled(DownloadCommands::OPEN_WHEN_COMPLETE));

  EXPECT_FALSE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE));
  EXPECT_FALSE(IsCommandChecked(DownloadCommands::ALWAYS_OPEN_TYPE));
}

TEST_F(DownloadCommandsTest, OpenWhenCompleteEnabled) {
  ON_CALL(item(), GetOpenWhenComplete()).WillByDefault(Return(true));

  EXPECT_TRUE(IsCommandEnabled(DownloadCommands::OPEN_WHEN_COMPLETE));
  EXPECT_TRUE(IsCommandChecked(DownloadCommands::OPEN_WHEN_COMPLETE));
}

TEST_F(DownloadCommandsTest, Finished) {
  ON_CALL(item(), IsDone()).WillByDefault(Return(true));

  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::CANCEL));
  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE));
  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME));
}

TEST_F(DownloadCommandsTest, PausedResumable) {
  ON_CALL(item(), IsPaused()).WillByDefault(Return(true));
  ON_CALL(item(), CanResume()).WillByDefault(Return(true));

  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE));
  EXPECT_TRUE(IsCommandEnabled(DownloadCommands::RESUME));
}

TEST_F(DownloadCommandsTest, PausedUnresumable) {
  ON_CALL(item(), IsPaused()).WillByDefault(Return(true));
  ON_CALL(item(), CanResume()).WillByDefault(Return(false));

  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE));
  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::RESUME));
}

129 130 131 132 133
TEST_F(DownloadCommandsTest, CantPauseSavePackage) {
  ON_CALL(item(), IsSavePackageDownload()).WillByDefault(Return(true));
  EXPECT_FALSE(IsCommandEnabled(DownloadCommands::PAUSE));
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
TEST_F(DownloadCommandsTest, DoOpenWhenComplete) {
  // Open when complete.
  EXPECT_CALL(item(), OpenDownload()).Times(1);
  commands().ExecuteCommand(DownloadCommands::OPEN_WHEN_COMPLETE);
}

TEST_F(DownloadCommandsTest, DoShowInFolder) {
  // Open when complete.
  EXPECT_CALL(item(), ShowDownloadInShell()).Times(1);
  commands().ExecuteCommand(DownloadCommands::SHOW_IN_FOLDER);
}

TEST_F(DownloadCommandsTest, DoCancel) {
  // Cancel.
  EXPECT_CALL(item(), Cancel(true)).Times(1);
  commands().ExecuteCommand(DownloadCommands::CANCEL);
}

TEST_F(DownloadCommandsTest, DoPause) {
  // Pause.
  EXPECT_CALL(item(), Pause()).Times(1);
  commands().ExecuteCommand(DownloadCommands::PAUSE);
}

TEST_F(DownloadCommandsTest, DoResume) {
  // Resume.
  EXPECT_CALL(item(), Resume()).Times(1);
  commands().ExecuteCommand(DownloadCommands::RESUME);
}
163 164 165 166 167 168 169 170 171 172 173 174

TEST_F(DownloadCommandsTest,
       GetLearnMoreURLForInterruptedDownload_ContainsContext) {
  EXPECT_CALL(item(), GetLastReason())
      .WillOnce(
          Return(content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED));
  GURL learn_more_url = commands().GetLearnMoreURLForInterruptedDownload();
  std::string name_value_pair = base::StringPrintf(
      "ctx=%d", content::DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED);
  EXPECT_LT(0u, learn_more_url.query().find(name_value_pair))
      << learn_more_url.spec();
}