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

Side by Side Diff: media/midi/midi_manager_unittest.cc

Issue 261263002: Web MIDI: add an unit test to check MidiManager instantiation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comment update Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "media/midi/midi_manager.h" 5 #include "media/midi/midi_manager.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 11
11 namespace media { 12 namespace media {
12 13
13 namespace { 14 namespace {
14 15
15 class FakeMidiManager : public MidiManager { 16 class FakeMidiManager : public MidiManager {
16 public: 17 public:
17 FakeMidiManager() 18 FakeMidiManager() : start_initialization_is_called_(false) {}
18 : start_initialization_is_called_(false),
19 complete_initialization_synchronously_(false) {}
20 virtual ~FakeMidiManager() {} 19 virtual ~FakeMidiManager() {}
21 20
22 // MidiManager implementation. 21 // MidiManager implementation.
23 virtual void StartInitialization() OVERRIDE { 22 virtual void StartInitialization() OVERRIDE {
24 start_initialization_is_called_ = true; 23 start_initialization_is_called_ = true;
25 if (complete_initialization_synchronously_)
26 CompleteInitialization(MIDI_OK);
27 } 24 }
28 25
29 virtual void DispatchSendMidiData(MidiManagerClient* client, 26 virtual void DispatchSendMidiData(MidiManagerClient* client,
30 uint32 port_index, 27 uint32 port_index,
31 const std::vector<uint8>& data, 28 const std::vector<uint8>& data,
32 double timestamp) OVERRIDE {} 29 double timestamp) OVERRIDE {}
33 30
34 // Utility functions for testing. 31 // Utility functions for testing.
35 void CallCompleteInitialization(MidiResult result) { 32 void CallCompleteInitialization(MidiResult result) {
36 CompleteInitialization(result); 33 CompleteInitialization(result);
37 } 34 }
38 35
39 size_t GetClientCount() { 36 size_t GetClientCount() {
40 return clients_.size(); 37 return clients_.size();
41 } 38 }
42 39
43 size_t GetPendingClientCount() { 40 size_t GetPendingClientCount() {
44 return pending_clients_.size(); 41 return pending_clients_.size();
45 } 42 }
46 43
47 bool start_initialization_is_called_; 44 bool start_initialization_is_called_;
48 bool complete_initialization_synchronously_;
49 45
50 private: 46 private:
51 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); 47 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager);
52 }; 48 };
53 49
54 class FakeMidiManagerClient : public MidiManagerClient { 50 class FakeMidiManagerClient : public MidiManagerClient {
55 public: 51 public:
56 FakeMidiManagerClient(int client_id) : client_id_(client_id) {} 52 explicit FakeMidiManagerClient(int client_id)
53 : client_id_(client_id),
54 result_(MIDI_NOT_SUPPORTED),
55 wait_for_result_(true) {}
57 virtual ~FakeMidiManagerClient() {} 56 virtual ~FakeMidiManagerClient() {}
58 57
59 // MidiManagerClient implementation. 58 // MidiManagerClient implementation.
60 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { 59 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE {
61 DCHECK_EQ(client_id_, client_id); 60 CHECK_EQ(client_id_, client_id);
62 result_ = result; 61 result_ = result;
62 wait_for_result_ = false;
63 } 63 }
64 64
65 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, 65 virtual void ReceiveMidiData(uint32 port_index, const uint8* data,
66 size_t size, double timestamp) OVERRIDE {} 66 size_t size, double timestamp) OVERRIDE {}
67 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} 67 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {}
68 68
69 int GetClientId() { 69 int GetClientId() const {
70 return client_id_; 70 return client_id_;
71 } 71 }
72 72
73 MidiResult GetResult() { 73 MidiResult GetResult() const {
74 return result_; 74 return result_;
75 } 75 }
76 76
77 MidiResult WaitForResult() {
78 base::RunLoop run_loop;
79 while (wait_for_result_)
yukawa 2014/05/05 04:07:36 Can we assume that |wait_for_result_| is modified
Takashi Toyoshima 2014/05/06 01:58:18 Done.
80 run_loop.RunUntilIdle();
81 return GetResult();
82 }
83
77 private: 84 private:
78 int client_id_; 85 int client_id_;
79 MidiResult result_; 86 MidiResult result_;
87 bool wait_for_result_;
80 88
81 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); 89 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
82 }; 90 };
83 91
84 class MidiManagerTest : public ::testing::Test { 92 class MidiManagerTest : public ::testing::Test {
85 public: 93 public:
86 MidiManagerTest() : manager_(new FakeMidiManager) {} 94 MidiManagerTest()
95 : message_loop_(new base::MessageLoop), manager_(new FakeMidiManager) {}
87 virtual ~MidiManagerTest() {} 96 virtual ~MidiManagerTest() {}
88 97
89 protected: 98 protected:
90 void StartTheFirstSession(FakeMidiManagerClient* client, 99 void StartTheFirstSession(FakeMidiManagerClient* client) {
91 bool complete_initialization_synchronously) {
92 manager_->complete_initialization_synchronously_ =
93 complete_initialization_synchronously;
94 EXPECT_FALSE(manager_->start_initialization_is_called_); 100 EXPECT_FALSE(manager_->start_initialization_is_called_);
95 EXPECT_EQ(0U, manager_->GetClientCount()); 101 EXPECT_EQ(0U, manager_->GetClientCount());
96 EXPECT_EQ(0U, manager_->GetPendingClientCount()); 102 EXPECT_EQ(0U, manager_->GetPendingClientCount());
97 manager_->StartSession(client, client->GetClientId()); 103 manager_->StartSession(client, client->GetClientId());
98 if (complete_initialization_synchronously) { 104 EXPECT_EQ(0U, manager_->GetClientCount());
99 EXPECT_EQ(1U, manager_->GetClientCount()); 105 EXPECT_EQ(1U, manager_->GetPendingClientCount());
100 EXPECT_EQ(0U, manager_->GetPendingClientCount()); 106 EXPECT_TRUE(manager_->start_initialization_is_called_);
101 EXPECT_TRUE(manager_->start_initialization_is_called_); 107 EXPECT_EQ(0U, manager_->GetClientCount());
102 EXPECT_EQ(MIDI_OK, client->GetResult()); 108 EXPECT_EQ(1U, manager_->GetPendingClientCount());
103 } else { 109 EXPECT_TRUE(manager_->start_initialization_is_called_);
104 EXPECT_EQ(0U, manager_->GetClientCount());
105 EXPECT_EQ(1U, manager_->GetPendingClientCount());
106 EXPECT_TRUE(manager_->start_initialization_is_called_);
107 EXPECT_EQ(0U, manager_->GetClientCount());
108 EXPECT_EQ(1U, manager_->GetPendingClientCount());
109 EXPECT_TRUE(manager_->start_initialization_is_called_);
110 }
111 } 110 }
112 111
113 void StartTheSecondSession(FakeMidiManagerClient* client) { 112 void StartTheSecondSession(FakeMidiManagerClient* client) {
114 EXPECT_TRUE(manager_->start_initialization_is_called_); 113 EXPECT_TRUE(manager_->start_initialization_is_called_);
115 EXPECT_EQ(0U, manager_->GetClientCount()); 114 EXPECT_EQ(0U, manager_->GetClientCount());
116 EXPECT_EQ(1U, manager_->GetPendingClientCount()); 115 EXPECT_EQ(1U, manager_->GetPendingClientCount());
117 116
118 // StartInitialization() should not be called for the second session. 117 // StartInitialization() should not be called for the second session.
119 manager_->start_initialization_is_called_ = false; 118 manager_->start_initialization_is_called_ = false;
120 manager_->StartSession(client, client->GetClientId()); 119 manager_->StartSession(client, client->GetClientId());
121 EXPECT_FALSE(manager_->start_initialization_is_called_); 120 EXPECT_FALSE(manager_->start_initialization_is_called_);
122 } 121 }
123 122
124 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { 123 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
125 EXPECT_EQ(before, manager_->GetClientCount()); 124 EXPECT_EQ(before, manager_->GetClientCount());
126 manager_->EndSession(client); 125 manager_->EndSession(client);
127 EXPECT_EQ(after, manager_->GetClientCount()); 126 EXPECT_EQ(after, manager_->GetClientCount());
128 } 127 }
129 128
130 void CompleteInitialization(MidiResult result) { 129 void CompleteInitialization(MidiResult result) {
131 manager_->CallCompleteInitialization(result); 130 manager_->CallCompleteInitialization(result);
132 } 131 }
133 132
134 private: 133 private:
134 scoped_ptr<base::MessageLoop> message_loop_;
135 scoped_ptr<FakeMidiManager> manager_; 135 scoped_ptr<FakeMidiManager> manager_;
136 136
137 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); 137 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
138 }; 138 };
139 139
140 // Check if calling CompleteInitialization() does not acquire the same lock
141 // on the same thread.
142 TEST_F(MidiManagerTest, StartAndEndSessionSynchronously) {
143 scoped_ptr<FakeMidiManagerClient> client;
144 client.reset(new FakeMidiManagerClient(0));
145
146 StartTheFirstSession(client.get(), true);
147 EndSession(client.get(), 1U, 0U);
148 }
149
150 TEST_F(MidiManagerTest, StartAndEndSession) { 140 TEST_F(MidiManagerTest, StartAndEndSession) {
151 scoped_ptr<FakeMidiManagerClient> client; 141 scoped_ptr<FakeMidiManagerClient> client;
152 client.reset(new FakeMidiManagerClient(0)); 142 client.reset(new FakeMidiManagerClient(0));
153 143
154 StartTheFirstSession(client.get(), false); 144 StartTheFirstSession(client.get());
155 CompleteInitialization(MIDI_OK); 145 CompleteInitialization(MIDI_OK);
156 EXPECT_EQ(MIDI_OK, client->GetResult()); 146 EXPECT_EQ(MIDI_OK, client->WaitForResult());
157 EndSession(client.get(), 1U, 0U); 147 EndSession(client.get(), 1U, 0U);
158 } 148 }
159 149
160 TEST_F(MidiManagerTest, StartAndEndSessionWithError) { 150 TEST_F(MidiManagerTest, StartAndEndSessionWithError) {
161 scoped_ptr<FakeMidiManagerClient> client; 151 scoped_ptr<FakeMidiManagerClient> client;
162 client.reset(new FakeMidiManagerClient(1)); 152 client.reset(new FakeMidiManagerClient(1));
163 153
164 StartTheFirstSession(client.get(), false); 154 StartTheFirstSession(client.get());
165 CompleteInitialization(MIDI_INITIALIZATION_ERROR); 155 CompleteInitialization(MIDI_INITIALIZATION_ERROR);
166 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->GetResult()); 156 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->WaitForResult());
167 EndSession(client.get(), 0U, 0U); 157 EndSession(client.get(), 0U, 0U);
168 } 158 }
169 159
170 TEST_F(MidiManagerTest, StartMultipleSessions) { 160 TEST_F(MidiManagerTest, StartMultipleSessions) {
171 scoped_ptr<FakeMidiManagerClient> client1; 161 scoped_ptr<FakeMidiManagerClient> client1;
172 scoped_ptr<FakeMidiManagerClient> client2; 162 scoped_ptr<FakeMidiManagerClient> client2;
173 client1.reset(new FakeMidiManagerClient(0)); 163 client1.reset(new FakeMidiManagerClient(0));
174 client2.reset(new FakeMidiManagerClient(1)); 164 client2.reset(new FakeMidiManagerClient(1));
175 165
176 StartTheFirstSession(client1.get(), false); 166 StartTheFirstSession(client1.get());
177 StartTheSecondSession(client2.get()); 167 StartTheSecondSession(client2.get());
178 CompleteInitialization(MIDI_OK); 168 CompleteInitialization(MIDI_OK);
179 EXPECT_EQ(MIDI_OK, client1->GetResult()); 169 EXPECT_EQ(MIDI_OK, client1->WaitForResult());
180 EXPECT_EQ(MIDI_OK, client2->GetResult()); 170 EXPECT_EQ(MIDI_OK, client2->WaitForResult());
181 EndSession(client1.get(), 2U, 1U); 171 EndSession(client1.get(), 2U, 1U);
182 EndSession(client2.get(), 1U, 0U); 172 EndSession(client2.get(), 1U, 0U);
183 } 173 }
184 174
175 TEST_F(MidiManagerTest, CreateMidiManager) {
176 scoped_ptr<FakeMidiManagerClient> client;
177 client.reset(new FakeMidiManagerClient(0));
178
179 scoped_ptr<MidiManager> manager(MidiManager::Create());
180 manager->StartSession(client.get(), client->GetClientId());
181 EXPECT_EQ(MIDI_OK, client->WaitForResult());
182 }
183
185 } // namespace 184 } // namespace
186 185
187 } // namespace media 186 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698