Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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/settings/downloads_handler.h" | |
| 6 | |
| 7 #include "chrome/browser/download/chrome_download_manager_delegate.h" | |
| 8 #include "chrome/browser/download/download_prefs.h" | |
| 9 #include "chrome/common/pref_names.h" | |
| 10 #include "chrome/test/base/testing_profile.h" | |
| 11 #include "components/prefs/pref_service.h" | |
| 12 #include "content/public/browser/web_ui.h" | |
| 13 #include "content/public/test/mock_download_manager.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "content/public/test/test_web_ui.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace settings { | |
| 19 | |
| 20 class DownloadsHandlerTest : public testing::Test { | |
| 21 public: | |
| 22 DownloadsHandlerTest() | |
| 23 : download_manager_(new content::MockDownloadManager()), | |
| 24 chrome_download_manager_delegate_(&profile_), | |
| 25 handler_(&profile_) { | |
| 26 content::BrowserContext::SetDownloadManagerForTesting(&profile_, | |
| 27 download_manager_); | |
| 28 EXPECT_EQ(download_manager_, | |
| 29 content::BrowserContext::GetDownloadManager(&profile_)); | |
| 30 | |
| 31 EXPECT_CALL(*download_manager_, GetDelegate()) | |
| 32 .WillRepeatedly(testing::Return(&chrome_download_manager_delegate_)); | |
| 33 EXPECT_CALL(*download_manager_, Shutdown()); | |
| 34 | |
| 35 handler_.set_web_ui(&test_web_ui_); | |
| 36 } | |
| 37 | |
| 38 void SetUp() override { | |
| 39 EXPECT_TRUE(test_web_ui_.call_data().empty()); | |
| 40 | |
| 41 base::ListValue args; | |
| 42 handler()->HandleInitialize(&args); | |
| 43 | |
| 44 EXPECT_TRUE(handler()->IsJavascriptAllowed()); | |
| 45 VerifyAutoOpenDownloadsChangedCallback(); | |
| 46 | |
| 47 test_web_ui_.ClearTrackedCalls(); | |
| 48 } | |
| 49 | |
| 50 void VerifyAutoOpenDownloadsChangedCallback() { | |
| 51 EXPECT_EQ(1u, test_web_ui_.call_data().size()); | |
| 52 | |
| 53 auto& data = *(test_web_ui_.call_data().back()); | |
| 54 EXPECT_EQ("cr.webUIListenerCallback", data.function_name()); | |
| 55 std::string event; | |
| 56 ASSERT_TRUE(data.arg1()->GetAsString(&event)); | |
| 57 EXPECT_EQ("auto-open-downloads-changed", event); | |
| 58 bool auto_open_downloads = false; | |
| 59 ASSERT_TRUE(data.arg2()->GetAsBoolean(&auto_open_downloads)); | |
| 60 EXPECT_FALSE(auto_open_downloads); | |
| 61 } | |
| 62 | |
| 63 Profile* profile() { return &profile_; } | |
| 64 DownloadsHandler* handler() { return &handler_; } | |
| 65 | |
| 66 private: | |
| 67 content::TestBrowserThreadBundle thread_bundle_; | |
| 68 content::TestWebUI test_web_ui_; | |
| 69 TestingProfile profile_; | |
| 70 | |
| 71 // Must be allocated on the heap for proper shutdown. | |
|
Dan Beam
2017/02/23 03:06:32
why is this? shouldn't these be fairly easy to de
tommycli
2017/02/24 19:02:41
Hey I updated the comment. This object is destroye
| |
| 72 content::MockDownloadManager* download_manager_; | |
| 73 ChromeDownloadManagerDelegate chrome_download_manager_delegate_; | |
| 74 | |
| 75 DownloadsHandler handler_; | |
| 76 }; | |
| 77 | |
| 78 TEST_F(DownloadsHandlerTest, AutoOpenDownloads) { | |
| 79 // Touch the pref. | |
| 80 profile()->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, ""); | |
| 81 VerifyAutoOpenDownloadsChangedCallback(); | |
| 82 } | |
| 83 | |
| 84 } // namespace settings | |
| OLD | NEW |