1 // Copyright 2016 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/ui/webui/downloads/downloads_dom_handler.h"
6 
7 #include <utility>
8 #include <vector>
9 
10 #include "chrome/browser/download/download_item_model.h"
11 #include "chrome/browser/ui/webui/downloads/downloads.mojom.h"
12 #include "chrome/browser/ui/webui/downloads/mock_downloads_page.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/download/public/common/mock_download_item.h"
15 #include "content/public/test/browser_task_environment.h"
16 #include "content/public/test/mock_download_manager.h"
17 #include "content/public/test/test_web_ui.h"
18 #include "mojo/public/cpp/bindings/pending_receiver.h"
19 #include "mojo/public/cpp/bindings/pending_remote.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 
23 namespace {
24 
25 class TestDownloadsDOMHandler : public DownloadsDOMHandler {
26  public:
TestDownloadsDOMHandler(mojo::PendingRemote<downloads::mojom::Page> page,content::DownloadManager * download_manager,content::WebUI * web_ui)27   TestDownloadsDOMHandler(mojo::PendingRemote<downloads::mojom::Page> page,
28                           content::DownloadManager* download_manager,
29                           content::WebUI* web_ui)
30       : DownloadsDOMHandler(
31             mojo::PendingReceiver<downloads::mojom::PageHandler>(),
32             std::move(page),
33             download_manager,
34             web_ui) {}
35 
36   using DownloadsDOMHandler::FinalizeRemovals;
37   using DownloadsDOMHandler::RemoveDownloads;
38 };
39 
40 }  // namespace
41 
42 // A fixture to test DownloadsDOMHandler.
43 class DownloadsDOMHandlerTest : public testing::Test {
44  public:
DownloadsDOMHandlerTest()45   DownloadsDOMHandlerTest() {}
46 
47   // testing::Test:
SetUp()48   void SetUp() override {
49     ON_CALL(manager_, GetBrowserContext())
50         .WillByDefault(testing::Return(&profile_));
51   }
52 
profile()53   TestingProfile* profile() { return &profile_; }
manager()54   content::MockDownloadManager* manager() { return &manager_; }
web_ui()55   content::TestWebUI* web_ui() { return &web_ui_; }
56 
57  protected:
58   testing::StrictMock<MockPage> page_;
59 
60  private:
61   // NOTE: The initialization order of these members matters.
62   content::BrowserTaskEnvironment task_environment_;
63   TestingProfile profile_;
64 
65   testing::NiceMock<content::MockDownloadManager> manager_;
66   content::TestWebUI web_ui_;
67 };
68 
TEST_F(DownloadsDOMHandlerTest,ChecksForRemovedFiles)69 TEST_F(DownloadsDOMHandlerTest, ChecksForRemovedFiles) {
70   EXPECT_CALL(*manager(), CheckForHistoryFilesRemoval());
71   TestDownloadsDOMHandler handler(page_.BindAndGetRemote(), manager(),
72                                   web_ui());
73 
74   testing::Mock::VerifyAndClear(manager());
75 
76   EXPECT_CALL(*manager(), CheckForHistoryFilesRemoval());
77 }
78 
TEST_F(DownloadsDOMHandlerTest,HandleGetDownloads)79 TEST_F(DownloadsDOMHandlerTest, HandleGetDownloads) {
80   TestDownloadsDOMHandler handler(page_.BindAndGetRemote(), manager(),
81                                   web_ui());
82 
83   handler.GetDownloads(std::vector<std::string>());
84 
85   EXPECT_CALL(page_, InsertItems(0, testing::_));
86 }
87 
TEST_F(DownloadsDOMHandlerTest,ClearAll)88 TEST_F(DownloadsDOMHandlerTest, ClearAll) {
89   std::vector<download::DownloadItem*> downloads;
90 
91   // Safe, in-progress items should be passed over.
92   testing::StrictMock<download::MockDownloadItem> in_progress;
93   EXPECT_CALL(in_progress, IsDangerous()).WillOnce(testing::Return(false));
94   EXPECT_CALL(in_progress, IsMixedContent()).WillOnce(testing::Return(false));
95   EXPECT_CALL(in_progress, IsTransient()).WillOnce(testing::Return(false));
96   EXPECT_CALL(in_progress, GetState())
97       .WillOnce(testing::Return(download::DownloadItem::IN_PROGRESS));
98   downloads.push_back(&in_progress);
99 
100   // Dangerous items should be removed (regardless of state).
101   testing::StrictMock<download::MockDownloadItem> dangerous;
102   EXPECT_CALL(dangerous, IsDangerous()).WillOnce(testing::Return(true));
103   EXPECT_CALL(dangerous, Remove());
104   downloads.push_back(&dangerous);
105 
106   // Completed items should be marked as hidden from the shelf.
107   testing::StrictMock<download::MockDownloadItem> completed;
108   EXPECT_CALL(completed, IsDangerous()).WillOnce(testing::Return(false));
109   EXPECT_CALL(completed, IsMixedContent()).WillOnce(testing::Return(false));
110   EXPECT_CALL(completed, IsTransient()).WillRepeatedly(testing::Return(false));
111   EXPECT_CALL(completed, GetState())
112       .WillOnce(testing::Return(download::DownloadItem::COMPLETE));
113   EXPECT_CALL(completed, GetId()).WillOnce(testing::Return(1));
114   EXPECT_CALL(completed, UpdateObservers());
115   downloads.push_back(&completed);
116 
117   ASSERT_TRUE(DownloadItemModel(&completed).ShouldShowInShelf());
118 
119   TestDownloadsDOMHandler handler(page_.BindAndGetRemote(), manager(),
120                                   web_ui());
121   handler.RemoveDownloads(downloads);
122 
123   // Ensure |completed| has been "soft removed" (i.e. can be revived).
124   EXPECT_FALSE(DownloadItemModel(&completed).ShouldShowInShelf());
125 
126   // Make sure |completed| actually get removed when removals are "finalized".
127   EXPECT_CALL(*manager(), GetDownload(1)).WillOnce(testing::Return(&completed));
128   EXPECT_CALL(completed, Remove());
129   handler.FinalizeRemovals();
130 }
131