From 12b5d06ea959032da7288b8b2b435adfc2c2cea3 Mon Sep 17 00:00:00 2001
From: "estade@chromium.org"
 <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Thu, 19 Feb 2009 21:59:32 +0000
Subject: save_package.cc: Move UI test special casing to a helper function.

This effectively mocks out prompt-spawning win_util calls and keeps untested lines of code to a minimum.
Review URL: http://codereview.chromium.org/21492

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10044 0039d316-1c4b-4281-b951-d872f2087c98
---
 chrome/browser/download/save_package.cc | 95 ++++++++++++++++++++++-----------
 chrome/common/temp_scaffolding_stubs.h  |  4 ++
 2 files changed, 68 insertions(+), 31 deletions(-)

(limited to 'chrome')

diff --git a/chrome/browser/download/save_package.cc b/chrome/browser/download/save_package.cc
index d26424e..32e2243 100644
--- a/chrome/browser/download/save_package.cc
+++ b/chrome/browser/download/save_package.cc
@@ -102,6 +102,53 @@ FilePath::StringType StripOrdinalNumber(
   return pure_file_name.substr(0, l_paren_index);
 }
 
+// In testing mode, |should_prompt_user| will be false, and we simply set the
+// final name as the suggested name. Otherwise we pop up a Save As dialog.
+bool SaveFileAsWithFilter(gfx::NativeView owner,
+                          const std::wstring& suggested_name,
+                          const std::wstring& filter,
+                          const std::wstring& def_ext,
+                          bool ignore_suggested_ext,
+                          unsigned* index,
+                          std::wstring* final_name,
+                          bool should_prompt_user) {
+// TODO(port): Until we have an equivalent call on other platforms, assume
+// |suggested_name| will work just fine.
+#if defined(OS_WIN)
+  if (should_prompt_user)
+    return win_util::SaveFileAsWithFilter(owner,
+                                          suggested_name,
+                                          filter,
+                                          def_ext,
+                                          ignore_suggested_ext,
+                                          index,
+                                          final_name);
+#elif defined(OS_POSIX)
+  NOTIMPLEMENTED();
+#endif
+
+  final_name->assign(suggested_name);
+  return true;
+}
+
+// As above, in testing mode, just assign |final_name| to be |suggested_name|.
+bool SaveFileAs(gfx::NativeView owner,
+                const std::wstring& suggested_name,
+                std::wstring* final_name,
+                bool should_prompt_user) {
+// TODO(port): Until we have an equivalent call on other platforms, assume
+// |suggested_name| will work just fine.
+#if defined(OS_WIN)
+  if (should_prompt_user)
+    return win_util::SaveFileAs(owner, suggested_name, final_name);
+#elif defined(OS_POSIX)
+  NOTIMPLEMENTED();
+#endif
+
+  final_name->assign(suggested_name);
+  return true;
+}
+
 }  // namespace
 
 SavePackage::SavePackage(WebContents* web_content,
@@ -959,7 +1006,6 @@ bool SavePackage::GetSaveInfo(const FilePath& suggest_name,
                               gfx::NativeView container_window,
                               SavePackageParam* param,
                               DownloadManager* download_manager) {
-#if defined(OS_WIN)
   // TODO(tc): It might be nice to move this code into the download
   // manager.  http://crbug.com/6025
 
@@ -975,29 +1021,24 @@ bool SavePackage::GetSaveInfo(const FilePath& suggest_name,
     filter[filter.size() - 1] = L'\0';
     filter[filter.size() - 2] = L'\0';
 
-    if (g_should_prompt_for_filename) {
-      // Since we take the suggested name from the web page's title, we want to
-      // ignore the file extension generated by SaveFileAsWithFilter, since it
-      // will always be ".htm".
-      std::wstring main_file_path;
-      bool success = win_util::SaveFileAsWithFilter(container_window,
-          suggest_name.value(), filter, L"htm", true, &index, &main_file_path);
-      param->saved_main_file_path = FilePath(main_file_path);
-      if (!success)
-        return false;
-    } else {
-      param->saved_main_file_path = suggest_name;
-    }
+    // Since we take the suggested name from the web page's title, we want to
+    // ignore the file extension generated by SaveFileAsWithFilter, since it
+    // will always be ".htm".
+    std::wstring main_file_path;
+    bool success = SaveFileAsWithFilter(container_window,
+        suggest_name.ToWStringHack(), filter, L"htm", true, &index,
+        &main_file_path, g_should_prompt_for_filename);
+    param->saved_main_file_path = FilePath::FromWStringHack(main_file_path);
+    if (!success)
+      return false;
   } else {
-    if (g_should_prompt_for_filename) {
-      std::wstring main_file_path;
-      if (!win_util::SaveFileAs(container_window, suggest_name.value(),
-                                &main_file_path))
-        param->saved_main_file_path = FilePath(main_file_path);
-        return false;
-    } else {
-      param->saved_main_file_path = suggest_name;
-    }
+    std::wstring main_file_path;
+    bool success = SaveFileAs(container_window, suggest_name.ToWStringHack(),
+                              &main_file_path, g_should_prompt_for_filename);
+    param->saved_main_file_path = FilePath::FromWStringHack(main_file_path);
+    if (!success)
+      return false;
+
     // Set save-as type to only-HTML if the contents of current tab can not be
     // saved as complete-HTML.
     index = 1;
@@ -1030,14 +1071,6 @@ bool SavePackage::GetSaveInfo(const FilePath& suggest_name,
   }
 
   return true;
-#elif defined(OS_POSIX)
-  // TODO(port): this function pops up a save file dialog and fills in the
-  // attributes in |param|. We need to do this on other platforms as well
-  // when we have some sort of equivalent to the win_util:: calls in this
-  // function.
-  NOTIMPLEMENTED();
-  return false;
-#endif
 }
 
 // Static
diff --git a/chrome/common/temp_scaffolding_stubs.h b/chrome/common/temp_scaffolding_stubs.h
index 79854333..44a7f21 100644
--- a/chrome/common/temp_scaffolding_stubs.h
+++ b/chrome/common/temp_scaffolding_stubs.h
@@ -537,6 +537,10 @@ class DownloadManager : public base::RefCountedThreadSafe<DownloadManager> {
     NOTIMPLEMENTED();
     return 0;
   }
+  void GenerateSafeFilename(const std::string& mime_type,
+                            FilePath* file_name) {
+    NOTIMPLEMENTED();
+  }
 };
 
 class TemplateURLFetcher {
-- 
cgit v1.1