| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/process_singleton.h" | 5 #include "chrome/browser/chrome_process_singleton.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 bool ServerCallback(int* callback_count, | 16 bool ServerCallback(int* callback_count, |
| 17 const CommandLine& command_line, | 17 const CommandLine& command_line, |
| 18 const base::FilePath& current_directory) { | 18 const base::FilePath& current_directory) { |
| 19 ++(*callback_count); | 19 ++(*callback_count); |
| 20 return true; | 20 return true; |
| 21 } | 21 } |
| 22 | 22 |
| 23 bool ClientCallback(const CommandLine& command_line, | 23 bool ClientCallback(const CommandLine& command_line, |
| 24 const base::FilePath& current_directory) { | 24 const base::FilePath& current_directory) { |
| 25 ADD_FAILURE(); | 25 ADD_FAILURE(); |
| 26 return false; | 26 return false; |
| 27 } | 27 } |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 TEST(ProcessSingletonWinTest, Basic) { | 31 TEST(ChromeProcessSingletonTest, Basic) { |
| 32 base::ScopedTempDir profile_dir; | 32 base::ScopedTempDir profile_dir; |
| 33 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | 33 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); |
| 34 | 34 |
| 35 int callback_count = 0; | 35 int callback_count = 0; |
| 36 | 36 |
| 37 ProcessSingleton ps1( | 37 ChromeProcessSingleton ps1( |
| 38 profile_dir.path(), | 38 profile_dir.path(), |
| 39 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | 39 base::Bind(&ServerCallback, base::Unretained(&callback_count))); |
| 40 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | 40 ps1.Unlock(); |
| 41 |
| 42 ChromeProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); |
| 43 ps2.Unlock(); |
| 41 | 44 |
| 42 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | 45 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); |
| 43 | 46 |
| 44 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | 47 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); |
| 45 ASSERT_EQ(0, callback_count); | 48 ASSERT_EQ(0, callback_count); |
| 46 | 49 |
| 47 result = ps2.NotifyOtherProcessOrCreate(); | 50 result = ps2.NotifyOtherProcessOrCreate(); |
| 48 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | 51 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); |
| 49 | 52 |
| 50 ASSERT_EQ(1, callback_count); | 53 ASSERT_EQ(1, callback_count); |
| 51 } | 54 } |
| 52 | 55 |
| 53 #if !defined(USE_AURA) | 56 TEST(ChromeProcessSingletonTest, Lock) { |
| 54 TEST(ProcessSingletonWinTest, Lock) { | |
| 55 base::ScopedTempDir profile_dir; | 57 base::ScopedTempDir profile_dir; |
| 56 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | 58 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); |
| 57 | 59 |
| 58 int callback_count = 0; | 60 int callback_count = 0; |
| 59 | 61 |
| 60 ProcessSingleton ps1( | 62 ChromeProcessSingleton ps1( |
| 61 profile_dir.path(), | 63 profile_dir.path(), |
| 62 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | 64 base::Bind(&ServerCallback, base::Unretained(&callback_count))); |
| 63 ps1.Lock(NULL); | |
| 64 | 65 |
| 65 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | 66 ChromeProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); |
| 67 ps2.Unlock(); |
| 66 | 68 |
| 67 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | 69 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); |
| 68 | 70 |
| 69 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | 71 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); |
| 70 ASSERT_EQ(0, callback_count); | 72 ASSERT_EQ(0, callback_count); |
| 71 | 73 |
| 72 result = ps2.NotifyOtherProcessOrCreate(); | 74 result = ps2.NotifyOtherProcessOrCreate(); |
| 73 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | 75 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); |
| 74 | 76 |
| 75 ASSERT_EQ(0, callback_count); | 77 ASSERT_EQ(0, callback_count); |
| 76 ps1.Unlock(); | 78 ps1.Unlock(); |
| 77 ASSERT_EQ(1, callback_count); | 79 ASSERT_EQ(1, callback_count); |
| 78 } | 80 } |
| 79 | 81 |
| 80 class TestableProcessSingleton : public ProcessSingleton { | 82 #if !defined(USE_AURA) |
| 81 public: | 83 namespace { |
| 82 TestableProcessSingleton(const base::FilePath& user_data_dir, | |
| 83 const NotificationCallback& notification_callback) | |
| 84 : ProcessSingleton(user_data_dir, notification_callback), | |
| 85 called_set_foreground_window_(false) {} | |
| 86 | 84 |
| 87 bool called_set_foreground_window() { return called_set_foreground_window_; } | 85 void SetForegroundWindowHandler(bool* flag, |
| 86 gfx::NativeWindow /* target_window */) { |
| 87 *flag = true; |
| 88 } |
| 88 | 89 |
| 89 protected: | 90 } // namespace |
| 90 virtual void DoSetForegroundWindow(HWND target_window) OVERRIDE { | |
| 91 called_set_foreground_window_ = true; | |
| 92 } | |
| 93 | 91 |
| 94 private: | 92 TEST(ChromeProcessSingletonTest, LockWithModalDialog) { |
| 95 bool called_set_foreground_window_; | |
| 96 }; | |
| 97 | |
| 98 TEST(ProcessSingletonWinTest, LockWithModalDialog) { | |
| 99 base::ScopedTempDir profile_dir; | 93 base::ScopedTempDir profile_dir; |
| 100 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | 94 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); |
| 101 | 95 |
| 102 int callback_count = 0; | 96 int callback_count = 0; |
| 97 bool called_set_foreground_window = false; |
| 103 | 98 |
| 104 TestableProcessSingleton ps1( | 99 ChromeProcessSingleton ps1( |
| 105 profile_dir.path(), | 100 profile_dir.path(), |
| 106 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | 101 base::Bind(&ServerCallback, base::Unretained(&callback_count)), |
| 107 ps1.Lock(::GetShellWindow()); | 102 base::Bind(&SetForegroundWindowHandler, |
| 103 base::Unretained(&called_set_foreground_window))); |
| 104 ps1.SetActiveModalDialog(::GetShellWindow()); |
| 108 | 105 |
| 109 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | 106 ChromeProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); |
| 107 ps2.Unlock(); |
| 110 | 108 |
| 111 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | 109 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); |
| 112 | 110 |
| 113 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | 111 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); |
| 114 ASSERT_EQ(0, callback_count); | 112 ASSERT_EQ(0, callback_count); |
| 115 | 113 |
| 116 ASSERT_FALSE(ps1.called_set_foreground_window()); | 114 ASSERT_FALSE(called_set_foreground_window); |
| 117 result = ps2.NotifyOtherProcessOrCreate(); | 115 result = ps2.NotifyOtherProcessOrCreate(); |
| 118 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | 116 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); |
| 119 ASSERT_TRUE(ps1.called_set_foreground_window()); | 117 ASSERT_TRUE(called_set_foreground_window); |
| 120 | 118 |
| 121 ASSERT_EQ(0, callback_count); | 119 ASSERT_EQ(0, callback_count); |
| 120 ps1.SetActiveModalDialog(NULL); |
| 122 ps1.Unlock(); | 121 ps1.Unlock(); |
| 123 // When a modal dialog is present, the new command-line invocation is silently | 122 // The notification sent while a modal dialog was present was silently |
| 124 // dropped. | 123 // dropped. |
| 125 ASSERT_EQ(0, callback_count); | 124 ASSERT_EQ(0, callback_count); |
| 125 |
| 126 // But now that the active modal dialog is NULL notifications will be handled. |
| 127 result = ps2.NotifyOtherProcessOrCreate(); |
| 128 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); |
| 129 ASSERT_EQ(1, callback_count); |
| 126 } | 130 } |
| 127 #endif // !defined(USE_AURA) | 131 #endif // !defined(USE_AURA) |
| OLD | NEW |