[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / chrome_process_singleton_win_unittest.cc
1 // Copyright 2013 The Chromium Authors
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/chrome_process_singleton.h"
6
7 #include "base/command_line.h"
8 #include "base/compiler_specific.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/functional/bind.h"
12 #include "base/functional/callback.h"
13 #include "build/build_config.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace {
17
18 bool ServerCallback(int* callback_count,
19                     const base::CommandLine& command_line,
20                     const base::FilePath& current_directory) {
21   ++(*callback_count);
22   return true;
23 }
24
25 bool ClientCallback(const base::CommandLine& command_line,
26                     const base::FilePath& current_directory) {
27   ADD_FAILURE();
28   return false;
29 }
30
31 }  // namespace
32
33 TEST(ChromeProcessSingletonTest, Basic) {
34   base::ScopedTempDir profile_dir;
35   ASSERT_TRUE(profile_dir.CreateUniqueTempDir());
36
37   int callback_count = 0;
38
39   ChromeProcessSingleton ps1(profile_dir.GetPath());
40   ps1.Unlock(
41       base::BindRepeating(&ServerCallback, base::Unretained(&callback_count)));
42
43   ChromeProcessSingleton ps2(profile_dir.GetPath());
44   ps2.Unlock(base::BindRepeating(&ClientCallback));
45
46   EXPECT_FALSE(ps1.IsSingletonInstanceForTesting());
47   EXPECT_FALSE(ps2.IsSingletonInstanceForTesting());
48
49   ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate();
50
51   ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result);
52   ASSERT_EQ(0, callback_count);
53
54   result = ps2.NotifyOtherProcessOrCreate();
55   ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result);
56
57   EXPECT_TRUE(ps1.IsSingletonInstanceForTesting());
58   EXPECT_FALSE(ps2.IsSingletonInstanceForTesting());
59
60   ASSERT_EQ(1, callback_count);
61 }
62
63 TEST(ChromeProcessSingletonTest, Lock) {
64   base::ScopedTempDir profile_dir;
65   ASSERT_TRUE(profile_dir.CreateUniqueTempDir());
66
67   int callback_count = 0;
68
69   ChromeProcessSingleton ps1(profile_dir.GetPath());
70
71   ChromeProcessSingleton ps2(profile_dir.GetPath());
72   ps2.Unlock(base::BindRepeating(&ClientCallback));
73
74   EXPECT_FALSE(ps1.IsSingletonInstanceForTesting());
75   EXPECT_FALSE(ps2.IsSingletonInstanceForTesting());
76
77   ProcessSingleton::NotifyResult result = ps1.NotifyOtherProcessOrCreate();
78
79   ASSERT_EQ(ProcessSingleton::PROCESS_NONE, result);
80   ASSERT_EQ(0, callback_count);
81
82   result = ps2.NotifyOtherProcessOrCreate();
83   ASSERT_EQ(ProcessSingleton::PROCESS_NOTIFIED, result);
84
85   ASSERT_EQ(0, callback_count);
86   ps1.Unlock(
87       base::BindRepeating(&ServerCallback, base::Unretained(&callback_count)));
88   ASSERT_EQ(1, callback_count);
89
90   EXPECT_TRUE(ps1.IsSingletonInstanceForTesting());
91   EXPECT_FALSE(ps2.IsSingletonInstanceForTesting());
92 }