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

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: win build fix 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
« no previous file with comments | « media/midi/midi_manager.cc ('k') | media/midi/midi_manager_usb_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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() const {
40 return clients_.size(); 37 return get_clients_size_for_testing();
41 } 38 }
42 39
43 size_t GetPendingClientCount() { 40 size_t GetPendingClientCount() const {
44 return pending_clients_.size(); 41 return get_pending_clients_size_for_testing();
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 get_client_id() const { return client_id_; }
70 return client_id_; 70 MidiResult get_result() const { return result_; }
71 }
72 71
73 MidiResult GetResult() { 72 MidiResult WaitForResult() {
74 return result_; 73 base::RunLoop run_loop;
74 // CompleteStartSession() is called inside the message loop on the same
75 // thread. Protection for |wait_for_result_| is not needed.
76 while (wait_for_result_)
77 run_loop.RunUntilIdle();
78 return get_result();
75 } 79 }
76 80
77 private: 81 private:
78 int client_id_; 82 int client_id_;
79 MidiResult result_; 83 MidiResult result_;
84 bool wait_for_result_;
80 85
81 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); 86 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
82 }; 87 };
83 88
84 class MidiManagerTest : public ::testing::Test { 89 class MidiManagerTest : public ::testing::Test {
85 public: 90 public:
86 MidiManagerTest() : manager_(new FakeMidiManager) {} 91 MidiManagerTest()
92 : message_loop_(new base::MessageLoop), manager_(new FakeMidiManager) {}
87 virtual ~MidiManagerTest() {} 93 virtual ~MidiManagerTest() {}
88 94
89 protected: 95 protected:
90 void StartTheFirstSession(FakeMidiManagerClient* client, 96 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_); 97 EXPECT_FALSE(manager_->start_initialization_is_called_);
95 EXPECT_EQ(0U, manager_->GetClientCount()); 98 EXPECT_EQ(0U, manager_->GetClientCount());
96 EXPECT_EQ(0U, manager_->GetPendingClientCount()); 99 EXPECT_EQ(0U, manager_->GetPendingClientCount());
97 manager_->StartSession(client, client->GetClientId()); 100 manager_->StartSession(client, client->get_client_id());
98 if (complete_initialization_synchronously) { 101 EXPECT_EQ(0U, manager_->GetClientCount());
99 EXPECT_EQ(1U, manager_->GetClientCount()); 102 EXPECT_EQ(1U, manager_->GetPendingClientCount());
100 EXPECT_EQ(0U, manager_->GetPendingClientCount()); 103 EXPECT_TRUE(manager_->start_initialization_is_called_);
101 EXPECT_TRUE(manager_->start_initialization_is_called_); 104 EXPECT_EQ(0U, manager_->GetClientCount());
102 EXPECT_EQ(MIDI_OK, client->GetResult()); 105 EXPECT_EQ(1U, manager_->GetPendingClientCount());
103 } else { 106 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 } 107 }
112 108
113 void StartTheSecondSession(FakeMidiManagerClient* client) { 109 void StartTheSecondSession(FakeMidiManagerClient* client) {
114 EXPECT_TRUE(manager_->start_initialization_is_called_); 110 EXPECT_TRUE(manager_->start_initialization_is_called_);
115 EXPECT_EQ(0U, manager_->GetClientCount()); 111 EXPECT_EQ(0U, manager_->GetClientCount());
116 EXPECT_EQ(1U, manager_->GetPendingClientCount()); 112 EXPECT_EQ(1U, manager_->GetPendingClientCount());
117 113
118 // StartInitialization() should not be called for the second session. 114 // StartInitialization() should not be called for the second session.
119 manager_->start_initialization_is_called_ = false; 115 manager_->start_initialization_is_called_ = false;
120 manager_->StartSession(client, client->GetClientId()); 116 manager_->StartSession(client, client->get_client_id());
121 EXPECT_FALSE(manager_->start_initialization_is_called_); 117 EXPECT_FALSE(manager_->start_initialization_is_called_);
122 } 118 }
123 119
124 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) { 120 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
125 EXPECT_EQ(before, manager_->GetClientCount()); 121 EXPECT_EQ(before, manager_->GetClientCount());
126 manager_->EndSession(client); 122 manager_->EndSession(client);
127 EXPECT_EQ(after, manager_->GetClientCount()); 123 EXPECT_EQ(after, manager_->GetClientCount());
128 } 124 }
129 125
130 void CompleteInitialization(MidiResult result) { 126 void CompleteInitialization(MidiResult result) {
131 manager_->CallCompleteInitialization(result); 127 manager_->CallCompleteInitialization(result);
132 } 128 }
133 129
134 private: 130 private:
131 scoped_ptr<base::MessageLoop> message_loop_;
135 scoped_ptr<FakeMidiManager> manager_; 132 scoped_ptr<FakeMidiManager> manager_;
136 133
137 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); 134 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
138 }; 135 };
139 136
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) { 137 TEST_F(MidiManagerTest, StartAndEndSession) {
151 scoped_ptr<FakeMidiManagerClient> client; 138 scoped_ptr<FakeMidiManagerClient> client;
152 client.reset(new FakeMidiManagerClient(0)); 139 client.reset(new FakeMidiManagerClient(0));
153 140
154 StartTheFirstSession(client.get(), false); 141 StartTheFirstSession(client.get());
155 CompleteInitialization(MIDI_OK); 142 CompleteInitialization(MIDI_OK);
156 EXPECT_EQ(MIDI_OK, client->GetResult()); 143 EXPECT_EQ(MIDI_OK, client->WaitForResult());
157 EndSession(client.get(), 1U, 0U); 144 EndSession(client.get(), 1U, 0U);
158 } 145 }
159 146
160 TEST_F(MidiManagerTest, StartAndEndSessionWithError) { 147 TEST_F(MidiManagerTest, StartAndEndSessionWithError) {
161 scoped_ptr<FakeMidiManagerClient> client; 148 scoped_ptr<FakeMidiManagerClient> client;
162 client.reset(new FakeMidiManagerClient(1)); 149 client.reset(new FakeMidiManagerClient(1));
163 150
164 StartTheFirstSession(client.get(), false); 151 StartTheFirstSession(client.get());
165 CompleteInitialization(MIDI_INITIALIZATION_ERROR); 152 CompleteInitialization(MIDI_INITIALIZATION_ERROR);
166 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->GetResult()); 153 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->WaitForResult());
167 EndSession(client.get(), 0U, 0U); 154 EndSession(client.get(), 0U, 0U);
168 } 155 }
169 156
170 TEST_F(MidiManagerTest, StartMultipleSessions) { 157 TEST_F(MidiManagerTest, StartMultipleSessions) {
171 scoped_ptr<FakeMidiManagerClient> client1; 158 scoped_ptr<FakeMidiManagerClient> client1;
172 scoped_ptr<FakeMidiManagerClient> client2; 159 scoped_ptr<FakeMidiManagerClient> client2;
173 client1.reset(new FakeMidiManagerClient(0)); 160 client1.reset(new FakeMidiManagerClient(0));
174 client2.reset(new FakeMidiManagerClient(1)); 161 client2.reset(new FakeMidiManagerClient(1));
175 162
176 StartTheFirstSession(client1.get(), false); 163 StartTheFirstSession(client1.get());
177 StartTheSecondSession(client2.get()); 164 StartTheSecondSession(client2.get());
178 CompleteInitialization(MIDI_OK); 165 CompleteInitialization(MIDI_OK);
179 EXPECT_EQ(MIDI_OK, client1->GetResult()); 166 EXPECT_EQ(MIDI_OK, client1->WaitForResult());
180 EXPECT_EQ(MIDI_OK, client2->GetResult()); 167 EXPECT_EQ(MIDI_OK, client2->WaitForResult());
181 EndSession(client1.get(), 2U, 1U); 168 EndSession(client1.get(), 2U, 1U);
182 EndSession(client2.get(), 1U, 0U); 169 EndSession(client2.get(), 1U, 0U);
183 } 170 }
184 171
172 TEST_F(MidiManagerTest, CreateMidiManager) {
173 scoped_ptr<FakeMidiManagerClient> client;
174 client.reset(new FakeMidiManagerClient(0));
175
176 scoped_ptr<MidiManager> manager(MidiManager::Create());
177 manager->StartSession(client.get(), client->get_client_id());
178 EXPECT_EQ(MIDI_OK, client->WaitForResult());
179 }
180
185 } // namespace 181 } // namespace
186 182
187 } // namespace media 183 } // namespace media
OLDNEW
« no previous file with comments | « media/midi/midi_manager.cc ('k') | media/midi/midi_manager_usb_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698