1 // Copyright (c) 2011 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 <map>
6
7 #include "base/basictypes.h"
8 #include "chrome/browser/automation/automation_tab_helper.h"
9 #include "chrome/browser/automation/mock_tab_event_observer.h"
10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
11 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h"
12 #include "content/browser/tab_contents/tab_contents.h"
13 #include "content/browser/tab_contents/test_tab_contents.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using testing::_;
18
19 class AutomationTabHelperTest : public TabContentsWrapperTestHarness {
20 public:
SetUp()21 virtual void SetUp() {
22 TabContentsWrapperTestHarness::SetUp();
23 mock_observer_.StartObserving(tab_helper());
24 }
25
26 protected:
27 // These are here so that we don't have to add each test as a
28 // |AutomationTabHelper| friend.
StartLoading()29 void StartLoading() {
30 tab_helper()->DidStartLoading();
31 }
32
StopLoading()33 void StopLoading() {
34 tab_helper()->DidStopLoading();
35 }
36
TabContentsDestroyed()37 void TabContentsDestroyed() {
38 tab_helper()->TabContentsDestroyed(contents_wrapper()->tab_contents());
39 }
40
WillPerformClientRedirect(int64 frame_id)41 void WillPerformClientRedirect(int64 frame_id) {
42 tab_helper()->OnWillPerformClientRedirect(frame_id, 0);
43 }
44
CompleteOrCancelClientRedirect(int64 frame_id)45 void CompleteOrCancelClientRedirect(int64 frame_id) {
46 tab_helper()->OnDidCompleteOrCancelClientRedirect(frame_id);
47 }
48
tab_helper()49 AutomationTabHelper* tab_helper() {
50 return contents_wrapper()->automation_tab_helper();
51 }
52
53 MockTabEventObserver mock_observer_;
54 };
55
ACTION_P2(CheckHasPendingLoads,tab_helper,has_pending_loads)56 ACTION_P2(CheckHasPendingLoads, tab_helper, has_pending_loads) {
57 EXPECT_EQ(has_pending_loads, tab_helper->has_pending_loads());
58 }
59
TEST_F(AutomationTabHelperTest,AddAndRemoveObserver)60 TEST_F(AutomationTabHelperTest, AddAndRemoveObserver) {
61 testing::MockFunction<void()> check;
62
63 {
64 testing::InSequence expect_in_sequence;
65 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(_));
66 EXPECT_CALL(check, Call());
67 }
68
69 StartLoading();
70 check.Call();
71 tab_helper()->RemoveObserver(&mock_observer_);
72 StartLoading();
73 }
74
TEST_F(AutomationTabHelperTest,StartStopLoading)75 TEST_F(AutomationTabHelperTest, StartStopLoading) {
76 testing::MockFunction<void()> check;
77
78 testing::InSequence expect_in_sequence;
79 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()))
80 .WillOnce(CheckHasPendingLoads(tab_helper(), true));
81 EXPECT_CALL(check, Call());
82 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()))
83 .WillOnce(CheckHasPendingLoads(tab_helper(), false));
84
85 EXPECT_FALSE(tab_helper()->has_pending_loads());
86 StartLoading();
87 EXPECT_TRUE(tab_helper()->has_pending_loads());
88 check.Call();
89 StopLoading();
90 EXPECT_FALSE(tab_helper()->has_pending_loads());
91 }
92
TEST_F(AutomationTabHelperTest,DuplicateLoads)93 TEST_F(AutomationTabHelperTest, DuplicateLoads) {
94 testing::InSequence expect_in_sequence;
95 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()));
96 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()));
97
98 StartLoading();
99 StartLoading();
100 StopLoading();
101 StopLoading();
102 }
103
TEST_F(AutomationTabHelperTest,ClientRedirect)104 TEST_F(AutomationTabHelperTest, ClientRedirect) {
105 testing::MockFunction<void()> check;
106
107 testing::InSequence expect_in_sequence;
108 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()))
109 .WillOnce(CheckHasPendingLoads(tab_helper(), true));
110 EXPECT_CALL(check, Call());
111 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()))
112 .WillOnce(CheckHasPendingLoads(tab_helper(), false));
113
114 WillPerformClientRedirect(1);
115 EXPECT_TRUE(tab_helper()->has_pending_loads());
116 check.Call();
117 CompleteOrCancelClientRedirect(1);
118 EXPECT_FALSE(tab_helper()->has_pending_loads());
119 }
120
TEST_F(AutomationTabHelperTest,DiscardExtraClientRedirects)121 TEST_F(AutomationTabHelperTest, DiscardExtraClientRedirects) {
122 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()));
123 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()));
124
125 WillPerformClientRedirect(1);
126 WillPerformClientRedirect(1);
127 CompleteOrCancelClientRedirect(1);
128 EXPECT_FALSE(tab_helper()->has_pending_loads());
129 CompleteOrCancelClientRedirect(1);
130 CompleteOrCancelClientRedirect(2);
131 }
132
TEST_F(AutomationTabHelperTest,StartStopLoadingWithClientRedirect)133 TEST_F(AutomationTabHelperTest, StartStopLoadingWithClientRedirect) {
134 testing::MockFunction<void()> check;
135
136 testing::InSequence expect_in_sequence;
137 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()));
138 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()));
139
140 StartLoading();
141 WillPerformClientRedirect(1);
142 CompleteOrCancelClientRedirect(1);
143 StopLoading();
144 }
145
TEST_F(AutomationTabHelperTest,ClientRedirectBeforeLoad)146 TEST_F(AutomationTabHelperTest, ClientRedirectBeforeLoad) {
147 testing::MockFunction<void()> check;
148
149 testing::InSequence expect_in_sequence;
150 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()));
151 EXPECT_CALL(check, Call());
152 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()));
153
154 StartLoading();
155 WillPerformClientRedirect(1);
156 CompleteOrCancelClientRedirect(1);
157 EXPECT_TRUE(tab_helper()->has_pending_loads());
158 check.Call();
159 StopLoading();
160 }
161
TEST_F(AutomationTabHelperTest,ClientRedirectAfterLoad)162 TEST_F(AutomationTabHelperTest, ClientRedirectAfterLoad) {
163 testing::MockFunction<void()> check;
164
165 testing::InSequence expect_in_sequence;
166 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()));
167 EXPECT_CALL(check, Call());
168 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()));
169
170 StartLoading();
171 WillPerformClientRedirect(1);
172 StopLoading();
173 EXPECT_TRUE(tab_helper()->has_pending_loads());
174 check.Call();
175 CompleteOrCancelClientRedirect(1);
176 EXPECT_FALSE(tab_helper()->has_pending_loads());
177 }
178
TEST_F(AutomationTabHelperTest,AllFramesMustFinishRedirects)179 TEST_F(AutomationTabHelperTest, AllFramesMustFinishRedirects) {
180 testing::MockFunction<void()> check;
181
182 testing::InSequence expect_in_sequence;
183 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()));
184 EXPECT_CALL(check, Call());
185 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()));
186
187 WillPerformClientRedirect(1);
188 WillPerformClientRedirect(2);
189 CompleteOrCancelClientRedirect(1);
190 check.Call();
191 EXPECT_TRUE(tab_helper()->has_pending_loads());
192 CompleteOrCancelClientRedirect(2);
193 EXPECT_FALSE(tab_helper()->has_pending_loads());
194 }
195
TEST_F(AutomationTabHelperTest,DestroyedTabStopsLoading)196 TEST_F(AutomationTabHelperTest, DestroyedTabStopsLoading) {
197 testing::MockFunction<void()> check;
198
199 testing::InSequence expect_in_sequence;
200 EXPECT_CALL(mock_observer_, OnFirstPendingLoad(contents()));
201 EXPECT_CALL(mock_observer_, OnNoMorePendingLoads(contents()));
202
203 StartLoading();
204 WillPerformClientRedirect(1);
205 TabContentsDestroyed();
206 EXPECT_FALSE(tab_helper()->has_pending_loads());
207 }
208