| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/mach_broker_mac.h" | 5 #include "chrome/browser/mach_broker_mac.h" |
| 6 | 6 |
| 7 #include "base/lock.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 9 |
| 9 class MachBrokerTest : public testing::Test { | 10 class MachBrokerTest : public testing::Test { |
| 10 public: | 11 public: |
| 12 // Helper function to acquire/release locks and call |PlaceholderForPid()|. |
| 13 void AddPlaceholderForPid(base::ProcessHandle pid) { |
| 14 AutoLock lock(broker_.GetLock()); |
| 15 broker_.AddPlaceholderForPid(pid); |
| 16 } |
| 17 |
| 18 // Helper function to acquire/release locks and call |FinalizePid()|. |
| 19 void FinalizePid(base::ProcessHandle pid, |
| 20 const MachBroker::MachInfo& mach_info) { |
| 21 AutoLock lock(broker_.GetLock()); |
| 22 broker_.FinalizePid(pid, mach_info); |
| 23 } |
| 24 |
| 25 protected: |
| 11 MachBroker broker_; | 26 MachBroker broker_; |
| 12 }; | 27 }; |
| 13 | 28 |
| 14 TEST_F(MachBrokerTest, Setter) { | 29 TEST_F(MachBrokerTest, Locks) { |
| 15 broker_.RegisterPid(1u, MachBroker::MachInfo().SetTask(2u)); | 30 // Acquire and release the locks. Nothing bad should happen. |
| 16 EXPECT_EQ(2u, broker_.TaskForPid(1)); | 31 AutoLock lock(broker_.GetLock()); |
| 32 } |
| 33 |
| 34 TEST_F(MachBrokerTest, AddPlaceholderAndFinalize) { |
| 35 // Add a placeholder for PID 1. |
| 36 AddPlaceholderForPid(1); |
| 37 EXPECT_EQ(0u, broker_.TaskForPid(1)); |
| 38 |
| 39 // Finalize PID 1. |
| 40 FinalizePid(1, MachBroker::MachInfo().SetTask(100u)); |
| 41 EXPECT_EQ(100u, broker_.TaskForPid(1)); |
| 42 |
| 43 // Should be no entry for PID 2. |
| 17 EXPECT_EQ(0u, broker_.TaskForPid(2)); | 44 EXPECT_EQ(0u, broker_.TaskForPid(2)); |
| 18 } | 45 } |
| 19 | 46 |
| 20 TEST_F(MachBrokerTest, Invalidate) { | 47 TEST_F(MachBrokerTest, Invalidate) { |
| 21 broker_.RegisterPid(1u, MachBroker::MachInfo().SetTask(2)); | 48 AddPlaceholderForPid(1); |
| 22 broker_.Invalidate(1u); | 49 FinalizePid(1, MachBroker::MachInfo().SetTask(100u)); |
| 50 |
| 51 EXPECT_EQ(100u, broker_.TaskForPid(1)); |
| 52 broker_.InvalidatePid(1u); |
| 23 EXPECT_EQ(0u, broker_.TaskForPid(1)); | 53 EXPECT_EQ(0u, broker_.TaskForPid(1)); |
| 24 } | 54 } |
| 55 |
| 56 TEST_F(MachBrokerTest, FinalizeUnknownPid) { |
| 57 // Finalizing an entry for an unknown pid should not add it to the map. |
| 58 FinalizePid(1u, MachBroker::MachInfo().SetTask(100u)); |
| 59 EXPECT_EQ(0u, broker_.TaskForPid(1u)); |
| 60 } |
| OLD | NEW |