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.
5 #include "chrome/browser/process_singleton.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"
16 bool ServerCallback(int* callback_count
,
17 const CommandLine
& command_line
,
18 const base::FilePath
& current_directory
) {
23 bool ClientCallback(const CommandLine
& command_line
,
24 const base::FilePath
& current_directory
) {
31 TEST(ProcessSingletonWinTest
, Basic
) {
32 base::ScopedTempDir profile_dir
;
33 ASSERT_TRUE(profile_dir
.CreateUniqueTempDir());
35 int callback_count
= 0;
39 base::Bind(&ServerCallback
, base::Unretained(&callback_count
)));
40 ProcessSingleton
ps2(profile_dir
.path(), base::Bind(&ClientCallback
));
42 ProcessSingleton::NotifyResult result
= ps1
.NotifyOtherProcessOrCreate();
44 ASSERT_EQ(ProcessSingleton::PROCESS_NONE
, result
);
45 ASSERT_EQ(0, callback_count
);
47 result
= ps2
.NotifyOtherProcessOrCreate();
48 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED
, result
);
50 ASSERT_EQ(1, callback_count
);
53 #if !defined(USE_AURA)
54 TEST(ProcessSingletonWinTest
, Lock
) {
55 base::ScopedTempDir profile_dir
;
56 ASSERT_TRUE(profile_dir
.CreateUniqueTempDir());
58 int callback_count
= 0;
62 base::Bind(&ServerCallback
, base::Unretained(&callback_count
)));
65 ProcessSingleton
ps2(profile_dir
.path(), base::Bind(&ClientCallback
));
67 ProcessSingleton::NotifyResult result
= ps1
.NotifyOtherProcessOrCreate();
69 ASSERT_EQ(ProcessSingleton::PROCESS_NONE
, result
);
70 ASSERT_EQ(0, callback_count
);
72 result
= ps2
.NotifyOtherProcessOrCreate();
73 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED
, result
);
75 ASSERT_EQ(0, callback_count
);
77 ASSERT_EQ(1, callback_count
);
80 class TestableProcessSingleton
: public ProcessSingleton
{
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) {}
87 bool called_set_foreground_window() { return called_set_foreground_window_
; }
90 virtual void DoSetForegroundWindow(HWND target_window
) OVERRIDE
{
91 called_set_foreground_window_
= true;
95 bool called_set_foreground_window_
;
98 TEST(ProcessSingletonWinTest
, LockWithModalDialog
) {
99 base::ScopedTempDir profile_dir
;
100 ASSERT_TRUE(profile_dir
.CreateUniqueTempDir());
102 int callback_count
= 0;
104 TestableProcessSingleton
ps1(
106 base::Bind(&ServerCallback
, base::Unretained(&callback_count
)));
107 ps1
.Lock(::GetShellWindow());
109 ProcessSingleton
ps2(profile_dir
.path(), base::Bind(&ClientCallback
));
111 ProcessSingleton::NotifyResult result
= ps1
.NotifyOtherProcessOrCreate();
113 ASSERT_EQ(ProcessSingleton::PROCESS_NONE
, result
);
114 ASSERT_EQ(0, callback_count
);
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());
121 ASSERT_EQ(0, callback_count
);
123 // When a modal dialog is present, the new command-line invocation is silently
125 ASSERT_EQ(0, callback_count
);
127 #endif // !defined(USE_AURA)