| 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/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 "chrome/browser/process_singleton_modal_dialog_lock.h" |
| 13 #include "chrome/browser/process_singleton_startup_lock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 15 |
| 14 namespace { | 16 namespace { |
| 15 | 17 |
| 16 bool ServerCallback(int* callback_count, | 18 bool ServerCallback(int* callback_count, |
| 17 const CommandLine& command_line, | 19 const CommandLine& command_line, |
| 18 const base::FilePath& current_directory) { | 20 const base::FilePath& current_directory) { |
| 19 ++(*callback_count); | 21 ++(*callback_count); |
| 20 return true; | 22 return true; |
| 21 } | 23 } |
| 22 | 24 |
| 23 bool ClientCallback(const CommandLine& command_line, | 25 bool ClientCallback(const CommandLine& command_line, |
| 24 const base::FilePath& current_directory) { | 26 const base::FilePath& current_directory) { |
| 25 ADD_FAILURE(); | 27 ADD_FAILURE(); |
| 26 return false; | 28 return false; |
| 27 } | 29 } |
| 28 | 30 |
| 29 } // namespace | 31 } // namespace |
| 30 | 32 |
| 31 TEST(ProcessSingletonWinTest, Basic) { | 33 TEST(ProcessSingletonWinTest, Basic) { |
| 32 base::ScopedTempDir profile_dir; | 34 base::ScopedTempDir profile_dir; |
| 33 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | 35 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); |
| 34 | 36 |
| 35 int callback_count = 0; | 37 int callback_count = 0; |
| 36 | 38 |
| 39 ProcessSingletonStartupLock startup_lock( |
| 40 base::Bind(&ServerCallback, base::Unretained(&callback_count))); |
| 41 ProcessSingletonModalDialogLock modal_dialog_lock( |
| 42 startup_lock.AsNotificationCallback()); |
| 37 ProcessSingleton ps1( | 43 ProcessSingleton ps1( |
| 38 profile_dir.path(), | 44 profile_dir.path(), modal_dialog_lock.AsNotificationCallback()); |
| 39 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | 45 startup_lock.Unlock(); |
| 46 |
| 40 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | 47 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); |
| 41 | 48 |
| 42 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | 49 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); |
| 43 | 50 |
| 44 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | 51 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); |
| 45 ASSERT_EQ(0, callback_count); | 52 ASSERT_EQ(0, callback_count); |
| 46 | 53 |
| 47 result = ps2.NotifyOtherProcessOrCreate(); | 54 result = ps2.NotifyOtherProcessOrCreate(); |
| 48 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | 55 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); |
| 49 | 56 |
| 50 ASSERT_EQ(1, callback_count); | 57 ASSERT_EQ(1, callback_count); |
| 51 } | 58 } |
| 52 | 59 |
| 53 #if !defined(USE_AURA) | |
| 54 TEST(ProcessSingletonWinTest, Lock) { | 60 TEST(ProcessSingletonWinTest, Lock) { |
| 55 base::ScopedTempDir profile_dir; | 61 base::ScopedTempDir profile_dir; |
| 56 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | 62 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); |
| 57 | 63 |
| 58 int callback_count = 0; | 64 int callback_count = 0; |
| 59 | 65 |
| 66 ProcessSingletonStartupLock startup_lock( |
| 67 base::Bind(&ServerCallback, base::Unretained(&callback_count))); |
| 68 ProcessSingletonModalDialogLock modal_dialog_lock( |
| 69 startup_lock.AsNotificationCallback()); |
| 60 ProcessSingleton ps1( | 70 ProcessSingleton ps1( |
| 61 profile_dir.path(), | 71 profile_dir.path(), modal_dialog_lock.AsNotificationCallback()); |
| 62 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | |
| 63 ps1.Lock(NULL); | |
| 64 | 72 |
| 65 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | 73 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); |
| 66 | 74 |
| 67 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | 75 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); |
| 68 | 76 |
| 69 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | 77 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); |
| 70 ASSERT_EQ(0, callback_count); | 78 ASSERT_EQ(0, callback_count); |
| 71 | 79 |
| 72 result = ps2.NotifyOtherProcessOrCreate(); | 80 result = ps2.NotifyOtherProcessOrCreate(); |
| 73 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | 81 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); |
| 74 | 82 |
| 75 ASSERT_EQ(0, callback_count); | 83 ASSERT_EQ(0, callback_count); |
| 76 ps1.Unlock(); | 84 startup_lock.Unlock(); |
| 77 ASSERT_EQ(1, callback_count); | 85 ASSERT_EQ(1, callback_count); |
| 78 } | 86 } |
| 79 | 87 |
| 80 class TestableProcessSingleton : public ProcessSingleton { | 88 #if !defined(USE_AURA) |
| 81 public: | 89 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 | 90 |
| 87 bool called_set_foreground_window() { return called_set_foreground_window_; } | 91 void SetForegroundWindowHandler(bool* flag, |
| 92 gfx::NativeWindow /* target_window */) { |
| 93 *flag = true; |
| 94 } |
| 88 | 95 |
| 89 protected: | 96 } // namespace |
| 90 virtual void DoSetForegroundWindow(HWND target_window) OVERRIDE { | |
| 91 called_set_foreground_window_ = true; | |
| 92 } | |
| 93 | |
| 94 private: | |
| 95 bool called_set_foreground_window_; | |
| 96 }; | |
| 97 | 97 |
| 98 TEST(ProcessSingletonWinTest, LockWithModalDialog) { | 98 TEST(ProcessSingletonWinTest, LockWithModalDialog) { |
| 99 base::ScopedTempDir profile_dir; | 99 base::ScopedTempDir profile_dir; |
| 100 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | 100 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); |
| 101 | 101 |
| 102 int callback_count = 0; | 102 int callback_count = 0; |
| 103 bool called_set_foreground_window = false; |
| 103 | 104 |
| 104 TestableProcessSingleton ps1( | 105 ProcessSingletonStartupLock startup_lock( |
| 105 profile_dir.path(), | |
| 106 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | 106 base::Bind(&ServerCallback, base::Unretained(&callback_count))); |
| 107 ps1.Lock(::GetShellWindow()); | 107 ProcessSingletonModalDialogLock modal_dialog_lock( |
| 108 startup_lock.AsNotificationCallback(), |
| 109 base::Bind(&SetForegroundWindowHandler, |
| 110 base::Unretained(&called_set_foreground_window))); |
| 111 ProcessSingleton ps1( |
| 112 profile_dir.path(), modal_dialog_lock.AsNotificationCallback()); |
| 113 modal_dialog_lock.SetActiveModalDialog(::GetShellWindow()); |
| 108 | 114 |
| 109 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | 115 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); |
| 110 | 116 |
| 111 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | 117 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); |
| 112 | 118 |
| 113 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | 119 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); |
| 114 ASSERT_EQ(0, callback_count); | 120 ASSERT_EQ(0, callback_count); |
| 115 | 121 |
| 116 ASSERT_FALSE(ps1.called_set_foreground_window()); | 122 ASSERT_FALSE(called_set_foreground_window); |
| 117 result = ps2.NotifyOtherProcessOrCreate(); | 123 result = ps2.NotifyOtherProcessOrCreate(); |
| 118 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | 124 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); |
| 119 ASSERT_TRUE(ps1.called_set_foreground_window()); | 125 ASSERT_TRUE(called_set_foreground_window); |
| 120 | 126 |
| 121 ASSERT_EQ(0, callback_count); | 127 ASSERT_EQ(0, callback_count); |
| 122 ps1.Unlock(); | 128 modal_dialog_lock.SetActiveModalDialog(NULL); |
| 129 startup_lock.Unlock(); |
| 123 // When a modal dialog is present, the new command-line invocation is silently | 130 // When a modal dialog is present, the new command-line invocation is silently |
| 124 // dropped. | 131 // dropped. |
| 125 ASSERT_EQ(0, callback_count); | 132 ASSERT_EQ(0, callback_count); |
| 126 } | 133 } |
| 127 #endif // !defined(USE_AURA) | 134 #endif // !defined(USE_AURA) |
| OLD | NEW |