Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / mach_broker_mac_unittest.cc
blob9ba50c3109022be8757370123d8e38c2ba819fde
1 // Copyright (c) 2009 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 "content/browser/mach_broker_mac.h"
7 #include "base/synchronization/lock.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace content {
12 class MachBrokerTest : public testing::Test {
13 public:
14 // Helper function to acquire/release locks and call |PlaceholderForPid()|.
15 void AddPlaceholderForPid(base::ProcessHandle pid, int child_process_id) {
16 base::AutoLock lock(broker_.GetLock());
17 broker_.AddPlaceholderForPid(pid, child_process_id);
20 void InvalidateChildProcessId(int child_process_id) {
21 broker_.InvalidateChildProcessId(child_process_id);
24 int GetChildProcessCount(int child_process_id) {
25 return broker_.child_process_id_map_.count(child_process_id);
28 // Helper function to acquire/release locks and call |FinalizePid()|.
29 void FinalizePid(base::ProcessHandle pid,
30 mach_port_t task_port) {
31 base::AutoLock lock(broker_.GetLock());
32 broker_.FinalizePid(pid, task_port);
35 protected:
36 MachBroker broker_;
39 TEST_F(MachBrokerTest, Locks) {
40 // Acquire and release the locks. Nothing bad should happen.
41 base::AutoLock lock(broker_.GetLock());
44 TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) {
45 // Add a placeholder for PID 1.
46 AddPlaceholderForPid(1, 1);
47 EXPECT_EQ(0u, broker_.TaskForPid(1));
49 // Finalize PID 1.
50 FinalizePid(1, 100u);
51 EXPECT_EQ(100u, broker_.TaskForPid(1));
53 // Should be no entry for PID 2.
54 EXPECT_EQ(0u, broker_.TaskForPid(2));
57 TEST_F(MachBrokerTest, InvalidateChildProcessId) {
58 // Add a placeholder for PID 1 and child process id 50.
59 AddPlaceholderForPid(1, 50);
60 FinalizePid(1, 100u);
62 EXPECT_EQ(100u, broker_.TaskForPid(1));
63 InvalidateChildProcessId(50);
64 EXPECT_EQ(0u, broker_.TaskForPid(1));
67 TEST_F(MachBrokerTest, ValidateChildProcessIdMap) {
68 // Add a placeholder for PID 1 and child process id 50.
69 AddPlaceholderForPid(1, 50);
70 FinalizePid(1, 100u);
72 EXPECT_EQ(1, GetChildProcessCount(50));
73 InvalidateChildProcessId(50);
74 EXPECT_EQ(0, GetChildProcessCount(50));
77 TEST_F(MachBrokerTest, FinalizeUnknownPid) {
78 // Finalizing an entry for an unknown pid should not add it to the map.
79 FinalizePid(1u, 100u);
80 EXPECT_EQ(0u, broker_.TaskForPid(1u));
83 } // namespace content