OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include <wayland-client-core.h> |
| 6 |
| 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/process/process_handle.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "components/exo/display.h" |
| 14 #include "components/exo/wayland/server.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace exo { |
| 18 namespace wayland { |
| 19 namespace { |
| 20 |
| 21 base::StaticAtomicSequenceNumber g_next_socket_id; |
| 22 |
| 23 std::string GetUniqueSocketName() { |
| 24 return base::StringPrintf("wayland-test-%d-%d", base::GetCurrentProcId(), |
| 25 g_next_socket_id.GetNext()); |
| 26 } |
| 27 |
| 28 TEST(ServerTest, AddSocket) { |
| 29 scoped_ptr<Display> display(new Display); |
| 30 scoped_ptr<Server> server(new Server(display.get())); |
| 31 |
| 32 // Check that calling AddSocket() with a unique socket name succeeds. |
| 33 bool rv = server->AddSocket(GetUniqueSocketName()); |
| 34 EXPECT_TRUE(rv); |
| 35 } |
| 36 |
| 37 TEST(ServerTest, GetFileDescriptor) { |
| 38 scoped_ptr<Display> display(new Display); |
| 39 scoped_ptr<Server> server(new Server(display.get())); |
| 40 |
| 41 bool rv = server->AddSocket(GetUniqueSocketName()); |
| 42 EXPECT_TRUE(rv); |
| 43 |
| 44 // Check that the returned file descriptor is valid. |
| 45 int fd = server->GetFileDescriptor(); |
| 46 DCHECK_GE(fd, 0); |
| 47 } |
| 48 |
| 49 void ConnectToServer(const std::string socket_name, |
| 50 bool* connected_to_server, |
| 51 base::WaitableEvent* event) { |
| 52 wl_display* display = wl_display_connect(socket_name.c_str()); |
| 53 *connected_to_server = !!display; |
| 54 event->Signal(); |
| 55 wl_display_disconnect(display); |
| 56 } |
| 57 |
| 58 TEST(ServerTest, Dispatch) { |
| 59 scoped_ptr<Display> display(new Display); |
| 60 scoped_ptr<Server> server(new Server(display.get())); |
| 61 |
| 62 std::string socket_name = GetUniqueSocketName(); |
| 63 bool rv = server->AddSocket(socket_name); |
| 64 EXPECT_TRUE(rv); |
| 65 |
| 66 base::Thread client("client-" + socket_name); |
| 67 ASSERT_TRUE(client.Start()); |
| 68 |
| 69 // Post a task that connects server on the created thread. |
| 70 bool connected_to_server = false; |
| 71 base::WaitableEvent event(false, false); |
| 72 client.task_runner()->PostTask( |
| 73 FROM_HERE, |
| 74 base::Bind(&ConnectToServer, socket_name, &connected_to_server, &event)); |
| 75 |
| 76 // Call Dispatch() with a 5 second timeout. |
| 77 server->Dispatch(base::TimeDelta::FromSeconds(5)); |
| 78 |
| 79 // Check if client thread managed to connect to server. |
| 80 event.Wait(); |
| 81 EXPECT_TRUE(connected_to_server); |
| 82 } |
| 83 |
| 84 TEST(ServerTest, Flush) { |
| 85 scoped_ptr<Display> display(new Display); |
| 86 scoped_ptr<Server> server(new Server(display.get())); |
| 87 |
| 88 bool rv = server->AddSocket(GetUniqueSocketName()); |
| 89 EXPECT_TRUE(rv); |
| 90 |
| 91 // Just call Flush to check that it doesn't have any bad side-effects. |
| 92 server->Flush(); |
| 93 } |
| 94 |
| 95 } // namespace |
| 96 } // namespace wayland |
| 97 } // namespace exo |
OLD | NEW |