1 // Copyright (c) 2009 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 "base/basictypes.h" 6 #include "chrome/browser/debugger/devtools_client_host.h" 7 #include "chrome/browser/debugger/devtools_manager.h" 8 #include "chrome/browser/debugger/devtools_window.h" 9 #include "chrome/common/render_messages.h" 10 #include "content/browser/renderer_host/test_render_view_host.h" 11 #include "testing/gtest/include/gtest/gtest.h" 12 13 namespace { 14 15 class TestDevToolsClientHost : public DevToolsClientHost { 16 public: 17 TestDevToolsClientHost() 18 : last_sent_message(NULL), 19 closed_(false) { 20 } 21 22 virtual ~TestDevToolsClientHost() { 23 EXPECT_TRUE(closed_); 24 } 25 26 virtual void Close() { 27 EXPECT_FALSE(closed_); 28 close_counter++; 29 NotifyCloseListener(); 30 closed_ = true; 31 } 32 virtual void InspectedTabClosing() { 33 Close(); 34 } 35 36 virtual void SetInspectedTabUrl(const std::string& url) { 37 } 38 39 virtual void SendMessageToClient(const IPC::Message& message) { 40 last_sent_message = &message; 41 } 42 43 virtual void TabReplaced(TabContentsWrapper* new_tab) { 44 } 45 46 static void ResetCounters() { 47 close_counter = 0; 48 } 49 50 static int close_counter; 51 52 const IPC::Message* last_sent_message; 53 54 private: 55 bool closed_; 56 57 virtual void FrameNavigating(const std::string& url) {} 58 59 DISALLOW_COPY_AND_ASSIGN(TestDevToolsClientHost); 60 }; 61 62 int TestDevToolsClientHost::close_counter = 0; 63 64 } // namespace 65 66 class DevToolsManagerTest : public RenderViewHostTestHarness { 67 public: 68 DevToolsManagerTest() : RenderViewHostTestHarness() { 69 } 70 71 protected: 72 virtual void SetUp() { 73 RenderViewHostTestHarness::SetUp(); 74 TestDevToolsClientHost::ResetCounters(); 75 } 76 }; 77 78 TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsClientHost) { 79 scoped_refptr<DevToolsManager> manager(new DevToolsManager()); 80 81 DevToolsClientHost* host = manager->GetDevToolsClientHostFor(rvh()); 82 EXPECT_TRUE(NULL == host); 83 84 TestDevToolsClientHost client_host; 85 manager->RegisterDevToolsClientHostFor(rvh(), &client_host); 86 // Test that just registered devtools host is returned. 87 host = manager->GetDevToolsClientHostFor(rvh()); 88 EXPECT_TRUE(&client_host == host); 89 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); 90 91 // Test that the same devtools host is returned. 92 host = manager->GetDevToolsClientHostFor(rvh()); 93 EXPECT_TRUE(&client_host == host); 94 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); 95 96 client_host.Close(); 97 EXPECT_EQ(1, TestDevToolsClientHost::close_counter); 98 host = manager->GetDevToolsClientHostFor(rvh()); 99 EXPECT_TRUE(NULL == host); 100 } 101 102 TEST_F(DevToolsManagerTest, ForwardMessageToClient) { 103 scoped_refptr<DevToolsManager> manager(new DevToolsManager()); 104 105 TestDevToolsClientHost client_host; 106 manager->RegisterDevToolsClientHostFor(rvh(), &client_host); 107 EXPECT_EQ(0, TestDevToolsClientHost::close_counter); 108 109 IPC::Message m; 110 manager->ForwardToDevToolsClient(rvh(), m); 111 EXPECT_TRUE(&m == client_host.last_sent_message); 112 113 client_host.Close(); 114 EXPECT_EQ(1, TestDevToolsClientHost::close_counter); 115 } 116