summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-12 10:29:04 +0000
committerphajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-03-12 10:29:04 +0000
commitaf71d648c150826278086efb7a718a9da5728cde (patch)
tree14cbfdd65ac3d7ac0caaa583a0293d2abba2fbe8 /chrome/browser
parentceefd3dc54b0126bec1298a129b0ca4361e7ab7b (diff)
downloadchromium_src-af71d648c150826278086efb7a718a9da5728cde.zip
chromium_src-af71d648c150826278086efb7a718a9da5728cde.tar.gz
chromium_src-af71d648c150826278086efb7a718a9da5728cde.tar.bz2
Add missing tests for Linux desktop shortcuts logic.
This also makes the code use EnvironmentVariableGetter instead of calling getenv directly because we need the control in tests. This is a follow-up after http://codereview.chromium.org/607001 TEST=ShellIntegrationTest.GetDesktopShortcutTemplate BUG=30785 Review URL: http://codereview.chromium.org/798002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41423 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc7
-rw-r--r--chrome/browser/shell_integration.h7
-rw-r--r--chrome/browser/shell_integration_linux.cc49
-rw-r--r--chrome/browser/shell_integration_unittest.cc106
-rw-r--r--chrome/browser/web_applications/web_app.cc8
5 files changed, 153 insertions, 24 deletions
diff --git a/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc b/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc
index b1ea27c..5641408 100644
--- a/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc
+++ b/chrome/browser/gtk/create_application_shortcuts_dialog_gtk.cc
@@ -5,6 +5,7 @@
#include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h"
#include "app/l10n_util.h"
+#include "base/linux_util.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/gtk/gtk_util.h"
#include "chrome/browser/shell_integration.h"
@@ -120,8 +121,12 @@ void CreateApplicationShortcutsDialogGtk::CreateDesktopShortcut(
const ShellIntegration::ShortcutInfo& shortcut_info) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
+ scoped_ptr<base::EnvironmentVariableGetter> env_getter(
+ base::EnvironmentVariableGetter::Create());
+
std::string shortcut_template;
- if (ShellIntegration::GetDesktopShortcutTemplate(&shortcut_template)) {
+ if (ShellIntegration::GetDesktopShortcutTemplate(env_getter.get(),
+ &shortcut_template)) {
ShellIntegration::CreateDesktopShortcut(shortcut_info,
shortcut_template);
Release();
diff --git a/chrome/browser/shell_integration.h b/chrome/browser/shell_integration.h
index 96e1dea..42adfd6 100644
--- a/chrome/browser/shell_integration.h
+++ b/chrome/browser/shell_integration.h
@@ -15,6 +15,10 @@
class FilePath;
+namespace base {
+class EnvironmentVariableGetter;
+}
+
class ShellIntegration {
public:
// Sets Chrome as default browser (only for current user). Returns false if
@@ -69,7 +73,8 @@ class ShellIntegration {
const string16& extension_app_id);
#if defined(USE_X11)
- static bool GetDesktopShortcutTemplate(std::string* output);
+ static bool GetDesktopShortcutTemplate(
+ base::EnvironmentVariableGetter* env_getter, std::string* output);
// Returns filename for .desktop file based on |url|, sanitized for security.
static FilePath GetDesktopShortcutFilename(const GURL& url);
diff --git a/chrome/browser/shell_integration_linux.cc b/chrome/browser/shell_integration_linux.cc
index 3b057b7..fdea1da 100644
--- a/chrome/browser/shell_integration_linux.cc
+++ b/chrome/browser/shell_integration_linux.cc
@@ -19,6 +19,7 @@
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/i18n/file_util_icu.h"
+#include "base/linux_util.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/process_util.h"
@@ -37,20 +38,17 @@
namespace {
-const char* GetDesktopName() {
+std::string GetDesktopName(base::EnvironmentVariableGetter* env_getter) {
#if defined(GOOGLE_CHROME_BUILD)
return "google-chrome.desktop";
#else // CHROMIUM_BUILD
- static const char* name = NULL;
- if (!name) {
- // Allow $CHROME_DESKTOP to override the built-in value, so that development
- // versions can set themselves as the default without interfering with
- // non-official, packaged versions using the built-in value.
- name = getenv("CHROME_DESKTOP");
- if (!name)
- name = "chromium-browser.desktop";
- }
- return name;
+ // Allow $CHROME_DESKTOP to override the built-in value, so that development
+ // versions can set themselves as the default without interfering with
+ // non-official, packaged versions using the built-in value.
+ std::string name;
+ if (env_getter->Getenv("CHROME_DESKTOP", &name) && !name.empty())
+ return name;
+ return "chromium-browser.desktop";
#endif
}
@@ -197,21 +195,27 @@ void CreateShortcutInApplicationsMenu(const FilePath& shortcut_filename,
// static
bool ShellIntegration::SetAsDefaultBrowser() {
+ scoped_ptr<base::EnvironmentVariableGetter> env_getter(
+ base::EnvironmentVariableGetter::Create());
+
std::vector<std::string> argv;
argv.push_back("xdg-settings");
argv.push_back("set");
argv.push_back("default-web-browser");
- argv.push_back(GetDesktopName());
+ argv.push_back(GetDesktopName(env_getter.get()));
return LaunchXdgUtility(argv);
}
// static
ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() {
+ scoped_ptr<base::EnvironmentVariableGetter> env_getter(
+ base::EnvironmentVariableGetter::Create());
+
std::vector<std::string> argv;
argv.push_back("xdg-settings");
argv.push_back("check");
argv.push_back("default-web-browser");
- argv.push_back(GetDesktopName());
+ argv.push_back(GetDesktopName(env_getter.get()));
std::string reply;
if (!base::GetAppOutput(CommandLine(argv), &reply)) {
@@ -237,19 +241,22 @@ bool ShellIntegration::IsFirefoxDefaultBrowser() {
}
// static
-bool ShellIntegration::GetDesktopShortcutTemplate(std::string* output) {
+bool ShellIntegration::GetDesktopShortcutTemplate(
+ base::EnvironmentVariableGetter* env_getter, std::string* output) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
std::vector<FilePath> search_paths;
- const char* xdg_data_home = getenv("XDG_DATA_HOME");
- if (xdg_data_home)
+ std::string xdg_data_home;
+ if (env_getter->Getenv("XDG_DATA_HOME", &xdg_data_home) &&
+ !xdg_data_home.empty()) {
search_paths.push_back(FilePath(xdg_data_home));
+ }
- const char* xdg_data_dirs = getenv("XDG_DATA_DIRS");
- if (xdg_data_dirs) {
- CStringTokenizer tokenizer(xdg_data_dirs,
- xdg_data_dirs + strlen(xdg_data_dirs), ":");
+ std::string xdg_data_dirs;
+ if (env_getter->Getenv("XDG_DATA_DIRS", &xdg_data_dirs) &&
+ !xdg_data_dirs.empty()) {
+ StringTokenizer tokenizer(xdg_data_dirs, ":");
while (tokenizer.GetNext()) {
FilePath data_dir(tokenizer.token());
search_paths.push_back(data_dir);
@@ -262,7 +269,7 @@ bool ShellIntegration::GetDesktopShortcutTemplate(std::string* output) {
search_paths.push_back(FilePath("/usr/share/applications"));
search_paths.push_back(FilePath("/usr/local/share/applications"));
- std::string template_filename(GetDesktopName());
+ std::string template_filename(GetDesktopName(env_getter));
for (std::vector<FilePath>::const_iterator i = search_paths.begin();
i != search_paths.end(); ++i) {
FilePath path = (*i).Append(template_filename);
diff --git a/chrome/browser/shell_integration_unittest.cc b/chrome/browser/shell_integration_unittest.cc
index cfbaed9..5714b31 100644
--- a/chrome/browser/shell_integration_unittest.cc
+++ b/chrome/browser/shell_integration_unittest.cc
@@ -4,8 +4,16 @@
#include "chrome/browser/shell_integration.h"
+#include <map>
+
#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/linux_util.h"
+#include "base/message_loop.h"
+#include "base/scoped_temp_dir.h"
+#include "base/stl_util-inl.h"
#include "base/string_util.h"
+#include "chrome/browser/chrome_thread.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths_internal.h"
#include "googleurl/src/gurl.h"
@@ -14,6 +22,104 @@
#define FPL FILE_PATH_LITERAL
#if defined(OS_LINUX)
+namespace {
+
+// Provides mock environment variables values based on a stored map.
+class MockEnvironmentVariableGetter : public base::EnvironmentVariableGetter {
+ public:
+ MockEnvironmentVariableGetter() {
+ }
+
+ void Set(const std::string& name, const std::string& value) {
+ variables_[name] = value;
+ }
+
+ virtual bool Getenv(const char* variable_name, std::string* result) {
+ if (ContainsKey(variables_, variable_name)) {
+ *result = variables_[variable_name];
+ return true;
+ }
+
+ return false;
+ }
+
+ private:
+ std::map<std::string, std::string> variables_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockEnvironmentVariableGetter);
+};
+
+} // namespace
+
+TEST(ShellIntegrationTest, GetDesktopShortcutTemplate) {
+#if defined(GOOGLE_CHROME_BUILD)
+ const char kTemplateFilename[] = "google-chrome.desktop";
+#else // CHROMIUM_BUILD
+ const char kTemplateFilename[] = "chromium-browser.desktop";
+#endif
+
+ const char kTestData1[] = "a magical testing string";
+ const char kTestData2[] = "a different testing string";
+
+ MessageLoop message_loop;
+ ChromeThread file_thread(ChromeThread::FILE, &message_loop);
+
+ {
+ ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+
+ MockEnvironmentVariableGetter env_getter;
+ env_getter.Set("XDG_DATA_HOME", temp_dir.path().value());
+ ASSERT_TRUE(file_util::WriteFile(
+ temp_dir.path().AppendASCII(kTemplateFilename),
+ kTestData1, strlen(kTestData1)));
+ std::string contents;
+ ASSERT_TRUE(ShellIntegration::GetDesktopShortcutTemplate(&env_getter,
+ &contents));
+ EXPECT_EQ(kTestData1, contents);
+ }
+
+ {
+ ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+
+ MockEnvironmentVariableGetter env_getter;
+ env_getter.Set("XDG_DATA_DIRS", temp_dir.path().value());
+ ASSERT_TRUE(file_util::CreateDirectory(
+ temp_dir.path().AppendASCII("applications")));
+ ASSERT_TRUE(file_util::WriteFile(
+ temp_dir.path().AppendASCII("applications")
+ .AppendASCII(kTemplateFilename),
+ kTestData2, strlen(kTestData2)));
+ std::string contents;
+ ASSERT_TRUE(ShellIntegration::GetDesktopShortcutTemplate(&env_getter,
+ &contents));
+ EXPECT_EQ(kTestData2, contents);
+ }
+
+ {
+ ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+
+ MockEnvironmentVariableGetter env_getter;
+ env_getter.Set("XDG_DATA_DIRS", temp_dir.path().value() + ":" +
+ temp_dir.path().AppendASCII("applications").value());
+ ASSERT_TRUE(file_util::CreateDirectory(
+ temp_dir.path().AppendASCII("applications")));
+ ASSERT_TRUE(file_util::WriteFile(
+ temp_dir.path().AppendASCII(kTemplateFilename),
+ kTestData1, strlen(kTestData1)));
+ ASSERT_TRUE(file_util::WriteFile(
+ temp_dir.path().AppendASCII("applications")
+ .AppendASCII(kTemplateFilename),
+ kTestData2, strlen(kTestData2)));
+ std::string contents;
+ ASSERT_TRUE(ShellIntegration::GetDesktopShortcutTemplate(&env_getter,
+ &contents));
+ EXPECT_EQ(kTestData1, contents);
+ }
+}
+
TEST(ShellIntegrationTest, GetDesktopShortcutFilename) {
const struct {
const FilePath::CharType* path;
diff --git a/chrome/browser/web_applications/web_app.cc b/chrome/browser/web_applications/web_app.cc
index 0a9f582..3767616 100644
--- a/chrome/browser/web_applications/web_app.cc
+++ b/chrome/browser/web_applications/web_app.cc
@@ -14,6 +14,7 @@
#include "base/callback.h"
#include "base/file_util.h"
+#include "base/linux_util.h"
#include "base/md5.h"
#include "base/message_loop.h"
#include "base/path_service.h"
@@ -256,9 +257,14 @@ bool CreateShortcutTask::CreateShortcut() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
#if defined(OS_LINUX)
+ scoped_ptr<base::EnvironmentVariableGetter> env_getter(
+ base::EnvironmentVariableGetter::Create());
+
std::string shortcut_template;
- if (!ShellIntegration::GetDesktopShortcutTemplate(&shortcut_template))
+ if (!ShellIntegration::GetDesktopShortcutTemplate(env_getter.get(),
+ &shortcut_template)) {
return false;
+ }
ShellIntegration::CreateDesktopShortcut(shortcut_info_, shortcut_template);
return true; // assuming always success.
#elif defined(OS_WIN)