Home | History | Annotate | Download | only in download
      1 // Copyright (c) 2010 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 "base/file_util.h"
      6 #include "base/path_service.h"
      7 #include "base/string_util.h"
      8 #include "base/test/test_file_util.h"
      9 #include "chrome/app/chrome_command_ids.h"
     10 #include "chrome/browser/net/url_request_mock_http_job.h"
     11 #include "chrome/browser/download/save_package.h"
     12 #include "chrome/common/chrome_paths.h"
     13 #include "chrome/test/automation/browser_proxy.h"
     14 #include "chrome/test/automation/tab_proxy.h"
     15 #include "chrome/test/ui/ui_test.h"
     16 #include "chrome/test/ui_test_utils.h"
     17 #include "net/url_request/url_request_test_util.h"
     18 
     19 namespace {
     20 
     21 const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
     22 
     23 const char* const kAppendedExtension =
     24 #if defined(OS_WIN)
     25     ".htm";
     26 #else
     27     ".html";
     28 #endif
     29 
     30 }  // namespace
     31 
     32 class SavePageTest : public UITest {
     33  protected:
     34   SavePageTest() : UITest() {}
     35 
     36   void CheckFile(const FilePath& generated_file,
     37                  const FilePath& expected_result_file) {
     38     FilePath expected_result_filepath = ui_test_utils::GetTestFilePath(
     39         FilePath(kTestDir), expected_result_file);
     40     ASSERT_TRUE(file_util::PathExists(expected_result_filepath));
     41     WaitForGeneratedFileAndCheck(generated_file,
     42                                  expected_result_filepath,
     43                                  false,  // Don't care whether they are equal.
     44                                  false,
     45                                  false);  // Don't delete file
     46   }
     47 
     48   virtual void SetUp() {
     49     UITest::SetUp();
     50     EXPECT_TRUE(file_util::CreateNewTempDirectory(FILE_PATH_LITERAL(""),
     51                                                   &save_dir_));
     52 
     53     download_dir_ = GetDownloadDirectory();
     54   }
     55 
     56   virtual void TearDown() {
     57     UITest::TearDown();
     58     file_util::DieFileDie(save_dir_, true);
     59   }
     60 
     61   FilePath save_dir_;
     62   FilePath download_dir_;
     63 };
     64 
     65 // This tests that a webpage with the title "test.exe" is saved as
     66 // "test.exe.htm".
     67 // We probably don't care to handle this on Linux or Mac.
     68 #if defined(OS_WIN)
     69 TEST_F(SavePageTest, CleanFilenameFromPageTitle) {
     70   const FilePath file_name(FILE_PATH_LITERAL("c.htm"));
     71   FilePath full_file_name =
     72       download_dir_.AppendASCII(std::string("test.exe") + kAppendedExtension);
     73   FilePath dir = download_dir_.AppendASCII("test.exe_files");
     74 
     75   GURL url = URLRequestMockHTTPJob::GetMockUrl(
     76       FilePath(kTestDir).Append(file_name));
     77   scoped_refptr<TabProxy> tab(GetActiveTab());
     78   ASSERT_TRUE(tab.get());
     79   ASSERT_TRUE(tab->NavigateToURL(url));
     80   WaitUntilTabCount(1);
     81 
     82   scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
     83   ASSERT_TRUE(browser.get());
     84   automation()->SavePackageShouldPromptUser(false);
     85   EXPECT_TRUE(browser->RunCommandAsync(IDC_SAVE_PAGE));
     86   EXPECT_TRUE(WaitForDownloadShelfVisible(browser.get()));
     87   automation()->SavePackageShouldPromptUser(true);
     88 
     89   CheckFile(full_file_name, file_name);
     90   EXPECT_TRUE(file_util::DieFileDie(full_file_name, false));
     91   EXPECT_TRUE(file_util::DieFileDie(dir, true));
     92 }
     93 #endif
     94