File: download_bubble_row_view_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (201 lines) | stat: -rw-r--r-- 8,673 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
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
129
130
131
132
133
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/download/bubble/download_bubble_row_view.h"

#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/download/bubble/download_bubble_ui_controller.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/download/bubble/download_toolbar_ui_controller.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "components/download/public/common/mock_download_item.h"
#include "components/vector_icons/vector_icons.h"
#include "content/public/browser/download_item_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/clipboard/test/test_clipboard.h"
#include "ui/events/test/test_event.h"
#include "ui/events/types/event_type.h"
#include "ui/views/input_event_activation_protector.h"
#include "ui/views/test/mock_input_event_activation_protector.h"

namespace {

using ::testing::_;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::ReturnRefOfCopy;

constexpr int kTimeSinceDownloadCompletedUpdateSeconds = 60;

class DownloadBubbleRowViewTest : public TestWithBrowserView {
 public:
  DownloadBubbleRowViewTest()
      : TestWithBrowserView(
            content::BrowserTaskEnvironment::TimeSource::MOCK_TIME) {}

  DownloadBubbleRowViewTest(const DownloadBubbleRowViewTest&) = delete;
  DownloadBubbleRowViewTest& operator=(const DownloadBubbleRowViewTest&) =
      delete;

  void SetUp() override {
    TestWithBrowserView::SetUp();

    content::DownloadItemUtils::AttachInfoForTesting(
        &download_item_, browser()->profile(), nullptr);
    ON_CALL(download_item_, GetURL())
        .WillByDefault(ReturnRef(GURL::EmptyGURL()));

    DownloadBubbleNavigationHandler* navigation_handler;
      navigation_handler =
          browser()->GetFeatures().download_toolbar_ui_controller();
    DownloadBubbleUIController* controller =
        browser_view()->GetDownloadBubbleUIController();

    const int bubble_width = ChromeLayoutProvider::Get()->GetDistanceMetric(
        views::DISTANCE_BUBBLE_PREFERRED_WIDTH);
    info_ = std::make_unique<DownloadBubbleRowViewInfo>(DownloadItemModel::Wrap(
        &download_item_,
        std::make_unique<DownloadUIModel::BubbleStatusTextBuilder>()));
    row_view_ = std::make_unique<DownloadBubbleRowView>(
        *info_, controller->GetWeakPtr(), navigation_handler->GetWeakPtr(),
        browser()->AsWeakPtr(), bubble_width);

    auto input_protector =
        std::make_unique<NiceMock<views::MockInputEventActivationProtector>>();
    input_protector_ = input_protector.get();
    ON_CALL(*input_protector_, IsPossiblyUnintendedInteraction(_, _))
        .WillByDefault(Return(false));
    row_view_->SetInputProtectorForTesting(std::move(input_protector));
  }

  void FastForward(base::TimeDelta time) {
    task_environment()->FastForwardBy(time);
  }

  DownloadBubbleRowView* row_view() { return row_view_.get(); }
  download::MockDownloadItem* download_item() { return &download_item_; }

