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/chrome_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 base::CommandLine
& command_line
,
18 const base::FilePath
& current_directory
) {
23 bool ClientCallback(const base::CommandLine
& command_line
,
24 const base::FilePath
& current_directory
) {
31 TEST(ChromeProcessSingletonTest
, Basic
) {
32 base::ScopedTempDir profile_dir
;
33 ASSERT_TRUE(profile_dir
.CreateUniqueTempDir());
35 int callback_count
= 0;
37 ChromeProcessSingleton
ps1(
39 base::Bind(&ServerCallback
, base::Unretained(&callback_count
)));
42 ChromeProcessSingleton
ps2(profile_dir
.path(), base::Bind(&ClientCallback
));
45 ProcessSingleton::NotifyResult result
= ps1
.NotifyOtherProcessOrCreate();
47 ASSERT_EQ(ProcessSingleton::PROCESS_NONE
, result
);
48 ASSERT_EQ(0, callback_count
);
50 result
= ps2
.NotifyOtherProcessOrCreate();
51 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED
, result
);
53 ASSERT_EQ(1, callback_count
);
56 TEST(ChromeProcessSingletonTest
, Lock
) {
57 base::ScopedTempDir profile_dir
;
58 ASSERT_TRUE(profile_dir
.CreateUniqueTempDir());
60 int callback_count
= 0;
62 ChromeProcessSingleton
ps1(
64 base::Bind(&ServerCallback
, base::Unretained(&callback_count
)));
66 ChromeProcessSingleton
ps2(profile_dir
.path(), base::Bind(&ClientCallback
));
69 ProcessSingleton::NotifyResult result
= ps1
.NotifyOtherProcessOrCreate();
71 ASSERT_EQ(ProcessSingleton::PROCESS_NONE
, result
);
72 ASSERT_EQ(0, callback_count
);
74 result
= ps2
.NotifyOtherProcessOrCreate();
75 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED
, result
);
77 ASSERT_EQ(0, callback_count
);
79 ASSERT_EQ(1, callback_count
);
82 #if defined(OS_WIN) && !defined(USE_AURA)
85 void SetForegroundWindowHandler(bool* flag
,
86 gfx::NativeWindow
/* target_window */) {
92 TEST(ChromeProcessSingletonTest
, LockWithModalDialog
) {
93 base::ScopedTempDir profile_dir
;
94 ASSERT_TRUE(profile_dir
.CreateUniqueTempDir());
96 int callback_count
= 0;
97 bool called_set_foreground_window
= false;
99 ChromeProcessSingleton
ps1(
101 base::Bind(&ServerCallback
, base::Unretained(&callback_count
)),
102 base::Bind(&SetForegroundWindowHandler
,
103 base::Unretained(&called_set_foreground_window
)));
104 ps1
.SetActiveModalDialog(::GetShellWindow());
106 ChromeProcessSingleton
ps2(profile_dir
.path(), base::Bind(&ClientCallback
));
109 ProcessSingleton::NotifyResult result
= ps1
.NotifyOtherProcessOrCreate();
111 ASSERT_EQ(ProcessSingleton::PROCESS_NONE
, result
);
112 ASSERT_EQ(0, callback_count
);
114 ASSERT_FALSE(called_set_foreground_window
);
115 result
= ps2
.NotifyOtherProcessOrCreate();
116 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED
, result
);
117 ASSERT_TRUE(called_set_foreground_window
);
119 ASSERT_EQ(0, callback_count
);
120 ps1
.SetActiveModalDialog(NULL
);
122 // The notification sent while a modal dialog was present was silently
124 ASSERT_EQ(0, callback_count
);
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
);
131 #endif // defined(OS_WIN) && !defined(USE_AURA)