1 // Copyright 2012 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.
5 #import <Foundation/Foundation.h>
7 #include "base/logging.h"
8 #import "base/mac/scoped_nsobject.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/sys_string_conversions.h"
11 #import "ios/web/navigation/crw_session_controller+private_constructors.h"
12 #import "ios/web/navigation/crw_session_controller.h"
13 #include "ios/web/navigation/crw_session_entry.h"
14 #include "ios/web/navigation/navigation_item_impl.h"
15 #include "ios/web/public/referrer.h"
16 #include "ios/web/public/test/test_browser_state.h"
17 #import "net/base/mac/url_conversions.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/gtest_mac.h"
20 #include "testing/platform_test.h"
22 @interface CRWSessionController (Testing)
23 - (const GURL&)URLForSessionAtIndex:(NSUInteger)index;
24 - (const GURL&)currentURL;
27 @implementation CRWSessionController (Testing)
28 - (const GURL&)URLForSessionAtIndex:(NSUInteger)index {
29 CRWSessionEntry* entry =
30 static_cast<CRWSessionEntry*>([self.entries objectAtIndex:index]);
31 return entry.navigationItem->GetURL();
34 - (const GURL&)currentURL {
35 DCHECK([self currentEntry]);
36 return [self currentEntry].navigationItem->GetURL();
42 class CRWSessionControllerTest : public PlatformTest {
44 void SetUp() override {
45 session_controller_.reset(
46 [[CRWSessionController alloc] initWithWindowName:@"test window"
49 openerNavigationIndex:0
50 browserState:&browser_state_]);
53 web::Referrer MakeReferrer(std::string url) {
54 return web::Referrer(GURL(url), web::ReferrerPolicyDefault);
57 web::TestBrowserState browser_state_;
58 base::scoped_nsobject<CRWSessionController> session_controller_;
61 TEST_F(CRWSessionControllerTest, InitWithWindowName) {
62 EXPECT_NSEQ(@"test window", [session_controller_ windowName]);
63 EXPECT_NSEQ(@"opener", [session_controller_ openerId]);
64 EXPECT_FALSE([session_controller_ isOpenedByDOM]);
65 EXPECT_EQ(0U, [[session_controller_ entries] count]);
66 EXPECT_EQ(nil, [session_controller_ currentEntry]);
69 TEST_F(CRWSessionControllerTest, AddPendingEntry) {
71 addPendingEntry:GURL("http://www.url.com")
72 referrer:MakeReferrer("http://www.referer.com")
73 transition:ui::PAGE_TRANSITION_TYPED
74 rendererInitiated:NO];
76 EXPECT_EQ(0U, [[session_controller_ entries] count]);
78 GURL("http://www.url.com/"),
79 [session_controller_ currentURL]);
82 TEST_F(CRWSessionControllerTest, AddPendingEntryWithCommittedEntries) {
84 addPendingEntry:GURL("http://www.committed.url.com")
85 referrer:MakeReferrer("http://www.referer.com")
86 transition:ui::PAGE_TRANSITION_TYPED
87 rendererInitiated:NO];
88 [session_controller_ commitPendingEntry];
91 addPendingEntry:GURL("http://www.url.com")
92 referrer:MakeReferrer("http://www.referer.com")
93 transition:ui::PAGE_TRANSITION_TYPED
94 rendererInitiated:NO];
96 EXPECT_EQ(1U, [[session_controller_ entries] count]);
98 GURL("http://www.committed.url.com/"),
99 [session_controller_ URLForSessionAtIndex:0U]);
101 GURL("http://www.url.com/"),
102 [session_controller_ currentURL]);
105 TEST_F(CRWSessionControllerTest, AddPendingEntryOverriding) {
107 addPendingEntry:GURL("http://www.url.com")
108 referrer:MakeReferrer("http://www.referer.com")
109 transition:ui::PAGE_TRANSITION_TYPED
110 rendererInitiated:NO];
112 addPendingEntry:GURL("http://www.another.url.com")
113 referrer:MakeReferrer("http://www.another.referer.com")
114 transition:ui::PAGE_TRANSITION_TYPED
115 rendererInitiated:NO];
117 EXPECT_EQ(0U, [[session_controller_ entries] count]);
119 GURL("http://www.another.url.com/"),
120 [session_controller_ currentURL]);
123 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommit) {
125 addPendingEntry:GURL("http://www.url.com")
126 referrer:MakeReferrer("http://www.referer.com")
127 transition:ui::PAGE_TRANSITION_TYPED
128 rendererInitiated:NO];
129 [session_controller_ commitPendingEntry];
131 EXPECT_EQ(1U, [[session_controller_ entries] count]);
133 GURL("http://www.url.com/"),
134 [session_controller_ URLForSessionAtIndex:0U]);
136 [[session_controller_ entries] objectAtIndex:0U],
137 [session_controller_ currentEntry]);
140 TEST_F(CRWSessionControllerTest, AddPendingEntryOverridingAndCommit) {
142 addPendingEntry:GURL("http://www.url.com")
143 referrer:MakeReferrer("http://www.referer.com")
144 transition:ui::PAGE_TRANSITION_TYPED
145 rendererInitiated:NO];
147 addPendingEntry:GURL("http://www.another.url.com")
148 referrer:MakeReferrer("http://www.another.referer.com")
149 transition:ui::PAGE_TRANSITION_TYPED
150 rendererInitiated:NO];
151 [session_controller_ commitPendingEntry];
153 EXPECT_EQ(1U, [[session_controller_ entries] count]);
155 GURL("http://www.another.url.com/"),
156 [session_controller_ URLForSessionAtIndex:0U]);
158 [[session_controller_ entries] objectAtIndex:0U],
159 [session_controller_ currentEntry]);
162 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommitMultiple) {
164 addPendingEntry:GURL("http://www.url.com")
165 referrer:MakeReferrer("http://www.referer.com")
166 transition:ui::PAGE_TRANSITION_TYPED
167 rendererInitiated:NO];
168 [session_controller_ commitPendingEntry];
171 addPendingEntry:GURL("http://www.another.url.com")
172 referrer:MakeReferrer("http://www.another.referer.com")
173 transition:ui::PAGE_TRANSITION_TYPED
174 rendererInitiated:NO];
175 [session_controller_ commitPendingEntry];
177 EXPECT_EQ(2U, [[session_controller_ entries] count]);
179 GURL("http://www.url.com/"),
180 [session_controller_ URLForSessionAtIndex:0U]);
182 GURL("http://www.another.url.com/"),
183 [session_controller_ URLForSessionAtIndex:1U]);
185 [[session_controller_ entries] objectAtIndex:1U],
186 [session_controller_ currentEntry]);
189 TEST_F(CRWSessionControllerTest, AddPendingEntryAndDiscard) {
191 addPendingEntry:GURL("http://www.url.com")
192 referrer:MakeReferrer("http://www.referer.com")
193 transition:ui::PAGE_TRANSITION_TYPED
194 rendererInitiated:NO];
195 [session_controller_ discardNonCommittedEntries];
197 EXPECT_EQ(0U, [[session_controller_ entries] count]);
198 EXPECT_EQ(nil, [session_controller_ currentEntry]);
201 TEST_F(CRWSessionControllerTest, AddPendingEntryAndDiscardAndAddAndCommit) {
203 addPendingEntry:GURL("http://www.url.com")
204 referrer:MakeReferrer("http://www.referer.com")
205 transition:ui::PAGE_TRANSITION_TYPED
206 rendererInitiated:NO];
207 [session_controller_ discardNonCommittedEntries];
210 addPendingEntry:GURL("http://www.another.url.com")
211 referrer:MakeReferrer("http://www.referer.com")
212 transition:ui::PAGE_TRANSITION_TYPED
213 rendererInitiated:NO];
214 [session_controller_ commitPendingEntry];
216 EXPECT_EQ(1U, [[session_controller_ entries] count]);
218 GURL("http://www.another.url.com/"),
219 [session_controller_ URLForSessionAtIndex:0U]);
221 [[session_controller_ entries] objectAtIndex:0U],
222 [session_controller_ currentEntry]);
225 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommitAndAddAndDiscard) {
227 addPendingEntry:GURL("http://www.url.com")
228 referrer:MakeReferrer("http://www.referer.com")
229 transition:ui::PAGE_TRANSITION_TYPED
230 rendererInitiated:NO];
231 [session_controller_ commitPendingEntry];
234 addPendingEntry:GURL("http://www.another.url.com")
235 referrer:MakeReferrer("http://www.referer.com")
236 transition:ui::PAGE_TRANSITION_TYPED
237 rendererInitiated:NO];
238 [session_controller_ discardNonCommittedEntries];
240 EXPECT_EQ(1U, [[session_controller_ entries] count]);
242 GURL("http://www.url.com/"),
243 [session_controller_ URLForSessionAtIndex:0U]);
245 [[session_controller_ entries] objectAtIndex:0U],
246 [session_controller_ currentEntry]);
249 TEST_F(CRWSessionControllerTest,
250 CommitPendingEntryWithoutPendingOrCommittedEntry) {
251 [session_controller_ commitPendingEntry];
253 EXPECT_EQ(0U, [[session_controller_ entries] count]);
254 EXPECT_EQ(nil, [session_controller_ currentEntry]);
257 TEST_F(CRWSessionControllerTest,
258 CommitPendingEntryWithoutPendingEntryWithCommittedEntry) {
259 // Setup committed entry
261 addPendingEntry:GURL("http://www.url.com")
262 referrer:MakeReferrer("http://www.referer.com")
263 transition:ui::PAGE_TRANSITION_TYPED
264 rendererInitiated:NO];
265 [session_controller_ commitPendingEntry];
267 // Commit pending entry when there is no such one
268 [session_controller_ commitPendingEntry];
270 EXPECT_EQ(1U, [[session_controller_ entries] count]);
272 [[session_controller_ entries] objectAtIndex:0U],
273 [session_controller_ currentEntry]);
276 TEST_F(CRWSessionControllerTest,
277 DiscardPendingEntryWithoutPendingOrCommittedEntry) {
278 [session_controller_ discardNonCommittedEntries];
280 EXPECT_EQ(0U, [[session_controller_ entries] count]);
281 EXPECT_EQ(nil, [session_controller_ currentEntry]);
284 TEST_F(CRWSessionControllerTest,
285 DiscardPendingEntryWithoutPendingEntryWithCommittedEntry) {
286 // Setup committed entry
288 addPendingEntry:GURL("http://www.url.com")
289 referrer:MakeReferrer("http://www.referer.com")
290 transition:ui::PAGE_TRANSITION_TYPED
291 rendererInitiated:NO];
292 [session_controller_ commitPendingEntry];
294 // Discard noncommitted entries when there is no such one
295 [session_controller_ discardNonCommittedEntries];
297 EXPECT_EQ(1U, [[session_controller_ entries] count]);
299 [[session_controller_ entries] objectAtIndex:0U],
300 [session_controller_ currentEntry]);
303 TEST_F(CRWSessionControllerTest, UpdatePendingEntryWithoutPendingEntry) {
305 updatePendingEntry:GURL("http://www.another.url.com")];
306 [session_controller_ commitPendingEntry];
308 EXPECT_EQ(0U, [[session_controller_ entries] count]);
309 EXPECT_EQ(nil, [session_controller_ currentEntry]);
312 TEST_F(CRWSessionControllerTest, UpdatePendingEntryWithPendingEntry) {
314 addPendingEntry:GURL("http://www.url.com")
315 referrer:MakeReferrer("http://www.referer.com")
316 transition:ui::PAGE_TRANSITION_TYPED
317 rendererInitiated:NO];
319 updatePendingEntry:GURL("http://www.another.url.com")];
322 GURL("http://www.another.url.com/"),
323 [session_controller_ currentURL]);
326 TEST_F(CRWSessionControllerTest,
327 UpdatePendingEntryWithPendingEntryAlreadyCommited) {
329 addPendingEntry:GURL("http://www.url.com")
330 referrer:MakeReferrer("http://www.referer.com")
331 transition:ui::PAGE_TRANSITION_TYPED
332 rendererInitiated:NO];
333 [session_controller_ commitPendingEntry];
335 updatePendingEntry:GURL("http://www.another.url.com")];
336 [session_controller_ commitPendingEntry];
338 EXPECT_EQ(1U, [[session_controller_ entries] count]);
340 GURL("http://www.url.com/"),
341 [session_controller_ URLForSessionAtIndex:0U]);
343 [[session_controller_ entries] objectAtIndex:0U],
344 [session_controller_ currentEntry]);
347 TEST_F(CRWSessionControllerTest, GoBackWithoutCommitedEntry) {
348 [session_controller_ goBack];
350 EXPECT_EQ(0U, [[session_controller_ entries] count]);
351 EXPECT_EQ(nil, [session_controller_ currentEntry]);
354 TEST_F(CRWSessionControllerTest, GoBackWithSingleCommitedEntry) {
356 addPendingEntry:GURL("http://www.url.com")
357 referrer:MakeReferrer("http://www.referer.com")
358 transition:ui::PAGE_TRANSITION_TYPED
359 rendererInitiated:NO];
360 [session_controller_ commitPendingEntry];
362 [session_controller_ goBack];
364 EXPECT_EQ(1U, [[session_controller_ entries] count]);
366 GURL("http://www.url.com/"),
367 [session_controller_ URLForSessionAtIndex:0U]);
369 [[session_controller_ entries] objectAtIndex:0U],
370 [session_controller_ currentEntry]);
373 TEST_F(CRWSessionControllerTest, GoBackFromTheEnd) {
375 addPendingEntry:GURL("http://www.url.com")
376 referrer:MakeReferrer("http://www.referer.com")
377 transition:ui::PAGE_TRANSITION_TYPED
378 rendererInitiated:NO];
379 [session_controller_ commitPendingEntry];
381 addPendingEntry:GURL("http://www.url2.com")
382 referrer:MakeReferrer("http://www.referer.com")
383 transition:ui::PAGE_TRANSITION_TYPED
384 rendererInitiated:NO];
385 [session_controller_ commitPendingEntry];
387 [session_controller_ goBack];
389 EXPECT_EQ(2U, [[session_controller_ entries] count]);
391 GURL("http://www.url.com/"),
392 [session_controller_ URLForSessionAtIndex:0U]);
394 GURL("http://www.url2.com/"),
395 [session_controller_ URLForSessionAtIndex:1U]);
397 [[session_controller_ entries] objectAtIndex:0U],
398 [session_controller_ currentEntry]);
401 TEST_F(CRWSessionControllerTest, GoBackFromTheBeginning) {
403 addPendingEntry:GURL("http://www.url.com")
404 referrer:MakeReferrer("http://www.referer.com")
405 transition:ui::PAGE_TRANSITION_TYPED
406 rendererInitiated:NO];
407 [session_controller_ commitPendingEntry];
409 addPendingEntry:GURL("http://www.url2.com")
410 referrer:MakeReferrer("http://www.referer.com")
411 transition:ui::PAGE_TRANSITION_TYPED
412 rendererInitiated:NO];
413 [session_controller_ commitPendingEntry];
415 [session_controller_ goBack];
416 [session_controller_ goBack];
418 EXPECT_EQ(2U, [[session_controller_ entries] count]);
420 GURL("http://www.url.com/"),
421 [session_controller_ URLForSessionAtIndex:0U]);
423 GURL("http://www.url2.com/"),
424 [session_controller_ URLForSessionAtIndex:1U]);
426 [[session_controller_ entries] objectAtIndex:0U],
427 [session_controller_ currentEntry]);
430 TEST_F(CRWSessionControllerTest, GoBackFromTheMiddle) {
432 addPendingEntry:GURL("http://www.url.com")
433 referrer:MakeReferrer("http://www.referer.com")
434 transition:ui::PAGE_TRANSITION_TYPED
435 rendererInitiated:NO];
436 [session_controller_ commitPendingEntry];
438 addPendingEntry:GURL("http://www.url2.com")
439 referrer:MakeReferrer("http://www.referer.com")
440 transition:ui::PAGE_TRANSITION_TYPED
441 rendererInitiated:NO];
442 [session_controller_ commitPendingEntry];
444 addPendingEntry:GURL("http://www.url3.com")
445 referrer:MakeReferrer("http://www.referer.com")
446 transition:ui::PAGE_TRANSITION_TYPED
447 rendererInitiated:NO];
448 [session_controller_ commitPendingEntry];
450 addPendingEntry:GURL("http://www.url4.com")
451 referrer:MakeReferrer("http://www.referer.com")
452 transition:ui::PAGE_TRANSITION_TYPED
453 rendererInitiated:NO];
454 [session_controller_ commitPendingEntry];
456 [session_controller_ goBack];
457 [session_controller_ goBack];
459 EXPECT_EQ(4U, [[session_controller_ entries] count]);
461 GURL("http://www.url.com/"),
462 [session_controller_ URLForSessionAtIndex:0U]);
464 GURL("http://www.url2.com/"),
465 [session_controller_ URLForSessionAtIndex:1U]);
467 GURL("http://www.url3.com/"),
468 [session_controller_ URLForSessionAtIndex:2U]);
470 GURL("http://www.url4.com/"),
471 [session_controller_ URLForSessionAtIndex:3U]);
473 [[session_controller_ entries] objectAtIndex:1U],
474 [session_controller_ currentEntry]);
477 TEST_F(CRWSessionControllerTest, GoBackAndRemove) {
479 addPendingEntry:GURL("http://www.url.com")
480 referrer:MakeReferrer("http://www.referer.com")
481 transition:ui::PAGE_TRANSITION_TYPED
482 rendererInitiated:NO];
483 [session_controller_ commitPendingEntry];
485 addPendingEntry:GURL("http://www.url2.com")
486 referrer:MakeReferrer("http://www.referer.com")
487 transition:ui::PAGE_TRANSITION_TYPED
488 rendererInitiated:NO];
489 [session_controller_ commitPendingEntry];
491 [session_controller_ goBack];
492 [session_controller_ removeEntryAtIndex:1];
494 EXPECT_EQ(1U, [[session_controller_ entries] count]);
496 GURL("http://www.url.com/"),
497 [session_controller_ URLForSessionAtIndex:0U]);
499 [[session_controller_ entries] objectAtIndex:0U],
500 [session_controller_ currentEntry]);
501 EXPECT_EQ([session_controller_ currentEntry],
502 [session_controller_ previousEntry]);
505 TEST_F(CRWSessionControllerTest, GoForwardWithoutCommitedEntry) {
506 [session_controller_ goForward];
508 EXPECT_EQ(0U, [[session_controller_ entries] count]);
509 EXPECT_EQ(nil, [session_controller_ currentEntry]);
512 TEST_F(CRWSessionControllerTest, GoForwardWithSingleCommitedEntry) {
514 addPendingEntry:GURL("http://www.url.com")
515 referrer:MakeReferrer("http://www.referer.com")
516 transition:ui::PAGE_TRANSITION_TYPED
517 rendererInitiated:NO];
518 [session_controller_ commitPendingEntry];
520 [session_controller_ goForward];
522 EXPECT_EQ(1U, [[session_controller_ entries] count]);
524 GURL("http://www.url.com/"),
525 [session_controller_ URLForSessionAtIndex:0U]);
527 [[session_controller_ entries] objectAtIndex:0U],
528 [session_controller_ currentEntry]);
531 TEST_F(CRWSessionControllerTest, GoForewardFromTheEnd) {
533 addPendingEntry:GURL("http://www.url.com")
534 referrer:MakeReferrer("http://www.referer.com")
535 transition:ui::PAGE_TRANSITION_TYPED
536 rendererInitiated:NO];
537 [session_controller_ commitPendingEntry];
539 addPendingEntry:GURL("http://www.url2.com")
540 referrer:MakeReferrer("http://www.referer.com")
541 transition:ui::PAGE_TRANSITION_TYPED
542 rendererInitiated:NO];
543 [session_controller_ commitPendingEntry];
545 [session_controller_ goForward];
547 EXPECT_EQ(2U, [[session_controller_ entries] count]);
549 GURL("http://www.url.com/"),
550 [session_controller_ URLForSessionAtIndex:0U]);
552 GURL("http://www.url2.com/"),
553 [session_controller_ URLForSessionAtIndex:1U]);
555 [[session_controller_ entries] objectAtIndex:1U],
556 [session_controller_ currentEntry]);
559 TEST_F(CRWSessionControllerTest, GoForewardFromTheBeginning) {
561 addPendingEntry:GURL("http://www.url.com")
562 referrer:MakeReferrer("http://www.referer.com")
563 transition:ui::PAGE_TRANSITION_TYPED
564 rendererInitiated:NO];
565 [session_controller_ commitPendingEntry];
567 addPendingEntry:GURL("http://www.url2.com")
568 referrer:MakeReferrer("http://www.referer.com")
569 transition:ui::PAGE_TRANSITION_TYPED
570 rendererInitiated:NO];
571 [session_controller_ commitPendingEntry];
573 [session_controller_ goBack];
574 [session_controller_ goForward];
576 EXPECT_EQ(2U, [[session_controller_ entries] count]);
578 GURL("http://www.url.com/"),
579 [session_controller_ URLForSessionAtIndex:0U]);
581 GURL("http://www.url2.com/"),
582 [session_controller_ URLForSessionAtIndex:1U]);
584 [[session_controller_ entries] objectAtIndex:1U],
585 [session_controller_ currentEntry]);
588 TEST_F(CRWSessionControllerTest, GoForwardFromTheMiddle) {
590 addPendingEntry:GURL("http://www.url.com")
591 referrer:MakeReferrer("http://www.referer.com")
592 transition:ui::PAGE_TRANSITION_TYPED
593 rendererInitiated:NO];
594 [session_controller_ commitPendingEntry];
596 addPendingEntry:GURL("http://www.url2.com")
597 referrer:MakeReferrer("http://www.referer.com")
598 transition:ui::PAGE_TRANSITION_TYPED
599 rendererInitiated:NO];
600 [session_controller_ commitPendingEntry];
602 addPendingEntry:GURL("http://www.url3.com")
603 referrer:MakeReferrer("http://www.referer.com")
604 transition:ui::PAGE_TRANSITION_TYPED
605 rendererInitiated:NO];
606 [session_controller_ commitPendingEntry];
608 addPendingEntry:GURL("http://www.url4.com")
609 referrer:MakeReferrer("http://www.referer.com")
610 transition:ui::PAGE_TRANSITION_TYPED
611 rendererInitiated:NO];
612 [session_controller_ commitPendingEntry];
614 [session_controller_ goBack];
615 [session_controller_ goBack];
616 [session_controller_ goForward];
618 EXPECT_EQ(4U, [[session_controller_ entries] count]);
620 GURL("http://www.url.com/"),
621 [session_controller_ URLForSessionAtIndex:0U]);
623 GURL("http://www.url2.com/"),
624 [session_controller_ URLForSessionAtIndex:1U]);
626 GURL("http://www.url3.com/"),
627 [session_controller_ URLForSessionAtIndex:2U]);
629 GURL("http://www.url4.com/"),
630 [session_controller_ URLForSessionAtIndex:3U]);
632 [[session_controller_ entries] objectAtIndex:2U],
633 [session_controller_ currentEntry]);
636 TEST_F(CRWSessionControllerTest, CanGoBackWithoutCommitedEntry) {
637 EXPECT_FALSE([session_controller_ canGoBack]);
640 TEST_F(CRWSessionControllerTest, CanGoBackWithSingleCommitedEntry) {
642 addPendingEntry:GURL("http://www.url.com")
643 referrer:MakeReferrer("http://www.referer.com")
644 transition:ui::PAGE_TRANSITION_TYPED
645 rendererInitiated:NO];
646 [session_controller_ commitPendingEntry];
648 EXPECT_FALSE([session_controller_ canGoBack]);
651 TEST_F(CRWSessionControllerTest, CanGoBackWithMultipleCommitedEntries) {
653 addPendingEntry:GURL("http://www.url.com")
654 referrer:MakeReferrer("http://www.referer.com")
655 transition:ui::PAGE_TRANSITION_TYPED
656 rendererInitiated:NO];
657 [session_controller_ commitPendingEntry];
659 addPendingEntry:GURL("http://www.url1.com")
660 referrer:MakeReferrer("http://www.referer.com")
661 transition:ui::PAGE_TRANSITION_TYPED
662 rendererInitiated:NO];
663 [session_controller_ commitPendingEntry];
665 addPendingEntry:GURL("http://www.url2.com")
666 referrer:MakeReferrer("http://www.referer.com")
667 transition:ui::PAGE_TRANSITION_TYPED
668 rendererInitiated:NO];
669 [session_controller_ commitPendingEntry];
671 EXPECT_TRUE([session_controller_ canGoBack]);
673 [session_controller_ goBack];
674 EXPECT_TRUE([session_controller_ canGoBack]);
676 [session_controller_ goBack];
677 EXPECT_FALSE([session_controller_ canGoBack]);
679 [session_controller_ goBack];
680 EXPECT_FALSE([session_controller_ canGoBack]);
682 [session_controller_ goForward];
683 EXPECT_TRUE([session_controller_ canGoBack]);
686 TEST_F(CRWSessionControllerTest, CanGoForwardWithoutCommitedEntry) {
687 EXPECT_FALSE([session_controller_ canGoBack]);
690 TEST_F(CRWSessionControllerTest, CanGoForwardWithSingleCommitedEntry) {
692 addPendingEntry:GURL("http://www.url.com")
693 referrer:MakeReferrer("http://www.referer.com")
694 transition:ui::PAGE_TRANSITION_TYPED
695 rendererInitiated:NO];
696 [session_controller_ commitPendingEntry];
698 EXPECT_FALSE([session_controller_ canGoBack]);
701 TEST_F(CRWSessionControllerTest, CanGoForwardWithMultipleCommitedEntries) {
703 addPendingEntry:GURL("http://www.url.com")
704 referrer:MakeReferrer("http://www.referer.com")
705 transition:ui::PAGE_TRANSITION_TYPED
706 rendererInitiated:NO];
707 [session_controller_ commitPendingEntry];
709 addPendingEntry:GURL("http://www.url1.com")
710 referrer:MakeReferrer("http://www.referer.com")
711 transition:ui::PAGE_TRANSITION_TYPED
712 rendererInitiated:NO];
713 [session_controller_ commitPendingEntry];
715 addPendingEntry:GURL("http://www.url2.com")
716 referrer:MakeReferrer("http://www.referer.com")
717 transition:ui::PAGE_TRANSITION_TYPED
718 rendererInitiated:NO];
719 [session_controller_ commitPendingEntry];
721 EXPECT_FALSE([session_controller_ canGoForward]);
723 [session_controller_ goBack];
724 EXPECT_TRUE([session_controller_ canGoForward]);
726 [session_controller_ goBack];
727 EXPECT_TRUE([session_controller_ canGoForward]);
729 [session_controller_ goForward];
730 EXPECT_TRUE([session_controller_ canGoForward]);
732 [session_controller_ goForward];
733 EXPECT_FALSE([session_controller_ canGoForward]);
736 // Helper to create a NavigationItem. Caller is responsible for freeing
738 web::NavigationItem* CreateNavigationItem(const std::string& url,
739 const std::string& referrer,
741 web::Referrer referrer_object(GURL(referrer),
742 web::ReferrerPolicyDefault);
743 web::NavigationItemImpl* navigation_item = new web::NavigationItemImpl();
744 navigation_item->SetURL(GURL(url));
745 navigation_item->SetReferrer(referrer_object);
746 navigation_item->SetTitle(base::SysNSStringToUTF16(title));
747 navigation_item->SetTransitionType(ui::PAGE_TRANSITION_TYPED);
749 return navigation_item;
752 TEST_F(CRWSessionControllerTest, CreateWithEmptyNavigations) {
753 ScopedVector<web::NavigationItem> items;
754 base::scoped_nsobject<CRWSessionController> controller(
755 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
757 browserState:&browser_state_]);
758 EXPECT_EQ(controller.get().entries.count, 0U);
759 EXPECT_EQ(controller.get().currentNavigationIndex, -1);
760 EXPECT_EQ(controller.get().previousNavigationIndex, -1);
761 EXPECT_FALSE(controller.get().currentEntry);
764 TEST_F(CRWSessionControllerTest, CreateWithNavList) {
765 ScopedVector<web::NavigationItem> items;
766 items.push_back(CreateNavigationItem("http://www.google.com",
767 "http://www.referrer.com", @"Google"));
768 items.push_back(CreateNavigationItem("http://www.yahoo.com",
769 "http://www.google.com", @"Yahoo"));
770 items.push_back(CreateNavigationItem("http://www.espn.com",
771 "http://www.nothing.com", @"ESPN"));
772 base::scoped_nsobject<CRWSessionController> controller(
773 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
775 browserState:&browser_state_]);
777 EXPECT_EQ(controller.get().entries.count, 3U);
778 EXPECT_EQ(controller.get().currentNavigationIndex, 1);
779 EXPECT_EQ(controller.get().previousNavigationIndex, -1);
780 // Sanity check the current entry, the CRWSessionEntry unit test will ensure
781 // the entire object is created properly.
782 CRWSessionEntry* current_entry = controller.get().currentEntry;
783 EXPECT_EQ(current_entry.navigationItem->GetURL(),
784 GURL("http://www.yahoo.com"));
785 EXPECT_EQ([[controller openerId] length], 0UL);
788 TEST_F(CRWSessionControllerTest, PreviousNavigationEntry) {
790 addPendingEntry:GURL("http://www.url.com")
791 referrer:MakeReferrer("http://www.referer.com")
792 transition:ui::PAGE_TRANSITION_TYPED
793 rendererInitiated:NO];
794 [session_controller_ commitPendingEntry];
796 addPendingEntry:GURL("http://www.url1.com")
797 referrer:MakeReferrer("http://www.referer.com")
798 transition:ui::PAGE_TRANSITION_TYPED
799 rendererInitiated:NO];
800 [session_controller_ commitPendingEntry];
802 addPendingEntry:GURL("http://www.url2.com")
803 referrer:MakeReferrer("http://www.referer.com")
804 transition:ui::PAGE_TRANSITION_TYPED
805 rendererInitiated:NO];
806 [session_controller_ commitPendingEntry];
808 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 1);
810 [session_controller_ goBack];
811 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 2);
813 [session_controller_ goBack];
814 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 1);
816 [session_controller_ goForward];
817 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 0);
819 [session_controller_ goForward];
820 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 1);
823 TEST_F(CRWSessionControllerTest, PushNewEntry) {
824 ScopedVector<web::NavigationItem> items;
825 items.push_back(CreateNavigationItem("http://www.firstpage.com",
826 "http://www.starturl.com", @"First"));
827 items.push_back(CreateNavigationItem("http://www.secondpage.com",
828 "http://www.firstpage.com", @"Second"));
829 items.push_back(CreateNavigationItem("http://www.thirdpage.com",
830 "http://www.secondpage.com", @"Third"));
831 base::scoped_nsobject<CRWSessionController> controller(
832 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
834 browserState:&browser_state_]);
836 GURL pushPageGurl1("http://www.firstpage.com/#push1");
837 NSString* stateObject1 = @"{'foo': 1}";
838 [controller pushNewEntryWithURL:pushPageGurl1 stateObject:stateObject1];
839 CRWSessionEntry* pushedEntry = [controller currentEntry];
840 NSUInteger expectedCount = 2;
841 EXPECT_EQ(expectedCount, controller.get().entries.count);
842 EXPECT_EQ(pushPageGurl1, pushedEntry.navigationItem->GetURL());
843 EXPECT_TRUE(pushedEntry.createdFromPushState);
844 EXPECT_NSEQ(stateObject1, pushedEntry.serializedStateObject);
845 EXPECT_EQ(GURL("http://www.firstpage.com/"),
846 pushedEntry.navigationItem->GetReferrer().url);
848 // Add another new entry and check size and fields again.
849 GURL pushPageGurl2("http://www.firstpage.com/push2");
850 [controller pushNewEntryWithURL:pushPageGurl2 stateObject:nil];
851 pushedEntry = [controller currentEntry];
853 EXPECT_EQ(expectedCount, controller.get().entries.count);
854 EXPECT_EQ(pushPageGurl2, pushedEntry.navigationItem->GetURL());
855 EXPECT_TRUE(pushedEntry.createdFromPushState);
856 EXPECT_EQ(nil, pushedEntry.serializedStateObject);
857 EXPECT_EQ(pushPageGurl1, pushedEntry.navigationItem->GetReferrer().url);
860 TEST_F(CRWSessionControllerTest, IsPushStateNavigation) {
861 ScopedVector<web::NavigationItem> items;
863 CreateNavigationItem("http://foo.com", "http://google.com", @"First"));
864 // Push state navigation.
866 CreateNavigationItem("http://foo.com#bar", "http://foo.com", @"Second"));
867 items.push_back(CreateNavigationItem("http://google.com",
868 "http://foo.com#bar", @"Third"));
870 CreateNavigationItem("http://foo.com", "http://google.com", @"Fourth"));
871 // Push state navigation.
873 CreateNavigationItem("http://foo.com/bar", "http://foo.com", @"Fifth"));
874 // Push state navigation.
875 items.push_back(CreateNavigationItem("http://foo.com/bar#bar",
876 "http://foo.com/bar", @"Sixth"));
877 base::scoped_nsobject<CRWSessionController> controller(
878 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
880 browserState:&browser_state_]);
881 CRWSessionEntry* entry0 = [controller.get().entries objectAtIndex:0];
882 CRWSessionEntry* entry1 = [controller.get().entries objectAtIndex:1];
883 CRWSessionEntry* entry2 = [controller.get().entries objectAtIndex:2];
884 CRWSessionEntry* entry3 = [controller.get().entries objectAtIndex:3];
885 CRWSessionEntry* entry4 = [controller.get().entries objectAtIndex:4];
886 CRWSessionEntry* entry5 = [controller.get().entries objectAtIndex:5];
887 entry1.createdFromPushState = YES;
888 entry4.createdFromPushState = YES;
889 entry5.createdFromPushState = YES;
892 [controller isPushStateNavigationBetweenEntry:entry0 andEntry:entry1]);
894 [controller isPushStateNavigationBetweenEntry:entry5 andEntry:entry3]);
896 [controller isPushStateNavigationBetweenEntry:entry4 andEntry:entry3]);
898 [controller isPushStateNavigationBetweenEntry:entry1 andEntry:entry2]);
900 [controller isPushStateNavigationBetweenEntry:entry0 andEntry:entry5]);
902 [controller isPushStateNavigationBetweenEntry:entry2 andEntry:entry4]);
905 TEST_F(CRWSessionControllerTest, UpdateCurrentEntry) {
906 ScopedVector<web::NavigationItem> items;
907 items.push_back(CreateNavigationItem("http://www.firstpage.com",
908 "http://www.starturl.com", @"First"));
909 items.push_back(CreateNavigationItem("http://www.secondpage.com",
910 "http://www.firstpage.com", @"Second"));
911 items.push_back(CreateNavigationItem("http://www.thirdpage.com",
912 "http://www.secondpage.com", @"Third"));
913 base::scoped_nsobject<CRWSessionController> controller(
914 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
916 browserState:&browser_state_]);
918 GURL replacePageGurl1("http://www.firstpage.com/#replace1");
919 NSString* stateObject1 = @"{'foo': 1}";
921 // Replace current entry and check the size of history and fields of the
923 [controller updateCurrentEntryWithURL:replacePageGurl1
924 stateObject:stateObject1];
925 CRWSessionEntry* replacedEntry = [controller currentEntry];
926 NSUInteger expectedCount = 3;
927 EXPECT_EQ(expectedCount, controller.get().entries.count);
928 EXPECT_EQ(replacePageGurl1, replacedEntry.navigationItem->GetURL());
929 EXPECT_FALSE(replacedEntry.createdFromPushState);
930 EXPECT_NSEQ(stateObject1, replacedEntry.serializedStateObject);
931 EXPECT_EQ(GURL("http://www.starturl.com/"),
932 replacedEntry.navigationItem->GetReferrer().url);
934 // Replace current entry and check size and fields again.
935 GURL replacePageGurl2("http://www.firstpage.com/#replace2");
936 [controller.get() updateCurrentEntryWithURL:replacePageGurl2 stateObject:nil];
937 replacedEntry = [controller currentEntry];
938 EXPECT_EQ(expectedCount, controller.get().entries.count);
939 EXPECT_EQ(replacePageGurl2, replacedEntry.navigationItem->GetURL());
940 EXPECT_FALSE(replacedEntry.createdFromPushState);
941 EXPECT_NSEQ(nil, replacedEntry.serializedStateObject);
942 EXPECT_EQ(GURL("http://www.starturl.com/"),
943 replacedEntry.navigationItem->GetReferrer().url);
946 TEST_F(CRWSessionControllerTest, TestBackwardForwardEntries) {
947 [session_controller_ addPendingEntry:GURL("http://www.example.com/0")
948 referrer:MakeReferrer("http://www.example.com/a")
949 transition:ui::PAGE_TRANSITION_LINK
950 rendererInitiated:NO];
951 [session_controller_ commitPendingEntry];
952 [session_controller_ addPendingEntry:GURL("http://www.example.com/1")
953 referrer:MakeReferrer("http://www.example.com/b")
954 transition:ui::PAGE_TRANSITION_LINK
955 rendererInitiated:NO];
956 [session_controller_ commitPendingEntry];
957 [session_controller_ addPendingEntry:GURL("http://www.example.com/redirect")
958 referrer:MakeReferrer("http://www.example.com/r")
959 transition:ui::PAGE_TRANSITION_IS_REDIRECT_MASK
960 rendererInitiated:NO];
961 [session_controller_ commitPendingEntry];
962 [session_controller_ addPendingEntry:GURL("http://www.example.com/2")
963 referrer:MakeReferrer("http://www.example.com/c")
964 transition:ui::PAGE_TRANSITION_LINK
965 rendererInitiated:NO];
966 [session_controller_ commitPendingEntry];
968 EXPECT_EQ(3, session_controller_.get().currentNavigationIndex);
969 NSArray* backEntries = [session_controller_ backwardEntries];
970 EXPECT_EQ(2U, [backEntries count]);
971 EXPECT_EQ(0U, [[session_controller_ forwardEntries] count]);
972 EXPECT_EQ("http://www.example.com/1",
973 [[backEntries objectAtIndex:0] navigationItem]->GetURL().spec());
975 [session_controller_ goBack];
976 EXPECT_EQ(1U, [[session_controller_ backwardEntries] count]);
977 EXPECT_EQ(1U, [[session_controller_ forwardEntries] count]);
979 [session_controller_ goBack];
980 NSArray* forwardEntries = [session_controller_ forwardEntries];
981 EXPECT_EQ(0U, [[session_controller_ backwardEntries] count]);
982 EXPECT_EQ(2U, [forwardEntries count]);
983 EXPECT_EQ("http://www.example.com/2",
984 [[forwardEntries objectAtIndex:1] navigationItem]->GetURL().spec());
987 TEST_F(CRWSessionControllerTest, GoToEntry) {
988 [session_controller_ addPendingEntry:GURL("http://www.example.com/0")
989 referrer:MakeReferrer("http://www.example.com/a")
990 transition:ui::PAGE_TRANSITION_LINK
991 rendererInitiated:NO];
992 [session_controller_ commitPendingEntry];
993 [session_controller_ addPendingEntry:GURL("http://www.example.com/1")
994 referrer:MakeReferrer("http://www.example.com/b")
995 transition:ui::PAGE_TRANSITION_LINK
996 rendererInitiated:NO];
997 [session_controller_ commitPendingEntry];
998 [session_controller_ addPendingEntry:GURL("http://www.example.com/redirect")
999 referrer:MakeReferrer("http://www.example.com/r")
1000 transition:ui::PAGE_TRANSITION_IS_REDIRECT_MASK
1001 rendererInitiated:NO];
1002 [session_controller_ commitPendingEntry];
1003 [session_controller_ addPendingEntry:GURL("http://www.example.com/2")
1004 referrer:MakeReferrer("http://www.example.com/c")
1005 transition:ui::PAGE_TRANSITION_LINK
1006 rendererInitiated:NO];
1007 [session_controller_ commitPendingEntry];
1008 EXPECT_EQ(3, session_controller_.get().currentNavigationIndex);
1010 CRWSessionEntry* entry1 = [session_controller_.get().entries objectAtIndex:1];
1011 [session_controller_ goToEntry:entry1];
1012 EXPECT_EQ(1, session_controller_.get().currentNavigationIndex);
1014 // Remove an entry and attempt to go it. Ensure it outlives the removal.
1015 base::scoped_nsobject<CRWSessionEntry> entry3(
1016 [[session_controller_.get().entries objectAtIndex:3] retain]);
1017 [session_controller_ removeEntryAtIndex:3];
1018 [session_controller_ goToEntry:entry3];
1019 EXPECT_EQ(1, session_controller_.get().currentNavigationIndex);
1022 } // anonymous namespace