roll libyuv to r1450 from r1444. Adds direct YUY2ToNV12 and UYVYToNV12 conversions...
[chromium-blink-merge.git] / base / mac / dispatch_source_mach_unittest.cc
blob82dc13643c4ad7e00eeeb5b29c5e0b0e6ed89c16
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
3 // found in the LICENSE file.
5 #include "base/mac/dispatch_source_mach.h"
7 #include <mach/mach.h>
9 #include "base/logging.h"
10 #include "base/mac/scoped_mach_port.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/test/test_timeouts.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace base {
17 class DispatchSourceMachTest : public testing::Test {
18 public:
19 void SetUp() override {
20 mach_port_t port = MACH_PORT_NULL;
21 ASSERT_EQ(KERN_SUCCESS, mach_port_allocate(mach_task_self(),
22 MACH_PORT_RIGHT_RECEIVE, &port));
23 receive_right_.reset(port);
25 ASSERT_EQ(KERN_SUCCESS, mach_port_insert_right(mach_task_self(), port,
26 port, MACH_MSG_TYPE_MAKE_SEND));
27 send_right_.reset(port);
30 mach_port_t GetPort() { return receive_right_.get(); }
32 void WaitForSemaphore(dispatch_semaphore_t semaphore) {
33 dispatch_semaphore_wait(semaphore, dispatch_time(
34 DISPATCH_TIME_NOW,
35 TestTimeouts::action_timeout().InSeconds() * NSEC_PER_SEC));
38 private:
39 base::mac::ScopedMachReceiveRight receive_right_;
40 base::mac::ScopedMachSendRight send_right_;
43 TEST_F(DispatchSourceMachTest, ReceiveAfterResume) {
44 dispatch_semaphore_t signal = dispatch_semaphore_create(0);
45 mach_port_t port = GetPort();
47 bool __block did_receive = false;
48 DispatchSourceMach source("org.chromium.base.test.ReceiveAfterResume",
49 port, ^{
50 mach_msg_empty_rcv_t msg = {{0}};
51 msg.header.msgh_size = sizeof(msg);
52 msg.header.msgh_local_port = port;
53 mach_msg_receive(&msg.header);
54 did_receive = true;
56 dispatch_semaphore_signal(signal);
57 });
59 mach_msg_empty_send_t msg = {{0}};
60 msg.header.msgh_size = sizeof(msg);
61 msg.header.msgh_remote_port = port;
62 msg.header.msgh_bits = MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
63 ASSERT_EQ(KERN_SUCCESS, mach_msg_send(&msg.header));
65 EXPECT_FALSE(did_receive);
67 source.Resume();
69 WaitForSemaphore(signal);
70 dispatch_release(signal);
72 EXPECT_TRUE(did_receive);
75 TEST_F(DispatchSourceMachTest, NoMessagesAfterDestruction) {
76 mach_port_t port = GetPort();
78 scoped_ptr<int> count(new int(0));
79 int* __block count_ptr = count.get();
81 scoped_ptr<DispatchSourceMach> source(new DispatchSourceMach(
82 "org.chromium.base.test.NoMessagesAfterDestruction",
83 port, ^{
84 mach_msg_empty_rcv_t msg = {{0}};
85 msg.header.msgh_size = sizeof(msg);
86 msg.header.msgh_local_port = port;
87 mach_msg_receive(&msg.header);
88 LOG(INFO) << "Receieve " << *count_ptr;
89 ++(*count_ptr);
90 }));
91 source->Resume();
93 dispatch_queue_t queue =
94 dispatch_queue_create("org.chromium.base.test.MessageSend", NULL);
95 dispatch_semaphore_t signal = dispatch_semaphore_create(0);
96 for (int i = 0; i < 30; ++i) {
97 dispatch_async(queue, ^{
98 mach_msg_empty_send_t msg = {{0}};
99 msg.header.msgh_size = sizeof(msg);
100 msg.header.msgh_remote_port = port;
101 msg.header.msgh_bits =
102 MACH_MSGH_BITS_REMOTE(MACH_MSG_TYPE_COPY_SEND);
103 mach_msg_send(&msg.header);
106 // After sending five messages, shut down the source and taint the
107 // pointer the handler dereferences. The test will crash if |count_ptr|
108 // is being used after "free".
109 if (i == 5) {
110 scoped_ptr<DispatchSourceMach>* source_ptr = &source;
111 dispatch_async(queue, ^{
112 source_ptr->reset();
113 count_ptr = reinterpret_cast<int*>(0xdeaddead);
114 dispatch_semaphore_signal(signal);
119 WaitForSemaphore(signal);
120 dispatch_release(signal);
122 dispatch_release(queue);
125 } // namespace base