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

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: comment 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_ = coordinator_impl_.binding_.CreateInterfacePtrAndBind();
23 coordinator_impl_(mojo::GetProxy(&coordinator_), clients_) {} 21 }
24 22
25 void RegisterClient(MemoryCoordinatorClient* client) { 23 void RegisterClient(MemoryCoordinatorClient* client) {
26 clients_->AddObserver(client); 24 coordinator_impl_.RegisterClient(client);
27 } 25 }
28 26
29 void UnregisterClient(MemoryCoordinatorClient* client) { 27 void UnregisterClient(MemoryCoordinatorClient* client) {
30 clients_->RemoveObserver(client); 28 coordinator_impl_.UnregisterClient(client);
31 } 29 }
32 30
33 mojom::ChildMemoryCoordinatorPtr& coordinator() { return coordinator_; } 31 mojom::ChildMemoryCoordinatorPtr& coordinator() { return coordinator_; }
34 ChildMemoryCoordinatorImpl& coordinator_impl() { return coordinator_impl_; } 32 ChildMemoryCoordinatorImpl& coordinator_impl() { return coordinator_impl_; }
35 33
36 void ChangeState(mojom::MemoryState state) { 34 void ChangeState(mojom::MemoryState state) {
37 base::RunLoop loop; 35 base::RunLoop loop;
38 coordinator()->OnStateChange(state); 36 coordinator()->OnStateChange(state);
39 loop.RunUntilIdle(); 37 loop.RunUntilIdle();
40 } 38 }
41 39
42 protected:
43 scoped_refptr<ChildMemoryCoordinatorImpl::ClientList> clients_;
44
45 private: 40 private:
46 std::unique_ptr<base::MessageLoop> message_loop_; 41 std::unique_ptr<base::MessageLoop> message_loop_;
42 ChildMemoryCoordinatorImpl coordinator_impl_;
47 mojom::ChildMemoryCoordinatorPtr coordinator_ = nullptr; 43 mojom::ChildMemoryCoordinatorPtr coordinator_ = nullptr;
48 ChildMemoryCoordinatorImpl coordinator_impl_;
49 44
50 DISALLOW_COPY_AND_ASSIGN(ChildMemoryCoordinatorImplTest); 45 DISALLOW_COPY_AND_ASSIGN(ChildMemoryCoordinatorImplTest);
51 }; 46 };
52 47
48 namespace {
49
53 class MockMemoryCoordinatorClient final : public MemoryCoordinatorClient { 50 class MockMemoryCoordinatorClient final : public MemoryCoordinatorClient {
54 public: 51 public:
55 void OnMemoryStateChange(mojom::MemoryState state) override { 52 void OnMemoryStateChange(mojom::MemoryState state) override {
56 last_state_ = state; 53 last_state_ = state;
57 } 54 }
58 55
59 mojom::MemoryState last_state() const { return last_state_; } 56 mojom::MemoryState last_state() const { return last_state_; }
60 57
61 private: 58 private:
62 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN; 59 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN;
63 }; 60 };
64 61
65 class MemoryCoordinatorTestThread : public base::Thread, 62 class MemoryCoordinatorTestThread : public base::Thread,
66 public MemoryCoordinatorClient { 63 public MemoryCoordinatorClient {
67 public: 64 public:
68 MemoryCoordinatorTestThread( 65 MemoryCoordinatorTestThread(
69 const std::string& name, 66 const std::string& name,
70 scoped_refptr<ChildMemoryCoordinatorImpl::ClientList> clients) 67 ChildMemoryCoordinatorImpl& coordinator)
71 : Thread(name), clients_(clients) {} 68 : Thread(name), coordinator_(coordinator) {}
72 ~MemoryCoordinatorTestThread() override { Stop(); } 69 ~MemoryCoordinatorTestThread() override { Stop(); }
73 70
74 void Init() override { 71 void Init() override {
75 clients_->AddObserver(this); 72 coordinator_.RegisterClient(this);
76 } 73 }
77 74
78 void OnMemoryStateChange(mojom::MemoryState state) override { 75 void OnMemoryStateChange(mojom::MemoryState state) override {
79 EXPECT_EQ(message_loop(), base::MessageLoop::current()); 76 EXPECT_EQ(message_loop(), base::MessageLoop::current());
80 last_state_ = state; 77 last_state_ = state;
81 } 78 }
82 79
83 void CheckLastState(mojom::MemoryState state) { 80 void CheckLastState(mojom::MemoryState state) {
84 task_runner()->PostTask( 81 task_runner()->PostTask(
85 FROM_HERE, 82 FROM_HERE,
86 base::Bind(&MemoryCoordinatorTestThread::CheckLastStateInternal, 83 base::Bind(&MemoryCoordinatorTestThread::CheckLastStateInternal,
87 base::Unretained(this), state)); 84 base::Unretained(this), state));
88 } 85 }
89 86
90 private: 87 private:
91 void CheckLastStateInternal(mojom::MemoryState state) { 88 void CheckLastStateInternal(mojom::MemoryState state) {
92 base::RunLoop loop; 89 base::RunLoop loop;
93 loop.RunUntilIdle(); 90 loop.RunUntilIdle();
94 EXPECT_EQ(state, last_state_); 91 EXPECT_EQ(state, last_state_);
95 } 92 }
96 93
97 scoped_refptr<ChildMemoryCoordinatorImpl::ClientList> clients_; 94 ChildMemoryCoordinatorImpl& coordinator_;
98 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN; 95 mojom::MemoryState last_state_ = mojom::MemoryState::UNKNOWN;
99 }; 96 };
100 97
101 TEST_F(ChildMemoryCoordinatorImplTest, SingleClient) { 98 TEST_F(ChildMemoryCoordinatorImplTest, SingleClient) {
102 MockMemoryCoordinatorClient client; 99 MockMemoryCoordinatorClient client;
103 RegisterClient(&client); 100 RegisterClient(&client);
104 101
105 ChangeState(mojom::MemoryState::THROTTLED); 102 ChangeState(mojom::MemoryState::THROTTLED);
106 EXPECT_EQ(mojom::MemoryState::THROTTLED, client.last_state()); 103 EXPECT_EQ(mojom::MemoryState::THROTTLED, client.last_state());
107 104
108 ChangeState(mojom::MemoryState::NORMAL); 105 ChangeState(mojom::MemoryState::NORMAL);
109 EXPECT_EQ(mojom::MemoryState::NORMAL, client.last_state()); 106 EXPECT_EQ(mojom::MemoryState::NORMAL, client.last_state());
110 107
111 UnregisterClient(&client); 108 UnregisterClient(&client);
112 ChangeState(mojom::MemoryState::THROTTLED); 109 ChangeState(mojom::MemoryState::THROTTLED);
113 EXPECT_TRUE(mojom::MemoryState::THROTTLED != client.last_state()); 110 EXPECT_TRUE(mojom::MemoryState::THROTTLED != client.last_state());
114 } 111 }
115 112
116 TEST_F(ChildMemoryCoordinatorImplTest, MultipleClients) { 113 TEST_F(ChildMemoryCoordinatorImplTest, MultipleClients) {
117 MemoryCoordinatorTestThread t1("thread 1", clients_); 114 MemoryCoordinatorTestThread t1("thread 1", coordinator_impl());
118 MemoryCoordinatorTestThread t2("thread 2", clients_); 115 MemoryCoordinatorTestThread t2("thread 2", coordinator_impl());
119 116
120 t1.StartAndWaitForTesting(); 117 t1.StartAndWaitForTesting();
121 t2.StartAndWaitForTesting(); 118 t2.StartAndWaitForTesting();
122 119
123 ChangeState(mojom::MemoryState::THROTTLED); 120 ChangeState(mojom::MemoryState::THROTTLED);
124 t1.CheckLastState(mojom::MemoryState::THROTTLED); 121 t1.CheckLastState(mojom::MemoryState::THROTTLED);
125 t2.CheckLastState(mojom::MemoryState::THROTTLED); 122 t2.CheckLastState(mojom::MemoryState::THROTTLED);
126 123
127 ChangeState(mojom::MemoryState::NORMAL); 124 ChangeState(mojom::MemoryState::NORMAL);
128 t1.CheckLastState(mojom::MemoryState::NORMAL); 125 t1.CheckLastState(mojom::MemoryState::NORMAL);
129 t2.CheckLastState(mojom::MemoryState::NORMAL); 126 t2.CheckLastState(mojom::MemoryState::NORMAL);
130 127
131 t1.Stop(); 128 t1.Stop();
132 t2.Stop(); 129 t2.Stop();
133 } 130 }
134 131
135 } // namespace 132 } // namespace
136 133
137 } // namespace memory_coordinator 134 } // namespace memory_coordinator
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698