Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: components/memory_coordinator/child/child_memory_coordinator_impl_unittest.cc

Issue 2094583002: Add MemoryCoordinator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/memory_coordinator/child/child_memory_coordinator_impl.h" 5 #include "components/memory_coordinator/child/child_memory_coordinator_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace memory_coordinator { 14 namespace memory_coordinator {
15 15
16 namespace {
17
18 class ChildMemoryCoordinatorImplTest : public testing::Test { 16 class ChildMemoryCoordinatorImplTest : public testing::Test {
19 public: 17 public:
20 ChildMemoryCoordinatorImplTest() 18 ChildMemoryCoordinatorImplTest()
21 : clients_(new ChildMemoryCoordinatorImpl::ClientList), 19 : message_loop_(new base::MessageLoop),
22 message_loop_(new base::MessageLoop), 20 coordinator_impl_(nullptr) {
23 coordinator_impl_(mojo::GetProxy(&coordinator_), clients_) {} 21 coordinator_ = coordinator_impl_.binding_.CreateInterfacePtrAndBind();
22 }
24 23
25 void RegisterClient(MemoryCoordinatorClient* client) { 24 void RegisterClient(MemoryCoordinatorClient* client) {
26 clients_->AddObserver(client); 25 coordinator_impl_.RegisterClient(client);
27 } 26 }
28 27
29 void UnregisterClient(MemoryCoordinatorClient* client) { 28 void UnregisterClient(MemoryCoordinatorClient* client) {
30 clients_->RemoveObserver(client); 29 coordinator_impl_.UnregisterClient(client);
31 } 30 }
32 31
33 mojom::ChildMemoryCoordinatorPtr& coordinator() { return coordinator_; } 32 mojom::ChildMemoryCoordinatorPtr& coordinator() { return coordinator_; }
34 ChildMemoryCoordinatorImpl& coordinator_impl() { return coordinator_impl_; } 33 ChildMemoryCoordinatorImpl& coordinator_impl() { return coordinator_impl_; }
35 34
36 void ChangeState(mojom::MemoryState state) { 35 void ChangeState(mojom::MemoryState state) {
37 base::RunLoop loop; 36 base::RunLoop loop;
38 coordinator()->OnStateChange(state); 37 coordinator()->OnStateChange(state);
39 loop.RunUntilIdle(); 38 loop.RunUntilIdle();
40 } 39 }
41 40
42 protected:
43 scoped_refptr<ChildMemoryCoordinatorImpl::ClientList> clients_;
44
45 private: 41 private:
46 std::unique_ptr<base::MessageLoop> message_loop_; 42 std::unique_ptr<base::MessageLoop> message_loop_;
43 ChildMemoryCoordinatorImpl coordinator_impl_;
47 mojom::ChildMemoryCoordinatorPtr coordinator_ = nullptr; 44 mojom::ChildMemoryCoordinatorPtr coordinator_ = nullptr;
48 ChildMemoryCoordinatorImpl coordinator_impl_;
49 45
50 DISALLOW_COPY_AND_ASSIGN(ChildMemoryCoordinatorImplTest); 46 DISALLOW_COPY_AND_ASSIGN(ChildMemoryCoordinatorImplTest);
51 }; 47 };
52 48
49 namespace {
50
53 class MockMemoryCoordinatorClient final : public MemoryCoordinatorClient { 51 class MockMemoryCoordinatorClient final : public MemoryCoordinatorClient {
54 public: 52 public:
55 void OnMemoryStateChange(mojom::MemoryState state) override { 53 void OnMemoryStateChange(mojom::MemoryState state) override {
56 last_state_ = state; 54 last_state_ = state;
57 } 55 }
58 56
59 mojom::MemoryState last_state() const { return last_state_; } 57 mojom::MemoryState last_state() const { return last_state_; }
60 58
61 private: 59 private:
62 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN; 60 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN;
63 }; 61 };
64 62
65 class MemoryCoordinatorTestThread : public base::Thread, 63 class MemoryCoordinatorTestThread : public base::Thread,
66 public MemoryCoordinatorClient { 64 public MemoryCoordinatorClient {
67 public: 65 public:
68 MemoryCoordinatorTestThread( 66 MemoryCoordinatorTestThread(
69 const std::string& name, 67 const std::string& name,
70 scoped_refptr<ChildMemoryCoordinatorImpl::ClientList> clients) 68 ChildMemoryCoordinatorImpl& coordinator)
71 : Thread(name), clients_(clients) {} 69 : Thread(name), coordinator_(coordinator) {}
72 ~MemoryCoordinatorTestThread() override { Stop(); } 70 ~MemoryCoordinatorTestThread() override { Stop(); }
73 71
74 void Init() override { 72 void Init() override {
75 clients_->AddObserver(this); 73 coordinator_.RegisterClient(this);
76 } 74 }
77 75
78 void OnMemoryStateChange(mojom::MemoryState state) override { 76 void OnMemoryStateChange(mojom::MemoryState state) override {
79 EXPECT_EQ(message_loop(), base::MessageLoop::current()); 77 EXPECT_EQ(message_loop(), base::MessageLoop::current());
80 last_state_ = state; 78 last_state_ = state;
81 } 79 }
82 80
83 void CheckLastState(mojom::MemoryState state) { 81 void CheckLastState(mojom::MemoryState state) {
84 task_runner()->PostTask( 82 task_runner()->PostTask(
85 FROM_HERE, 83 FROM_HERE,
86 base::Bind(&MemoryCoordinatorTestThread::CheckLastStateInternal, 84 base::Bind(&MemoryCoordinatorTestThread::CheckLastStateInternal,
87 base::Unretained(this), state)); 85 base::Unretained(this), state));
88 } 86 }
89 87
90 private: 88 private:
91 void CheckLastStateInternal(mojom::MemoryState state) { 89 void CheckLastStateInternal(mojom::MemoryState state) {
92 base::RunLoop loop; 90 base::RunLoop loop;
93 loop.RunUntilIdle(); 91 loop.RunUntilIdle();
94 EXPECT_EQ(state, last_state_); 92 EXPECT_EQ(state, last_state_);
95 } 93 }
96 94
97 scoped_refptr<ChildMemoryCoordinatorImpl::ClientList> clients_; 95 ChildMemoryCoordinatorImpl& coordinator_;
98 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN; 96 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN;
99 }; 97 };
100 98
101 TEST_F(ChildMemoryCoordinatorImplTest, SingleClient) { 99 TEST_F(ChildMemoryCoordinatorImplTest, SingleClient) {
102 MockMemoryCoordinatorClient client; 100 MockMemoryCoordinatorClient client;
103 RegisterClient(&client); 101 RegisterClient(&client);
104 102
105 ChangeState(mojom::MemoryState::THROTTLED); 103 ChangeState(mojom::MemoryState::THROTTLED);
106 EXPECT_EQ(mojom::MemoryState::THROTTLED, client.last_state()); 104 EXPECT_EQ(mojom::MemoryState::THROTTLED, client.last_state());
107 105
108 ChangeState(mojom::MemoryState::NORMAL); 106 ChangeState(mojom::MemoryState::NORMAL);
109 EXPECT_EQ(mojom::MemoryState::NORMAL, client.last_state()); 107 EXPECT_EQ(mojom::MemoryState::NORMAL, client.last_state());
110 108
111 UnregisterClient(&client); 109 UnregisterClient(&client);
112 ChangeState(mojom::MemoryState::THROTTLED); 110 ChangeState(mojom::MemoryState::THROTTLED);
113 EXPECT_TRUE(mojom::MemoryState::THROTTLED != client.last_state()); 111 EXPECT_TRUE(mojom::MemoryState::THROTTLED != client.last_state());
114 } 112 }
115 113
116 TEST_F(ChildMemoryCoordinatorImplTest, MultipleClients) { 114 TEST_F(ChildMemoryCoordinatorImplTest, MultipleClients) {
117 MemoryCoordinatorTestThread t1("thread 1", clients_); 115 MemoryCoordinatorTestThread t1("thread 1", coordinator_impl());
118 MemoryCoordinatorTestThread t2("thread 2", clients_); 116 MemoryCoordinatorTestThread t2("thread 2", coordinator_impl());
119 117
120 t1.StartAndWaitForTesting(); 118 t1.StartAndWaitForTesting();
121 t2.StartAndWaitForTesting(); 119 t2.StartAndWaitForTesting();
122 120
123 ChangeState(mojom::MemoryState::THROTTLED); 121 ChangeState(mojom::MemoryState::THROTTLED);
124 t1.CheckLastState(mojom::MemoryState::THROTTLED); 122 t1.CheckLastState(mojom::MemoryState::THROTTLED);
125 t2.CheckLastState(mojom::MemoryState::THROTTLED); 123 t2.CheckLastState(mojom::MemoryState::THROTTLED);
126 124
127 ChangeState(mojom::MemoryState::NORMAL); 125 ChangeState(mojom::MemoryState::NORMAL);
128 t1.CheckLastState(mojom::MemoryState::NORMAL); 126 t1.CheckLastState(mojom::MemoryState::NORMAL);
129 t2.CheckLastState(mojom::MemoryState::NORMAL); 127 t2.CheckLastState(mojom::MemoryState::NORMAL);
130 128
131 t1.Stop(); 129 t1.Stop();
132 t2.Stop(); 130 t2.Stop();
133 } 131 }
134 132
135 } // namespace 133 } // namespace
136 134
137 } // namespace memory_coordinator 135 } // namespace memory_coordinator
OLDNEW
« no previous file with comments | « components/memory_coordinator/child/child_memory_coordinator_impl.cc ('k') | components/memory_coordinator/common/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698