| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/browser/process_singleton.h" |
| 6 |
| 7 #include <sys/types.h> |
| 8 #include <sys/wait.h> |
| 9 #include <signal.h> |
| 10 #include <unistd.h> |
| 11 #include <vector> |
| 12 #include <string> |
| 13 |
| 14 #include "base/logging.h" |
| 15 #include "base/path_service.h" |
| 16 #include "base/string_util.h" |
| 17 #include "base/thread.h" |
| 18 #include "chrome/browser/browser.h" |
| 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/common/chrome_constants.h" |
| 21 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/test/chrome_process_util.h" |
| 23 #include "chrome/test/ui/ui_test.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 class ProcessSingletonLinuxTest : public UITest { |
| 27 protected: |
| 28 // A helper method to call ProcessSingleton::NotifyOtherProcess(). |
| 29 // |url| will be added to CommandLine for current process, so that it can be |
| 30 // sent to browser process by ProcessSingleton::NotifyOtherProcess(). |
| 31 void NotifyOtherProcess(const std::string& url, bool expect_result) { |
| 32 FilePath user_data_dir; |
| 33 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 34 |
| 35 std::vector<std::string> old_argv = |
| 36 CommandLine::ForCurrentProcess()->argv(); |
| 37 std::vector<std::string> argv; |
| 38 argv.push_back(old_argv[0]); |
| 39 argv.push_back(url); |
| 40 |
| 41 CommandLine::Reset(); |
| 42 CommandLine::Init(argv); |
| 43 |
| 44 ProcessSingleton process_singleton(user_data_dir); |
| 45 |
| 46 if (expect_result) |
| 47 EXPECT_TRUE(process_singleton.NotifyOtherProcess()); |
| 48 else |
| 49 EXPECT_FALSE(process_singleton.NotifyOtherProcess()); |
| 50 } |
| 51 }; |
| 52 |
| 53 // Test if the socket file and symbol link created by ProcessSingletonLinux |
| 54 // are valid. When running this test, the ProcessSingleton object is already |
| 55 // initiated by UITest. So we just test against this existing object. |
| 56 TEST_F(ProcessSingletonLinuxTest, CheckSocketFile) { |
| 57 FilePath user_data_dir; |
| 58 FilePath path; |
| 59 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 60 |
| 61 path = user_data_dir.Append(chrome::kSingletonSocketFilename); |
| 62 |
| 63 struct stat statbuf; |
| 64 ASSERT_EQ(0, lstat(path.value().c_str(), &statbuf)); |
| 65 ASSERT_TRUE(S_ISLNK(statbuf.st_mode)); |
| 66 char buf[PATH_MAX + 1]; |
| 67 ssize_t len = readlink(path.value().c_str(), buf, PATH_MAX); |
| 68 ASSERT_GT(len, 0); |
| 69 buf[len] = '\0'; |
| 70 |
| 71 path = user_data_dir.Append(buf); |
| 72 ASSERT_EQ(0, lstat(path.value().c_str(), &statbuf)); |
| 73 ASSERT_TRUE(S_ISSOCK(statbuf.st_mode)); |
| 74 } |
| 75 |
| 76 // TODO(james.su@gmail.com): port following tests to Windows. |
| 77 // Test success case of NotifyOtherProcess(). |
| 78 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessSuccess) { |
| 79 std::string url("about:blank"); |
| 80 int original_tab_count = GetTabCount(); |
| 81 |
| 82 NotifyOtherProcess(url, true); |
| 83 EXPECT_EQ(original_tab_count + 1, GetTabCount()); |
| 84 EXPECT_EQ(url, GetActiveTabURL().spec()); |
| 85 } |
| 86 |
| 87 // Test failure case of NotifyOtherProcess(). |
| 88 TEST_F(ProcessSingletonLinuxTest, NotifyOtherProcessFailure) { |
| 89 base::ProcessId pid = ChromeBrowserProcessId(user_data_dir()); |
| 90 |
| 91 ASSERT_GT(pid, 1); |
| 92 |
| 93 // Block the browser process, then it'll be killed by |
| 94 // ProcessSingleton::NotifyOtherProcess(). |
| 95 kill(pid, SIGSTOP); |
| 96 |
| 97 // Wait for a while to make sure the browser process is actually stopped. |
| 98 // It's necessary when running with valgrind. |
| 99 sleep(1); |
| 100 |
| 101 std::string url("about:blank"); |
| 102 NotifyOtherProcess(url, false); |
| 103 |
| 104 // Wait for a while to make sure the browser process is actually killed. |
| 105 sleep(1); |
| 106 |
| 107 EXPECT_FALSE(IsBrowserRunning()); |
| 108 } |
| OLD | NEW |