diff --git a/content/browser/web_contents/web_contents_view_mac_unittest.mm b/content/browser/web_contents/web_contents_view_mac_unittest.mm --- a/content/browser/web_contents/web_contents_view_mac_unittest.mm +++ b/content/browser/web_contents/web_contents_view_mac_unittest.mm @@ -148,6 +148,45 @@ TEST_F(WebContentsViewMacTest, DragPromisedFileTo_DownloadURL) { EXPECT_TRUE(result); EXPECT_EQ(target_path, actual_path); } + +// Regression test: when a file with the requested name already exists at the +// drop destination, CreateFileForDrop() picks -1. and the download +// is written there. DragPromisedFileTo() must return THAT path so the drop +// target (Finder, Mail, etc.) opens the freshly downloaded file rather than +// the pre-existing local file. +TEST_F(WebContentsViewMacTest, DragPromisedFileTo_ReturnsCollisionAdjustedPath) { + base::ScopedTempDir temp_dir; + ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); + + // Pre-create the collision file, simulating a pre-existing user document at + // the drop destination. + base::FilePath target_path = temp_dir.GetPath().AppendASCII("Resume.pdf"); + ASSERT_TRUE(base::WriteFile(target_path, "PRE-EXISTING USER DOCUMENT")); + + DropData drop_data; + drop_data.file_contents = "downloaded bytes"; + + base::FilePath actual_path; + remote_cocoa::mojom::WebContentsNSViewHost* host = view(); + bool result = host->DragPromisedFileTo(process()->GetID(), + main_test_rfh()->GetDocumentToken(), + target_path, drop_data, &actual_path); + EXPECT_TRUE(result); + + // CreateFileForDrop() must have chosen the -1 suffix because the original + // name was taken. + base::FilePath expected_path = + temp_dir.GetPath().AppendASCII("Resume-1.pdf"); + EXPECT_TRUE(base::PathExists(expected_path)) + << "download should have been written to the -1 suffixed file"; + + // BUG: line 653 of web_contents_view_mac.mm clobbers the out-param back to + // the original (colliding) path. Without the fix this expectation FAILS and + // |actual_path| == |target_path|, i.e. the path of the pre-existing file. + EXPECT_EQ(expected_path, actual_path); + EXPECT_NE(target_path, actual_path); +} } // namespace } // namespace content