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