blob: 5017eb2197fd8822791027cf12ec8889cd37f8f9 [file] [log] [blame]
[email protected]7419d4b2011-04-06 15:18:041// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]11b6e602010-10-13 16:02:262// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]c62dd9d2011-09-21 18:05:415#include "chrome_frame/external_tab.h"
[email protected]314a06962010-10-20 21:32:326
[email protected]44f9c952011-01-02 06:05:397// #include "base/synchronization/waitable_event.h"
[email protected]11b6e602010-10-13 16:02:268
[email protected]bb04c4e2011-11-18 20:44:229#include "base/bind.h"
10#include "base/bind_helpers.h"
11#include "base/location.h"
12#include "base/threading/thread.h"
[email protected]a8ba6362010-11-10 20:02:2513#include "chrome/common/automation_messages.h"
[email protected]354bcba2010-12-14 04:34:4314#include "chrome_frame/navigation_constraints.h"
15#include "chrome_frame/test/chrome_frame_test_utils.h"
[email protected]11b6e602010-10-13 16:02:2616#include "testing/gtest/include/gtest/gtest.h"
17#include "testing/gmock/include/gmock/gmock.h"
[email protected]314a06962010-10-20 21:32:3218#include "testing/gmock_mutant.h"
[email protected]11b6e602010-10-13 16:02:2619
[email protected]314a06962010-10-20 21:32:3220using testing::StrictMock;
21using testing::_;
22using testing::Invoke;
23using testing::InvokeWithoutArgs;
24using testing::CreateFunctor;
25using testing::Return;
26using testing::DoAll;
27using testing::Field;
28using chrome_frame_test::TimedMsgLoop;
[email protected]11b6e602010-10-13 16:02:2629
30struct MockUIDelegate : public UIDelegate {
31 MOCK_METHOD2(OnNavigationStateChanged, void(int flags,
[email protected]f5494d42010-12-23 22:15:3432 const NavigationInfo& nav_info));
[email protected]11b6e602010-10-13 16:02:2633 MOCK_METHOD1(OnUpdateTargetUrl, void(const std::wstring& new_target_url));
[email protected]11b6e602010-10-13 16:02:2634 MOCK_METHOD1(OnLoad, void(const GURL& url));
[email protected]326d3b72011-03-29 20:38:2435 MOCK_METHOD1(OnMoveWindow, void(const gfx::Rect& pos));
[email protected]11b6e602010-10-13 16:02:2636 MOCK_METHOD3(OnMessageFromChromeFrame, void(const std::string& message,
37 const std::string& origin, const std::string& target));
[email protected]b516e2d2011-07-12 16:54:1238 MOCK_METHOD3(OnHandleContextMenu, void(const ContextMenuModel& menu_model,
39 int align_flags, const MiniContextMenuParams& params));
[email protected]11b6e602010-10-13 16:02:2640 MOCK_METHOD1(OnHandleAccelerator, void(const MSG& accel_message));
41 MOCK_METHOD1(OnTabbedOut, void(bool reverse));
42 MOCK_METHOD1(OnGoToHistoryOffset, void(int offset));
43 MOCK_METHOD3(OnOpenURL, void(const GURL& url_to_open, const GURL& referrer,
44 int open_disposition));
45};
46
47struct MockProxy : public ChromeProxy {
48 MOCK_METHOD1(RemoveBrowsingData, void(int remove_mask));
[email protected]11b6e602010-10-13 16:02:2649 MOCK_METHOD1(SetProxyConfig, void(const std::string& json_encoded_settings));
50
51 MOCK_METHOD2(CreateTab, void(ChromeProxyDelegate* delegate,
[email protected]f5494d42010-12-23 22:15:3452 const ExternalTabSettings& settings));
[email protected]11b6e602010-10-13 16:02:2653 MOCK_METHOD3(ConnectTab, void(ChromeProxyDelegate* delegate, HWND hwnd,
54 uint64 cookie));
55 MOCK_METHOD1(BlockTab, void(uint64 cookie));
56
57 MOCK_METHOD4(Tab_PostMessage, void(int tab, const std::string& message,
58 const std::string& origin, const std::string& target));
59 MOCK_METHOD1(Tab_Reload, void(int tab));
60 MOCK_METHOD1(Tab_Stop, void(int tab));
61 MOCK_METHOD1(Tab_SaveAs, void(int tab));
62 MOCK_METHOD1(Tab_Print, void(int tab));
63 MOCK_METHOD1(Tab_Cut, void(int tab));
64 MOCK_METHOD1(Tab_Copy, void(int tab));
65 MOCK_METHOD1(Tab_Paste, void(int tab));
66 MOCK_METHOD1(Tab_SelectAll, void(int tab));
67 MOCK_METHOD5(Tab_Find, void(int tab, const string16& search_string,
68 FindInPageDirection forward, FindInPageCase match_case, bool find_next));
69 MOCK_METHOD2(Tab_MenuCommand, void(int tab, int selected_command));
70
[email protected]54087fe2011-10-28 22:02:4871 MOCK_METHOD2(Tab_Zoom, void(int tab, content::PageZoom zoom_level));
[email protected]11b6e602010-10-13 16:02:2672 MOCK_METHOD2(Tab_FontSize, void(int tab, AutomationPageFontSize font_size));
73 MOCK_METHOD3(Tab_SetInitialFocus, void(int tab, bool reverse,
74 bool restore_focus_to_view));
75 MOCK_METHOD1(Tab_SetParentWindow, void(int tab));
76 MOCK_METHOD1(Tab_Resize, void(int tab));
77 MOCK_METHOD2(Tab_ProcessAccelerator, void(int tab, const MSG& msg));
78
79 // Misc.
80 MOCK_METHOD1(Tab_OnHostMoved, void(int tab));
81 MOCK_METHOD1(Tab_RunUnloadHandlers, void(int tab));
[email protected]11b6e602010-10-13 16:02:2682 MOCK_METHOD3(Tab_Navigate, void(int tab, const GURL& url,
83 const GURL& referrer));
84 MOCK_METHOD2(Tab_OverrideEncoding, void(int tab, const char* encoding));
85
[email protected]11b6e602010-10-13 16:02:2686 MOCK_METHOD1(Init, void(const ProxyParams& params));
87 MOCK_METHOD1(AddDelegate, int(ChromeProxyDelegate* delegate));
88 MOCK_METHOD1(RemoveDelegate, int(ChromeProxyDelegate* delegate));
89};
90
[email protected]314a06962010-10-20 21:32:3291struct MockFactory : public ChromeProxyFactory {
92 MOCK_METHOD0(CreateProxy, ChromeProxy*());
93};
94
95
96// This class just calls methods of ChromeProxyDelegate but in background thread
97struct AsyncEventCreator {
98 public:
99 explicit AsyncEventCreator(ChromeProxyDelegate* delegate)
100 : delegate_(delegate), ipc_thread_("ipc") {
101 base::Thread::Options options(MessageLoop::TYPE_IO, 0);
102 ipc_thread_.StartWithOptions(options);
103 ipc_loop_ = ipc_thread_.message_loop();
104 }
105
106 void Fire_Connected(ChromeProxy* proxy, base::TimeDelta delay) {
[email protected]bb04c4e2011-11-18 20:44:22107 ipc_loop_->PostDelayedTask(
108 FROM_HERE,
109 base::Bind(&ChromeProxyDelegate::Connected, base::Unretained(delegate_),
110 proxy),
111 delay.InMilliseconds());
[email protected]314a06962010-10-20 21:32:32112 }
113
114 void Fire_PeerLost(ChromeProxy* proxy,
115 ChromeProxyDelegate::DisconnectReason reason, base::TimeDelta delay) {
[email protected]bb04c4e2011-11-18 20:44:22116 ipc_loop_->PostDelayedTask(
117 FROM_HERE,
118 base::Bind(&ChromeProxyDelegate::PeerLost, base::Unretained(delegate_),
119 proxy, reason),
120 delay.InMilliseconds());
[email protected]314a06962010-10-20 21:32:32121 }
122
123 void Fire_Disconnected(base::TimeDelta delay) {
[email protected]bb04c4e2011-11-18 20:44:22124 ipc_loop_->PostDelayedTask(
125 FROM_HERE,
126 base::Bind(&ChromeProxyDelegate::Disconnected,
127 base::Unretained(delegate_)),
128 delay.InMilliseconds());
[email protected]314a06962010-10-20 21:32:32129 }
130
131 void Fire_CompletedCreateTab(bool success, HWND chrome_wnd, HWND tab_window,
[email protected]751bf4b2010-11-05 22:06:31132 int tab_handle, int session_id,
133 base::TimeDelta delay) {
[email protected]bb04c4e2011-11-18 20:44:22134 ipc_loop_->PostDelayedTask(
135 FROM_HERE,
136 base::Bind(&ChromeProxyDelegate::Completed_CreateTab,
137 base::Unretained(delegate_), success, chrome_wnd, tab_window,
138 tab_handle, session_id),
139 delay.InMilliseconds());
[email protected]314a06962010-10-20 21:32:32140 }
141
[email protected]314a06962010-10-20 21:32:32142 private:
143 ChromeProxyDelegate* delegate_;
144 base::Thread ipc_thread_;
145 MessageLoop* ipc_loop_;
146};
147
148// We may want the same test with 'real' proxy and mock 'proxy traits'.
149TEST(ExternalTabProxy, CancelledCreateTab) {
150 MockUIDelegate ui_delegate;
151 StrictMock<MockFactory> factory;
152 scoped_ptr<ExternalTabProxy> tab(new ExternalTabProxy());
153 AsyncEventCreator async_events(tab.get());
154 StrictMock<MockProxy>* proxy = new StrictMock<MockProxy>();
[email protected]115d4ca2010-10-20 23:33:34155 TimedMsgLoop loop;
[email protected]314a06962010-10-20 21:32:32156 tab->set_proxy_factory(&factory);
157
158 EXPECT_CALL(factory, CreateProxy()).WillOnce(Return(proxy));
159 EXPECT_CALL(*proxy, Init(_));
160 EXPECT_CALL(*proxy, AddDelegate(tab.get())).WillOnce(DoAll(InvokeWithoutArgs(
161 CreateFunctor(&async_events, &AsyncEventCreator::Fire_Connected,
162 proxy, base::TimeDelta::FromMilliseconds(30))),
163 Return(1)));
164
[email protected]115d4ca2010-10-20 23:33:34165 EXPECT_CALL(*proxy, CreateTab(tab.get(), _))
166 .WillOnce(QUIT_LOOP(loop));
167
[email protected]314a06962010-10-20 21:32:32168 EXPECT_CALL(*proxy, RemoveDelegate(_)).WillOnce(DoAll(
169 InvokeWithoutArgs(CreateFunctor(&async_events,
170 &AsyncEventCreator::Fire_CompletedCreateTab, false, HWND(0), HWND(0),
[email protected]751bf4b2010-11-05 22:06:31171 0, 0, base::TimeDelta::FromMilliseconds(0))),
[email protected]314a06962010-10-20 21:32:32172 InvokeWithoutArgs(CreateFunctor(&async_events,
173 &AsyncEventCreator::Fire_Disconnected,
174 base::TimeDelta::FromMilliseconds(0))),
175 Return(0)));
176
177 CreateTabParams tab_params;
178 tab_params.is_incognito = true;
179 tab_params.is_widget_mode = false;
180 tab_params.url = GURL("http://Xanadu.org");
181
182 tab->CreateTab(tab_params, &ui_delegate);
[email protected]115d4ca2010-10-20 23:33:34183 loop.RunFor(5);
184 EXPECT_FALSE(loop.WasTimedOut());
[email protected]314a06962010-10-20 21:32:32185 tab.reset();
186}