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 "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h"
17 bool ServerCallback(int* callback_count,
18 const base::CommandLine& command_line,
19 const base::FilePath& current_directory) {
24 bool ClientCallback(const base::CommandLine& command_line,
25 const base::FilePath& current_directory) {
32 TEST(ChromeProcessSingletonTest, Basic) {
33 base::ScopedTempDir profile_dir;
34 ASSERT_TRUE(profile_dir.CreateUniqueTempDir());
36 int callback_count = 0;
38 ChromeProcessSingleton ps1(
39 profile_dir.GetPath(),
40 base::Bind(&ServerCallback, base::Unretained(&callback_count)));
43 ChromeProcessSingleton ps2(profile_dir.GetPath(),
44 base::Bind(&ClientCallback));
47 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate();
49 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result);
50 ASSERT_EQ(0, callback_count);
52 result = ps2.NotifyOtherProcessOrCreate();
53 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result);
55 ASSERT_EQ(1, callback_count);
58 TEST(ChromeProcessSingletonTest, Lock) {
59 base::ScopedTempDir profile_dir;
60 ASSERT_TRUE(profile_dir.CreateUniqueTempDir());
62 int callback_count = 0;
64 ChromeProcessSingleton ps1(
65 profile_dir.GetPath(),
66 base::Bind(&ServerCallback, base::Unretained(&callback_count)));
68 ChromeProcessSingleton ps2(profile_dir.GetPath(),
69 base::Bind(&ClientCallback));
72 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate();
74 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result);
75 ASSERT_EQ(0, callback_count);
77 result = ps2.NotifyOtherProcessOrCreate();
78 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result);
80 ASSERT_EQ(0, callback_count);
82 ASSERT_EQ(1, callback_count);
85 #if defined(OS_WIN) && !defined(USE_AURA)
88 void ModalNotificationHandler(bool* flag) {
94 TEST(ChromeProcessSingletonTest, LockWithModalDialog) {
95 base::ScopedTempDir profile_dir;
96 ASSERT_TRUE(profile_dir.CreateUniqueTempDir());
98 int callback_count = 0;
99 bool called_modal_notification_handler = false;
101 ChromeProcessSingleton ps1(
102 profile_dir.GetPath(),
103 base::Bind(&ServerCallback, base::Unretained(&callback_count)));
104 ps1.SetModalDialogNotificationHandler(
105 base::Bind(&ModalNotificationHandler,
106 base::Unretained(&called_modal_notification_handler)));
108 ChromeProcessSingleton ps2(profile_dir.GetPath(),
109 base::Bind(&ClientCallback));
112 ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate();
114 ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result);
115 ASSERT_EQ(0, callback_count);
117 ASSERT_FALSE(called_modal_notification_handler);
118 result = ps2.NotifyOtherProcessOrCreate();
119 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result);
120 ASSERT_TRUE(called_modal_notification_handler);
122 ASSERT_EQ(0, callback_count);
123 ps1.SetModalDialogNotificationHandler(base::Closure());
125 // The notifications sent while a modal dialog was open were processed after
127 ASSERT_EQ(2, callback_count);
129 // And now that the handler was cleared notifications will still be handled.
130 result = ps2.NotifyOtherProcessOrCreate();
131 ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result);
132 ASSERT_EQ(3, callback_count);
134 #endif // defined(OS_WIN) && !defined(USE_AURA)