Fix build break
[chromium-blink-merge.git] / chrome / browser / process_singleton_win_unittest.cc
blob6484de1e9382437526fed209618c3e33a6ee174a
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"
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"
14 namespace {
16 bool ServerCallback(int* callback_count,
17 const CommandLine& command_line,
18 const base::FilePath& current_directory) {
19 ++(*callback_count);
20 return true;
23 bool ClientCallback(const CommandLine& command_line,
24 const base::FilePath& current_directory) {
25 ADD_FAILURE();
26 return false;
29 } // namespace
31 TEST(ProcessSingletonWinTest, Basic) {
32 base::ScopedTempDir profile_dir;
33 ASSERT_TRUE(profile_dir.CreateUniqueTempDir());
35 int callback_count = 0;
37 ProcessSingleton ps1(
38 profile_dir.path(),
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;
60 ProcessSingleton ps1(
61 profile_dir.path(),
62 base::Bind(&ServerCallback, base::Unretained(&callback_count)));
63 ps1.Lock(NULL);
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);
76 ps1.Unlock();
77 ASSERT_EQ(1, callback_count);
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) {}
87 bool called_set_foreground_window() { return called_set_foreground_window_; }
89 protected:
90 virtual void DoSetForegroundWindow(HWND target_window) OVERRIDE {
91 called_set_foreground_window_ = true;
94 private:
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(
105 profile_dir.path(),
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);
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);
127 #endif // !defined(USE_AURA)