OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "base/memory/memory_coordinator_client_registry.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace base { |
| 12 |
| 13 namespace { |
| 14 |
| 15 class TestMemoryCoordinatorClient : public MemoryCoordinatorClient { |
| 16 public: |
| 17 void OnMemoryStateChange(MemoryState state) override { state_ = state; } |
| 18 |
| 19 void OnPurgeMemory() override { ++purge_count_; } |
| 20 |
| 21 MemoryState state() const { return state_; } |
| 22 size_t purge_count() const { return purge_count_; } |
| 23 |
| 24 private: |
| 25 MemoryState state_ = MemoryState::UNKNOWN; |
| 26 size_t purge_count_ = 0; |
| 27 }; |
| 28 |
| 29 void RunUntilIdle() { |
| 30 base::RunLoop loop; |
| 31 loop.RunUntilIdle(); |
| 32 } |
| 33 |
| 34 TEST(MemoryCoordinatorClientRegistryTest, NotifyStateChange) { |
| 35 MessageLoop loop; |
| 36 auto* registry = MemoryCoordinatorClientRegistry::GetInstance(); |
| 37 TestMemoryCoordinatorClient client; |
| 38 registry->Register(&client); |
| 39 registry->Notify(MemoryState::THROTTLED); |
| 40 RunUntilIdle(); |
| 41 ASSERT_EQ(MemoryState::THROTTLED, client.state()); |
| 42 registry->Unregister(&client); |
| 43 } |
| 44 |
| 45 TEST(MemoryCoordinatorClientRegistryTest, PurgeMemory) { |
| 46 MessageLoop loop; |
| 47 auto* registry = MemoryCoordinatorClientRegistry::GetInstance(); |
| 48 TestMemoryCoordinatorClient client; |
| 49 registry->Register(&client); |
| 50 registry->PurgeMemory(); |
| 51 RunUntilIdle(); |
| 52 ASSERT_EQ(1u, client.purge_count()); |
| 53 registry->Unregister(&client); |
| 54 } |
| 55 |
| 56 } // namespace |
| 57 |
| 58 } // namespace base |
OLD | NEW |