Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: chrome/browser/extensions/api/extension_action/browser_action_apitest.cc

Issue 2407513002: Using ScopedTempDir as the download directory used by the DownloadViaPost test. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/files/scoped_temp_dir.h"
7 #include "base/macros.h" 8 #include "base/macros.h"
8 #include "build/build_config.h" 9 #include "build/build_config.h"
9 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
10 #include "chrome/browser/extensions/browser_action_test_util.h" 11 #include "chrome/browser/extensions/browser_action_test_util.h"
11 #include "chrome/browser/extensions/extension_action.h" 12 #include "chrome/browser/extensions/extension_action.h"
12 #include "chrome/browser/extensions/extension_action_icon_factory.h" 13 #include "chrome/browser/extensions/extension_action_icon_factory.h"
13 #include "chrome/browser/extensions/extension_action_manager.h" 14 #include "chrome/browser/extensions/extension_action_manager.h"
14 #include "chrome/browser/extensions/extension_action_runner.h" 15 #include "chrome/browser/extensions/extension_action_runner.h"
15 #include "chrome/browser/extensions/extension_apitest.h" 16 #include "chrome/browser/extensions/extension_apitest.h"
16 #include "chrome/browser/extensions/extension_tab_util.h" 17 #include "chrome/browser/extensions/extension_tab_util.h"
17 #include "chrome/browser/extensions/extension_util.h" 18 #include "chrome/browser/extensions/extension_util.h"
18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h" 20 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_commands.h" 21 #include "chrome/browser/ui/browser_commands.h"
21 #include "chrome/browser/ui/browser_finder.h" 22 #include "chrome/browser/ui/browser_finder.h"
22 #include "chrome/browser/ui/browser_navigator_params.h" 23 #include "chrome/browser/ui/browser_navigator_params.h"
23 #include "chrome/browser/ui/browser_window.h" 24 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/tabs/tab_strip_model.h" 25 #include "chrome/browser/ui/tabs/tab_strip_model.h"
25 #include "chrome/common/extensions/extension_process_policy.h" 26 #include "chrome/common/extensions/extension_process_policy.h"
27 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h" 28 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/ui_test_utils.h" 29 #include "chrome/test/base/ui_test_utils.h"
30 #include "components/prefs/pref_service.h"
28 #include "content/public/browser/browser_context.h" 31 #include "content/public/browser/browser_context.h"
29 #include "content/public/browser/notification_service.h" 32 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/render_frame_host.h" 33 #include "content/public/browser/render_frame_host.h"
31 #include "content/public/browser/web_contents.h" 34 #include "content/public/browser/web_contents.h"
32 #include "content/public/test/browser_test_utils.h" 35 #include "content/public/test/browser_test_utils.h"
33 #include "content/public/test/content_browser_test_utils.h" 36 #include "content/public/test/content_browser_test_utils.h"
34 #include "content/public/test/download_test_observer.h" 37 #include "content/public/test/download_test_observer.h"
35 #include "content/public/test/test_utils.h" 38 #include "content/public/test/test_utils.h"
36 #include "extensions/browser/extension_registry.h" 39 #include "extensions/browser/extension_registry.h"
37 #include "extensions/browser/extension_system.h" 40 #include "extensions/browser/extension_system.h"
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 const GURL& target_url, 847 const GURL& target_url,
845 ExpectedNavigationStatus expected_navigation_status) { 848 ExpectedNavigationStatus expected_navigation_status) {
846 std::string navigation_starting_script = 849 std::string navigation_starting_script =
847 "var form = document.getElementById('form');\n" 850 "var form = document.getElementById('form');\n"
848 "form.action = '" + target_url.spec() + "';\n" 851 "form.action = '" + target_url.spec() + "';\n"
849 "form.submit();\n"; 852 "form.submit();\n";
850 TestPopupNavigation(target_url, expected_navigation_status, 853 TestPopupNavigation(target_url, expected_navigation_status,
851 navigation_starting_script); 854 navigation_starting_script);
852 } 855 }
853 856
857 // Must be called after browser creation. Creates a temporary
858 // directory for downloads that is auto-deleted on destruction.
859 // Returning false indicates a failure of the function, and should be asserted
860 // in the caller.
861 bool CreateAndSetDownloadsDirectory(Browser* browser) {
862 if (!browser)
863 return false;
864
865 if (!downloads_directory_.CreateUniqueTempDir())
866 return false;
867
868 browser->profile()->GetPrefs()->SetFilePath(
869 prefs::kDownloadDefaultDirectory, downloads_directory_.GetPath());
870 browser->profile()->GetPrefs()->SetFilePath(
871 prefs::kSaveFileDefaultDirectory, downloads_directory_.GetPath());
872
873 return true;
874 }
875
854 private: 876 private:
855 void TestPopupNavigation(const GURL& target_url, 877 void TestPopupNavigation(const GURL& target_url,
856 ExpectedNavigationStatus expected_navigation_status, 878 ExpectedNavigationStatus expected_navigation_status,
857 std::string navigation_starting_script) { 879 std::string navigation_starting_script) {
858 // Were there any failures so far (e.g. in SetUpOnMainThread)? 880 // Were there any failures so far (e.g. in SetUpOnMainThread)?
859 ASSERT_FALSE(HasFailure()); 881 ASSERT_FALSE(HasFailure());
860 882
861 // Simulate a click on the browser action to open the popup. 883 // Simulate a click on the browser action to open the popup.
862 WebContents* popup = OpenPopup(0); 884 WebContents* popup = OpenPopup(0);
863 ASSERT_TRUE(popup); 885 ASSERT_TRUE(popup);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
918 for (int i = 0; i < tabs->count(); i++) { 940 for (int i = 0; i < tabs->count(); i++) {
919 WebContents* tab_contents = tabs->GetWebContentsAt(i); 941 WebContents* tab_contents = tabs->GetWebContentsAt(i);
920 WaitForLoadStop(tab_contents); 942 WaitForLoadStop(tab_contents);
921 EXPECT_NE(target_url, tab_contents->GetLastCommittedURL()) 943 EXPECT_NE(target_url, tab_contents->GetLastCommittedURL())
922 << "Navigating an extension pop-up should not affect tabs."; 944 << "Navigating an extension pop-up should not affect tabs.";
923 } 945 }
924 } 946 }
925 947
926 const Extension* popup_extension_; 948 const Extension* popup_extension_;
927 const Extension* other_extension_; 949 const Extension* other_extension_;
950 base::ScopedTempDir downloads_directory_;
928 }; 951 };
929 952
930 // Tests that an extension pop-up cannot be navigated to a web page. 953 // Tests that an extension pop-up cannot be navigated to a web page.
931 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, Webpage) { 954 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, Webpage) {
932 GURL web_url(embedded_test_server()->GetURL("foo.com", "/title1.html")); 955 GURL web_url(embedded_test_server()->GetURL("foo.com", "/title1.html"));
933 956
934 // With and without --isolate-extension the GET request will be blocked in 957 // With and without --isolate-extension the GET request will be blocked in
935 // ExtensionViewHost::OpenURLFromTab (which silently drops navigations with 958 // ExtensionViewHost::OpenURLFromTab (which silently drops navigations with
936 // CURRENT_TAB disposition). 959 // CURRENT_TAB disposition).
937 TestPopupNavigationViaGet(web_url, EXPECTING_NAVIGATION_FAILURE); 960 TestPopupNavigationViaGet(web_url, EXPECTING_NAVIGATION_FAILURE);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 // works: No navigation, but download shelf visible + download goes through. 1007 // works: No navigation, but download shelf visible + download goes through.
985 // 1008 //
986 // Note - there is no "...ViaGet" flavour of this test, because we don't care 1009 // Note - there is no "...ViaGet" flavour of this test, because we don't care
987 // (yet) if GET succeeds with the download or not (it probably should succeed 1010 // (yet) if GET succeeds with the download or not (it probably should succeed
988 // for consistency with POST, but it always failed in M54 and before). After 1011 // for consistency with POST, but it always failed in M54 and before). After
989 // abandoing ShouldFork/OpenURL for all methods (not just for POST) [see comment 1012 // abandoing ShouldFork/OpenURL for all methods (not just for POST) [see comment
990 // about https://crbug.com/646261 in ChromeContentRendererClient::ShouldFork] 1013 // about https://crbug.com/646261 in ChromeContentRendererClient::ShouldFork]
991 // GET should automagically start working for downloads. 1014 // GET should automagically start working for downloads.
992 // TODO(lukasza): https://crbug.com/650694: Add a "Get" flavour of the test once 1015 // TODO(lukasza): https://crbug.com/650694: Add a "Get" flavour of the test once
993 // the download works both for GET and POST requests. 1016 // the download works both for GET and POST requests.
994 1017 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest, DownloadViaPost) {
995 // Disabled on Windows. See http://crbug.com/653856. 1018 ASSERT_TRUE(CreateAndSetDownloadsDirectory(browser()));
Devlin 2016/10/07 21:11:38 I don't feel too strongly, but if this is only use
Łukasz Anforowicz 2016/10/07 23:32:02 Hmmm... not sure. Arguments for inlining: - More
Devlin 2016/10/10 14:38:50 Another option: create a method in the anonymous n
Łukasz Anforowicz 2016/10/10 15:08:57 Good idea - thanks. Done.
996 #if defined(OS_WIN)
997 #define MAYBE_DownloadViaPost DISABLED_DownloadViaPost
998 #else
999 #define MAYBE_DownloadViaPost DownloadViaPost
1000 #endif
1001
1002 IN_PROC_BROWSER_TEST_F(NavigatingExtensionPopupBrowserTest,
1003 MAYBE_DownloadViaPost) {
1004 content::DownloadTestObserverTerminal downloads_observer( 1019 content::DownloadTestObserverTerminal downloads_observer(
1005 content::BrowserContext::GetDownloadManager(browser()->profile()), 1020 content::BrowserContext::GetDownloadManager(browser()->profile()),
1006 1, // == wait_count (only waiting for "download-test3.gif"). 1021 1, // == wait_count (only waiting for "download-test3.gif").
1007 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL); 1022 content::DownloadTestObserver::ON_DANGEROUS_DOWNLOAD_FAIL);
1008 1023
1009 // Navigate to a URL that replies with 1024 // Navigate to a URL that replies with
1010 // Content-Disposition: attachment; filename=... 1025 // Content-Disposition: attachment; filename=...
1011 // header. 1026 // header.
1012 GURL download_url( 1027 GURL download_url(
1013 embedded_test_server()->GetURL("foo.com", "/download-test3.gif")); 1028 embedded_test_server()->GetURL("foo.com", "/download-test3.gif"));
1014 TestPopupNavigationViaPost(download_url, EXPECTING_NAVIGATION_FAILURE); 1029 TestPopupNavigationViaPost(download_url, EXPECTING_NAVIGATION_FAILURE);
1015 1030
1016 // Verify that "download-test3.gif got downloaded. 1031 // Verify that "download-test3.gif got downloaded.
1017 downloads_observer.WaitForFinished(); 1032 downloads_observer.WaitForFinished();
1018 EXPECT_EQ(0u, downloads_observer.NumDangerousDownloadsSeen()); 1033 EXPECT_EQ(0u, downloads_observer.NumDangerousDownloadsSeen());
1019 EXPECT_EQ(1u, downloads_observer.NumDownloadsSeenInState( 1034 EXPECT_EQ(1u, downloads_observer.NumDownloadsSeenInState(
1020 content::DownloadItem::COMPLETE)); 1035 content::DownloadItem::COMPLETE));
1021 1036
1022 // The test verification below is applicable only to scenarios where the 1037 // The test verification below is applicable only to scenarios where the
1023 // download shelf is supported - on ChromeOS, instead of the download shelf, 1038 // download shelf is supported - on ChromeOS, instead of the download shelf,
1024 // there is a download notification in the right-bottom corner of the screen. 1039 // there is a download notification in the right-bottom corner of the screen.
1025 #if !defined(OS_CHROMEOS) 1040 #if !defined(OS_CHROMEOS)
1026 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 1041 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
1027 #endif 1042 #endif
1028 } 1043 }
1029 1044
1030 } // namespace 1045 } // namespace
1031 } // namespace extensions 1046 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698