| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/process_singleton.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/command_line.h" | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 bool ServerCallback(int* callback_count, | |
| 17 const CommandLine& command_line, | |
| 18 const base::FilePath& current_directory) { | |
| 19 ++(*callback_count); | |
| 20 return true; | |
| 21 } | |
| 22 | |
| 23 bool ClientCallback(const CommandLine& command_line, | |
| 24 const base::FilePath& current_directory) { | |
| 25 ADD_FAILURE(); | |
| 26 return false; | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 TEST(ProcessSingletonWinTest, Basic) { | |
| 32 base::ScopedTempDir profile_dir; | |
| 33 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | |
| 34 | |
| 35 int callback_count = 0; | |
| 36 | |
| 37 ProcessSingleton ps1( | |
| 38 profile_dir.path(), | |
| 39 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | |
| 40 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | |
| 41 | |
| 42 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | |
| 43 | |
| 44 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | |
| 45 ASSERT_EQ(0, callback_count); | |
| 46 | |
| 47 result = ps2.NotifyOtherProcessOrCreate(); | |
| 48 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | |
| 49 | |
| 50 ASSERT_EQ(1, callback_count); | |
| 51 } | |
| 52 | |
| 53 #if !defined(USE_AURA) | |
| 54 TEST(ProcessSingletonWinTest, Lock) { | |
| 55 base::ScopedTempDir profile_dir; | |
| 56 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | |
| 57 | |
| 58 int callback_count = 0; | |
| 59 | |
| 60 ProcessSingleton ps1( | |
| 61 profile_dir.path(), | |
| 62 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | |
| 63 ps1.Lock(NULL); | |
| 64 | |
| 65 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | |
| 66 | |
| 67 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | |
| 68 | |
| 69 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | |
| 70 ASSERT_EQ(0, callback_count); | |
| 71 | |
| 72 result = ps2.NotifyOtherProcessOrCreate(); | |
| 73 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | |
| 74 | |
| 75 ASSERT_EQ(0, callback_count); | |
| 76 ps1.Unlock(); | |
| 77 ASSERT_EQ(1, callback_count); | |
| 78 } | |
| 79 | |
| 80 class TestableProcessSingleton : public ProcessSingleton { | |
| 81 public: | |
| 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 | |
| 87 bool called_set_foreground_window() { return called_set_foreground_window_; } | |
| 88 | |
| 89 protected: | |
| 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 | |
| 98 TEST(ProcessSingletonWinTest, LockWithModalDialog) { | |
| 99 base::ScopedTempDir profile_dir; | |
| 100 ASSERT_TRUE(profile_dir.CreateUniqueTempDir()); | |
| 101 | |
| 102 int callback_count = 0; | |
| 103 | |
| 104 TestableProcessSingleton ps1( | |
| 105 profile_dir.path(), | |
| 106 base::Bind(&ServerCallback, base::Unretained(&callback_count))); | |
| 107 ps1.Lock(::GetShellWindow()); | |
| 108 | |
| 109 ProcessSingleton ps2(profile_dir.path(), base::Bind(&ClientCallback)); | |
| 110 | |
| 111 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate(); | |
| 112 | |
| 113 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result); | |
| 114 ASSERT_EQ(0, callback_count); | |
| 115 | |
| 116 ASSERT_FALSE(ps1.called_set_foreground_window()); | |
| 117 result = ps2.NotifyOtherProcessOrCreate(); | |
| 118 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result); | |
| 119 ASSERT_TRUE(ps1.called_set_foreground_window()); | |
| 120 | |
| 121 ASSERT_EQ(0, callback_count); | |
| 122 ps1.Unlock(); | |
| 123 // When a modal dialog is present, the new command-line invocation is silently | |
| 124 // dropped. | |
| 125 ASSERT_EQ(0, callback_count); | |
| 126 } | |
| 127 #endif // !defined(USE_AURA) | |
| OLD | NEW |