 protected:
  NiceMock<download::MockDownloadItem> download_item_;
  std::unique_ptr<DownloadBubbleRowViewInfo> info_;
  std::unique_ptr<DownloadBubbleRowView> row_view_;
  raw_ptr<NiceMock<views::MockInputEventActivationProtector>> input_protector_;
};

TEST_F(DownloadBubbleRowViewTest, CopyAcceleratorCopiesFile) {
#if BUILDFLAG(IS_WIN)
  base::FilePath target_path(FILE_PATH_LITERAL("\\test.exe"));
#else
  base::FilePath target_path(FILE_PATH_LITERAL("/test.exe"));
#endif
  ON_CALL(*download_item(), GetState())
      .WillByDefault(Return(download::DownloadItem::COMPLETE));
  ON_CALL(*download_item(), GetTargetFilePath())
      .WillByDefault(ReturnRefOfCopy(target_path));

  ui::TestClipboard* clipboard = ui::TestClipboard::CreateForCurrentThread();

  ui::Accelerator accelerator;
  ASSERT_TRUE(browser_view()->GetAccelerator(IDC_COPY, &accelerator));

  row_view()->AcceleratorPressed(accelerator);

  std::vector<ui::FileInfo> filenames;
  clipboard->ReadFilenames(ui::ClipboardBuffer::kCopyPaste, nullptr,
                           &filenames);
  ASSERT_EQ(filenames.size(), 1u);
  EXPECT_EQ(filenames[0].path, target_path);

  clipboard->DestroyClipboardForCurrentThread();
}

TEST_F(DownloadBubbleRowViewTest, UpdateTimeFromCompletedDownload) {
  ON_CALL(*download_item(), GetState())
      .WillByDefault(Return(download::DownloadItem::COMPLETE));
  ON_CALL(*download_item(), GetEndTime())
      .WillByDefault(Return(base::Time::Now()));
  download_item()->NotifyObserversDownloadUpdated();
  // Get starting label for a finished download and ensure it stays
  // the same until one timer interval.
  std::u16string row_label(row_view()->GetSecondaryLabelTextForTesting());
  FastForward(base::Seconds(kTimeSinceDownloadCompletedUpdateSeconds - 1));
  EXPECT_EQ(row_label, row_view()->GetSecondaryLabelTextForTesting());
  // After a timer interval, check to make sure that the label has
  // changed.
  FastForward(base::Seconds(kTimeSinceDownloadCompletedUpdateSeconds));
  EXPECT_NE(row_label, row_view()->GetSecondaryLabelTextForTesting());
}

TEST_F(DownloadBubbleRowViewTest, MainButtonPressed) {
  EXPECT_CALL(*download_item(), OpenDownload()).Times(1);
  row_view()->SimulateMainButtonClickForTesting(ui::test::TestEvent());
}

// Tests that only enabled quick actions that are in the `ui_info_` are visible
// on the row view.
TEST_F(DownloadBubbleRowViewTest, OnlyEnabledQuickActionsVisible) {
  ON_CALL(*download_item(), GetState())
      .WillByDefault(Return(download::DownloadItem::COMPLETE));
  ON_CALL(*download_item(), CanShowInFolder()).WillByDefault(Return(true));
  info_->SetQuickActionsForTesting(
      {{DownloadCommands::PAUSE, u"label", &vector_icons::kPauseIcon},
       {DownloadCommands::SHOW_IN_FOLDER, u"label",
        &vector_icons::kFolderIcon}});
  download_item()->NotifyObserversDownloadUpdated();
  ASSERT_EQ(row_view()->info().quick_actions().size(), 2u);

  // Should not be available because they are not present in the ui_info.
  EXPECT_FALSE(row_view()->IsQuickActionButtonVisibleForTesting(
      DownloadCommands::OPEN_WHEN_COMPLETE));
  EXPECT_FALSE(row_view()->IsQuickActionButtonVisibleForTesting(
      DownloadCommands::RESUME));
  EXPECT_FALSE(row_view()->IsQuickActionButtonVisibleForTesting(
      DownloadCommands::CANCEL));
  // Should not be available because the download is complete.
  ASSERT_FALSE(DownloadCommands(row_view()->model()->GetWeakPtr())
                   .IsCommandEnabled(DownloadCommands::PAUSE));
  EXPECT_FALSE(row_view()->IsQuickActionButtonVisibleForTesting(
      DownloadCommands::PAUSE));
  // Should be available because it is present in the ui_info, and the
  // DownloadItem state allows for this command.
  ASSERT_TRUE(DownloadCommands(row_view()->model()->GetWeakPtr())
                  .IsCommandEnabled(DownloadCommands::SHOW_IN_FOLDER));
  EXPECT_TRUE(row_view()->IsQuickActionButtonVisibleForTesting(
      DownloadCommands::SHOW_IN_FOLDER));
}

// Test that the input protector can deny button clicks.
TEST_F(DownloadBubbleRowViewTest, InputProtectorDeniesClicks) {
  EXPECT_CALL(*input_protector_, IsPossiblyUnintendedInteraction(_, _))
      .WillRepeatedly(Return(true));

  // Test main button
  EXPECT_CALL(*download_item(), OpenDownload()).Times(0);
  row_view()->SimulateMainButtonClickForTesting(ui::test::TestEvent());

  // Test quick action button.
  ON_CALL(*download_item(), GetState())
      .WillByDefault(Return(download::DownloadItem::COMPLETE));
  ON_CALL(*download_item(), CanOpenDownload()).WillByDefault(Return(true));
  info_->SetQuickActionsForTesting({{DownloadCommands::OPEN_WHEN_COMPLETE,
                                     u"label", &vector_icons::kFolderIcon}});
  download_item()->NotifyObserversDownloadUpdated();
  ASSERT_TRUE(row_view()->IsQuickActionButtonVisibleForTesting(
      DownloadCommands::OPEN_WHEN_COMPLETE));

  EXPECT_CALL(*download_item(), OpenDownload()).Times(0);
  ui::MouseEvent event(ui::EventType::kMousePressed, gfx::PointF(),
                       gfx::PointF(), base::TimeTicks::Now(), 0, 0);
  row_view()
      ->GetQuickActionButtonForTesting(DownloadCommands::OPEN_WHEN_COMPLETE)
      ->OnMousePressed(event);
}

}  // namespace