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

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

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