OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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 "base/waitable_event.h" | 5 #include "base/waitable_event.h" |
6 #include "chrome/common/ipc_sync_channel.h" | 6 #include "chrome/common/ipc_sync_channel.h" |
7 #include "chrome/common/render_messages.h" | 7 #include "chrome/common/render_messages.h" |
8 #include "chrome/renderer/mock_render_process.h" | 8 #include "chrome/renderer/mock_render_process.h" |
9 #include "chrome/renderer/render_thread.h" | 9 #include "chrome/renderer/render_thread.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 const char kThreadName[] = "render_thread_unittest"; | 14 const char kThreadName[] = "render_thread_unittest"; |
15 | 15 |
16 class RenderThreadTest : public testing::Test { | 16 class RenderThreadTest : public testing::Test { |
17 public: | 17 public: |
18 virtual void SetUp() { | 18 virtual void SetUp() { |
19 // Need a MODE_SERVER to make MODE_CLIENTs (like a RenderThread) happy. | 19 // Need a MODE_SERVER to make MODE_CLIENTs (like a RenderThread) happy. |
20 channel_ = new IPC::Channel(kThreadName, IPC::Channel::MODE_SERVER, NULL); | 20 channel_ = new IPC::Channel(kThreadName, IPC::Channel::MODE_SERVER, NULL); |
21 mock_process_.reset(new MockProcess()); | 21 mock_process_.reset(new MockProcess(new RenderThread(kThreadName))); |
22 mock_process_->set_main_thread(new RenderThread(kThreadName)); | |
23 } | 22 } |
24 | 23 |
25 virtual void TearDown() { | 24 virtual void TearDown() { |
26 message_loop_.RunAllPending(); | 25 message_loop_.RunAllPending(); |
27 mock_process_.reset(); | 26 mock_process_.reset(); |
28 // Need to fully destruct IPC::SyncChannel before the message loop goes | 27 // Need to fully destruct IPC::SyncChannel before the message loop goes |
29 // away. | 28 // away. |
30 message_loop_.RunAllPending(); | 29 message_loop_.RunAllPending(); |
31 // Delete the server channel after the RenderThread so that | 30 // Delete the server channel after the RenderThread so that |
32 // IPC::SyncChannel's OnChannelError doesn't fire on the context and attempt | 31 // IPC::SyncChannel's OnChannelError doesn't fire on the context and attempt |
33 // to use the listener thread which is now gone. | 32 // to use the listener thread which is now gone. |
34 delete channel_; | 33 delete channel_; |
35 } | 34 } |
36 | 35 |
37 protected: | 36 protected: |
38 MessageLoop message_loop_; | 37 MessageLoopForIO message_loop_; |
39 scoped_ptr<MockProcess> mock_process_; | 38 scoped_ptr<MockProcess> mock_process_; |
40 IPC::Channel *channel_; | 39 IPC::Channel *channel_; |
41 }; | 40 }; |
42 | 41 |
43 TEST_F(RenderThreadTest, TestGlobal) { | 42 TEST_F(RenderThreadTest, TestGlobal) { |
44 ASSERT_TRUE(RenderThread::current()); | 43 // Can't reach the RenderThread object on other threads, since it's not |
| 44 // thread-safe! |
| 45 ASSERT_FALSE(RenderThread::current()); |
45 } | 46 } |
46 | 47 |
47 TEST_F(RenderThreadTest, TestVisitedMsg) { | 48 TEST_F(RenderThreadTest, TestVisitedMsg) { |
48 #if defined(OS_WIN) | 49 #if defined(OS_WIN) |
49 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(NULL); | 50 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable(NULL); |
50 #elif defined(OS_POSIX) | 51 #elif defined(OS_POSIX) |
51 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable( | 52 IPC::Message* msg = new ViewMsg_VisitedLink_NewTable( |
52 base::SharedMemoryHandle(0, false)); | 53 base::SharedMemoryHandle(0, false)); |
53 #endif | 54 #endif |
54 ASSERT_TRUE(msg); | 55 ASSERT_TRUE(msg); |
55 // Message goes nowhere, but this confirms Init() has happened. | 56 // Message goes nowhere, but this confirms Init() has happened. |
56 // Unusually (?), RenderThread() Start()s itself in it's constructor. | 57 // Unusually (?), RenderThread() Start()s itself in it's constructor. |
57 mock_process_->main_thread()->Send(msg); | 58 mock_process_->child_thread()->Send(msg); |
58 | 59 |
59 // No need to delete msg; per Message::Send() documentation, "The | 60 // No need to delete msg; per Message::Send() documentation, "The |
60 // implementor takes ownership of the given Message regardless of | 61 // implementor takes ownership of the given Message regardless of |
61 // whether or not this method succeeds." | 62 // whether or not this method succeeds." |
62 } | 63 } |
63 | 64 |
64 } // namespace | 65 } // namespace |
OLD | NEW |