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

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: clean up for review 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/message_loop/message_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()
18 : start_initialization_is_called_(false), 19 : start_initialization_is_called_(false),
(...skipping 27 matching lines...) Expand all
46 47
47 bool start_initialization_is_called_; 48 bool start_initialization_is_called_;
48 bool complete_initialization_synchronously_; 49 bool complete_initialization_synchronously_;
49 50
50 private: 51 private:
51 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager); 52 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager);
52 }; 53 };
53 54
54 class FakeMidiManagerClient : public MidiManagerClient { 55 class FakeMidiManagerClient : public MidiManagerClient {
55 public: 56 public:
56 FakeMidiManagerClient(int client_id) : client_id_(client_id) {} 57 FakeMidiManagerClient(int client_id)
yukawa 2014/05/04 23:16:02 explicit?
Takashi Toyoshima 2014/05/05 00:45:08 Done.
58 : client_id_(client_id),
59 result_(MIDI_NOT_SUPPORTED),
60 wait_for_result_(true) {}
57 virtual ~FakeMidiManagerClient() {} 61 virtual ~FakeMidiManagerClient() {}
58 62
59 // MidiManagerClient implementation. 63 // MidiManagerClient implementation.
60 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE { 64 virtual void CompleteStartSession(int client_id, MidiResult result) OVERRIDE {
61 DCHECK_EQ(client_id_, client_id); 65 CHECK_EQ(client_id_, client_id);
62 result_ = result; 66 result_ = result;
67 wait_for_result_ = false;
63 } 68 }
64 69
65 virtual void ReceiveMidiData(uint32 port_index, const uint8* data, 70 virtual void ReceiveMidiData(uint32 port_index, const uint8* data,
66 size_t size, double timestamp) OVERRIDE {} 71 size_t size, double timestamp) OVERRIDE {}
67 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {} 72 virtual void AccumulateMidiBytesSent(size_t size) OVERRIDE {}
68 73
69 int GetClientId() { 74 int GetClientId() {
yukawa 2014/05/04 23:16:02 [optional] int GetClientId() const {
Takashi Toyoshima 2014/05/05 00:45:08 Done.
70 return client_id_; 75 return client_id_;
71 } 76 }
72 77
73 MidiResult GetResult() { 78 MidiResult GetResult() {
yukawa 2014/05/04 23:16:02 [optiona] MidiResult GetResult() const {
Takashi Toyoshima 2014/05/05 00:45:08 Done.
74 return result_; 79 return result_;
75 } 80 }
76 81
82 MidiResult WaitForResult() {
83 while (wait_for_result_)
84 base::MessageLoop::current()->Run();
85 return GetResult();
86 }
87
77 private: 88 private:
78 int client_id_; 89 int client_id_;
79 MidiResult result_; 90 MidiResult result_;
91 bool wait_for_result_;
80 92
81 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient); 93 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
82 }; 94 };
83 95
84 class MidiManagerTest : public ::testing::Test { 96 class MidiManagerTest : public ::testing::Test {
85 public: 97 public:
86 MidiManagerTest() : manager_(new FakeMidiManager) {} 98 MidiManagerTest()
99 : message_loop_(new base::MessageLoop), manager_(new FakeMidiManager) {}
87 virtual ~MidiManagerTest() {} 100 virtual ~MidiManagerTest() {}
88 101
89 protected: 102 protected:
90 void StartTheFirstSession(FakeMidiManagerClient* client, 103 void StartTheFirstSession(FakeMidiManagerClient* client,
91 bool complete_initialization_synchronously) { 104 bool complete_initialization_synchronously) {
92 manager_->complete_initialization_synchronously_ = 105 manager_->complete_initialization_synchronously_ =
93 complete_initialization_synchronously; 106 complete_initialization_synchronously;
94 EXPECT_FALSE(manager_->start_initialization_is_called_); 107 EXPECT_FALSE(manager_->start_initialization_is_called_);
95 EXPECT_EQ(0U, manager_->GetClientCount()); 108 EXPECT_EQ(0U, manager_->GetClientCount());
96 EXPECT_EQ(0U, manager_->GetPendingClientCount()); 109 EXPECT_EQ(0U, manager_->GetPendingClientCount());
(...skipping 28 matching lines...) Expand all
125 EXPECT_EQ(before, manager_->GetClientCount()); 138 EXPECT_EQ(before, manager_->GetClientCount());
126 manager_->EndSession(client); 139 manager_->EndSession(client);
127 EXPECT_EQ(after, manager_->GetClientCount()); 140 EXPECT_EQ(after, manager_->GetClientCount());
128 } 141 }
129 142
130 void CompleteInitialization(MidiResult result) { 143 void CompleteInitialization(MidiResult result) {
131 manager_->CallCompleteInitialization(result); 144 manager_->CallCompleteInitialization(result);
132 } 145 }
133 146
134 private: 147 private:
148 scoped_ptr<base::MessageLoop> message_loop_;
135 scoped_ptr<FakeMidiManager> manager_; 149 scoped_ptr<FakeMidiManager> manager_;
136 150
137 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); 151 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
138 }; 152 };
139 153
140 // Check if calling CompleteInitialization() does not acquire the same lock 154 // Check if calling CompleteInitialization() does not acquire the same lock
Takashi Toyoshima 2014/05/05 00:45:08 Now that CompleteStartSession is invoked always as
141 // on the same thread. 155 // on the same thread.
142 TEST_F(MidiManagerTest, StartAndEndSessionSynchronously) { 156 TEST_F(MidiManagerTest, StartAndEndSessionSynchronously) {
143 scoped_ptr<FakeMidiManagerClient> client; 157 scoped_ptr<FakeMidiManagerClient> client;
144 client.reset(new FakeMidiManagerClient(0)); 158 client.reset(new FakeMidiManagerClient(0));
145 159
146 StartTheFirstSession(client.get(), true); 160 StartTheFirstSession(client.get(), true);
147 EndSession(client.get(), 1U, 0U); 161 EndSession(client.get(), 1U, 0U);
148 } 162 }
149 163
150 TEST_F(MidiManagerTest, StartAndEndSession) { 164 TEST_F(MidiManagerTest, StartAndEndSession) {
(...skipping 24 matching lines...) Expand all
175 189
176 StartTheFirstSession(client1.get(), false); 190 StartTheFirstSession(client1.get(), false);
177 StartTheSecondSession(client2.get()); 191 StartTheSecondSession(client2.get());
178 CompleteInitialization(MIDI_OK); 192 CompleteInitialization(MIDI_OK);
179 EXPECT_EQ(MIDI_OK, client1->GetResult()); 193 EXPECT_EQ(MIDI_OK, client1->GetResult());
180 EXPECT_EQ(MIDI_OK, client2->GetResult()); 194 EXPECT_EQ(MIDI_OK, client2->GetResult());
181 EndSession(client1.get(), 2U, 1U); 195 EndSession(client1.get(), 2U, 1U);
182 EndSession(client2.get(), 1U, 0U); 196 EndSession(client2.get(), 1U, 0U);
183 } 197 }
184 198
199 TEST_F(MidiManagerTest, CreateMidiManager) {
200 scoped_ptr<FakeMidiManagerClient> client;
201 client.reset(new FakeMidiManagerClient(0));
202
203 scoped_ptr<MidiManager> manager(MidiManager::Create());
204 manager->StartSession(client.get(), client->GetClientId());
205 EXPECT_EQ(MIDI_OK, client->WaitForResult());
206 }
207
185 } // namespace 208 } // namespace
186 209
187 } // namespace media 210 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698