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 #include "ios/web/public/test/test_web_thread_bundle.h"
18 #import "net/base/mac/url_conversions.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/gtest_mac.h"
21 #include "testing/platform_test.h"
23 @interface CRWSessionController (Testing)
24 - (const GURL&)URLForSessionAtIndex:(NSUInteger)index;
25 - (const GURL&)currentURL;
28 @implementation CRWSessionController (Testing)
29 - (const GURL&)URLForSessionAtIndex:(NSUInteger)index {
30 CRWSessionEntry* entry =
31 static_cast<CRWSessionEntry*>([self.entries objectAtIndex:index]);
32 return entry.navigationItem->GetURL();
35 - (const GURL&)currentURL {
36 DCHECK([self currentEntry]);
37 return [self currentEntry].navigationItem->GetURL();
43 class CRWSessionControllerTest : public PlatformTest {
45 void SetUp() override {
46 session_controller_.reset(
47 [[CRWSessionController alloc] initWithWindowName:@"test window"
50 openerNavigationIndex:0
51 browserState:&browser_state_]);
54 web::Referrer MakeReferrer(std::string url) {
55 return web::Referrer(GURL(url), web::ReferrerPolicyDefault);
58 web::TestWebThreadBundle thread_bundle_;
59 web::TestBrowserState browser_state_;
60 base::scoped_nsobject<CRWSessionController> session_controller_;
63 TEST_F(CRWSessionControllerTest, InitWithWindowName) {
64 EXPECT_NSEQ(@"test window", [session_controller_ windowName]);
65 EXPECT_NSEQ(@"opener", [session_controller_ openerId]);
66 EXPECT_FALSE([session_controller_ isOpenedByDOM]);
67 EXPECT_EQ(0U, [[session_controller_ entries] count]);
68 EXPECT_EQ(nil, [session_controller_ currentEntry]);
71 TEST_F(CRWSessionControllerTest, AddPendingEntry) {
73 addPendingEntry:GURL("http://www.url.com")
74 referrer:MakeReferrer("http://www.referer.com")
75 transition:ui::PAGE_TRANSITION_TYPED
76 rendererInitiated:NO];
78 EXPECT_EQ(0U, [[session_controller_ entries] count]);
80 GURL("http://www.url.com/"),
81 [session_controller_ currentURL]);
84 TEST_F(CRWSessionControllerTest, AddPendingEntryWithCommittedEntries) {
86 addPendingEntry:GURL("http://www.committed.url.com")
87 referrer:MakeReferrer("http://www.referer.com")
88 transition:ui::PAGE_TRANSITION_TYPED
89 rendererInitiated:NO];
90 [session_controller_ commitPendingEntry];
93 addPendingEntry:GURL("http://www.url.com")
94 referrer:MakeReferrer("http://www.referer.com")
95 transition:ui::PAGE_TRANSITION_TYPED
96 rendererInitiated:NO];
98 EXPECT_EQ(1U, [[session_controller_ entries] count]);
100 GURL("http://www.committed.url.com/"),
101 [session_controller_ URLForSessionAtIndex:0U]);
103 GURL("http://www.url.com/"),
104 [session_controller_ currentURL]);
107 TEST_F(CRWSessionControllerTest, AddPendingEntryOverriding) {
109 addPendingEntry:GURL("http://www.url.com")
110 referrer:MakeReferrer("http://www.referer.com")
111 transition:ui::PAGE_TRANSITION_TYPED
112 rendererInitiated:NO];
114 addPendingEntry:GURL("http://www.another.url.com")
115 referrer:MakeReferrer("http://www.another.referer.com")
116 transition:ui::PAGE_TRANSITION_TYPED
117 rendererInitiated:NO];
119 EXPECT_EQ(0U, [[session_controller_ entries] count]);
121 GURL("http://www.another.url.com/"),
122 [session_controller_ currentURL]);
125 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommit) {
127 addPendingEntry:GURL("http://www.url.com")
128 referrer:MakeReferrer("http://www.referer.com")
129 transition:ui::PAGE_TRANSITION_TYPED
130 rendererInitiated:NO];
131 [session_controller_ commitPendingEntry];
133 EXPECT_EQ(1U, [[session_controller_ entries] count]);
135 GURL("http://www.url.com/"),
136 [session_controller_ URLForSessionAtIndex:0U]);
138 [[session_controller_ entries] objectAtIndex:0U],
139 [session_controller_ currentEntry]);
142 TEST_F(CRWSessionControllerTest, AddPendingEntryOverridingAndCommit) {
144 addPendingEntry:GURL("http://www.url.com")
145 referrer:MakeReferrer("http://www.referer.com")
146 transition:ui::PAGE_TRANSITION_TYPED
147 rendererInitiated:NO];
149 addPendingEntry:GURL("http://www.another.url.com")
150 referrer:MakeReferrer("http://www.another.referer.com")
151 transition:ui::PAGE_TRANSITION_TYPED
152 rendererInitiated:NO];
153 [session_controller_ commitPendingEntry];
155 EXPECT_EQ(1U, [[session_controller_ entries] count]);
157 GURL("http://www.another.url.com/"),
158 [session_controller_ URLForSessionAtIndex:0U]);
160 [[session_controller_ entries] objectAtIndex:0U],
161 [session_controller_ currentEntry]);
164 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommitMultiple) {
166 addPendingEntry:GURL("http://www.url.com")
167 referrer:MakeReferrer("http://www.referer.com")
168 transition:ui::PAGE_TRANSITION_TYPED
169 rendererInitiated:NO];
170 [session_controller_ commitPendingEntry];
173 addPendingEntry:GURL("http://www.another.url.com")
174 referrer:MakeReferrer("http://www.another.referer.com")
175 transition:ui::PAGE_TRANSITION_TYPED
176 rendererInitiated:NO];
177 [session_controller_ commitPendingEntry];
179 EXPECT_EQ(2U, [[session_controller_ entries] count]);
181 GURL("http://www.url.com/"),
182 [session_controller_ URLForSessionAtIndex:0U]);
184 GURL("http://www.another.url.com/"),
185 [session_controller_ URLForSessionAtIndex:1U]);
187 [[session_controller_ entries] objectAtIndex:1U],
188 [session_controller_ currentEntry]);
191 TEST_F(CRWSessionControllerTest, AddPendingEntryAndDiscard) {
193 addPendingEntry:GURL("http://www.url.com")
194 referrer:MakeReferrer("http://www.referer.com")
195 transition:ui::PAGE_TRANSITION_TYPED
196 rendererInitiated:NO];
197 [session_controller_ discardNonCommittedEntries];
199 EXPECT_EQ(0U, [[session_controller_ entries] count]);
200 EXPECT_EQ(nil, [session_controller_ currentEntry]);
203 TEST_F(CRWSessionControllerTest, AddPendingEntryAndDiscardAndAddAndCommit) {
205 addPendingEntry:GURL("http://www.url.com")
206 referrer:MakeReferrer("http://www.referer.com")
207 transition:ui::PAGE_TRANSITION_TYPED
208 rendererInitiated:NO];
209 [session_controller_ discardNonCommittedEntries];
212 addPendingEntry:GURL("http://www.another.url.com")
213 referrer:MakeReferrer("http://www.referer.com")
214 transition:ui::PAGE_TRANSITION_TYPED
215 rendererInitiated:NO];
216 [session_controller_ commitPendingEntry];
218 EXPECT_EQ(1U, [[session_controller_ entries] count]);
220 GURL("http://www.another.url.com/"),
221 [session_controller_ URLForSessionAtIndex:0U]);
223 [[session_controller_ entries] objectAtIndex:0U],
224 [session_controller_ currentEntry]);
227 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommitAndAddAndDiscard) {
229 addPendingEntry:GURL("http://www.url.com")
230 referrer:MakeReferrer("http://www.referer.com")
231 transition:ui::PAGE_TRANSITION_TYPED
232 rendererInitiated:NO];
233 [session_controller_ commitPendingEntry];
236 addPendingEntry:GURL("http://www.another.url.com")
237 referrer:MakeReferrer("http://www.referer.com")
238 transition:ui::PAGE_TRANSITION_TYPED
239 rendererInitiated:NO];
240 [session_controller_ discardNonCommittedEntries];
242 EXPECT_EQ(1U, [[session_controller_ entries] count]);
244 GURL("http://www.url.com/"),
245 [session_controller_ URLForSessionAtIndex:0U]);
247 [[session_controller_ entries] objectAtIndex:0U],
248 [session_controller_ currentEntry]);
251 TEST_F(CRWSessionControllerTest,
252 CommitPendingEntryWithoutPendingOrCommittedEntry) {
253 [session_controller_ commitPendingEntry];
255 EXPECT_EQ(0U, [[session_controller_ entries] count]);
256 EXPECT_EQ(nil, [session_controller_ currentEntry]);
259 TEST_F(CRWSessionControllerTest,
260 CommitPendingEntryWithoutPendingEntryWithCommittedEntry) {
261 // Setup committed entry
263 addPendingEntry:GURL("http://www.url.com")
264 referrer:MakeReferrer("http://www.referer.com")
265 transition:ui::PAGE_TRANSITION_TYPED
266 rendererInitiated:NO];
267 [session_controller_ commitPendingEntry];
269 // Commit pending entry when there is no such one
270 [session_controller_ commitPendingEntry];
272 EXPECT_EQ(1U, [[session_controller_ entries] count]);
274 [[session_controller_ entries] objectAtIndex:0U],
275 [session_controller_ currentEntry]);
278 TEST_F(CRWSessionControllerTest,
279 DiscardPendingEntryWithoutPendingOrCommittedEntry) {
280 [session_controller_ discardNonCommittedEntries];
282 EXPECT_EQ(0U, [[session_controller_ entries] count]);
283 EXPECT_EQ(nil, [session_controller_ currentEntry]);
286 TEST_F(CRWSessionControllerTest,
287 DiscardPendingEntryWithoutPendingEntryWithCommittedEntry) {
288 // Setup committed entry
290 addPendingEntry:GURL("http://www.url.com")
291 referrer:MakeReferrer("http://www.referer.com")
292 transition:ui::PAGE_TRANSITION_TYPED
293 rendererInitiated:NO];
294 [session_controller_ commitPendingEntry];
296 // Discard noncommitted entries when there is no such one
297 [session_controller_ discardNonCommittedEntries];
299 EXPECT_EQ(1U, [[session_controller_ entries] count]);
301 [[session_controller_ entries] objectAtIndex:0U],
302 [session_controller_ currentEntry]);
305 TEST_F(CRWSessionControllerTest, UpdatePendingEntryWithoutPendingEntry) {
307 updatePendingEntry:GURL("http://www.another.url.com")];
308 [session_controller_ commitPendingEntry];
310 EXPECT_EQ(0U, [[session_controller_ entries] count]);
311 EXPECT_EQ(nil, [session_controller_ currentEntry]);
314 TEST_F(CRWSessionControllerTest, UpdatePendingEntryWithPendingEntry) {
316 addPendingEntry:GURL("http://www.url.com")
317 referrer:MakeReferrer("http://www.referer.com")
318 transition:ui::PAGE_TRANSITION_TYPED
319 rendererInitiated:NO];
321 updatePendingEntry:GURL("http://www.another.url.com")];
324 GURL("http://www.another.url.com/"),
325 [session_controller_ currentURL]);
328 TEST_F(CRWSessionControllerTest,
329 UpdatePendingEntryWithPendingEntryAlreadyCommited) {
331 addPendingEntry:GURL("http://www.url.com")
332 referrer:MakeReferrer("http://www.referer.com")
333 transition:ui::PAGE_TRANSITION_TYPED
334 rendererInitiated:NO];
335 [session_controller_ commitPendingEntry];
337 updatePendingEntry:GURL("http://www.another.url.com")];
338 [session_controller_ commitPendingEntry];
340 EXPECT_EQ(1U, [[session_controller_ entries] count]);
342 GURL("http://www.url.com/"),
343 [session_controller_ URLForSessionAtIndex:0U]);
345 [[session_controller_ entries] objectAtIndex:0U],
346 [session_controller_ currentEntry]);
349 TEST_F(CRWSessionControllerTest, GoBackWithoutCommitedEntry) {
350 [session_controller_ goBack];
352 EXPECT_EQ(0U, [[session_controller_ entries] count]);
353 EXPECT_EQ(nil, [session_controller_ currentEntry]);
356 TEST_F(CRWSessionControllerTest, GoBackWithSingleCommitedEntry) {
358 addPendingEntry:GURL("http://www.url.com")
359 referrer:MakeReferrer("http://www.referer.com")
360 transition:ui::PAGE_TRANSITION_TYPED
361 rendererInitiated:NO];
362 [session_controller_ commitPendingEntry];
364 [session_controller_ goBack];
366 EXPECT_EQ(1U, [[session_controller_ entries] count]);
368 GURL("http://www.url.com/"),
369 [session_controller_ URLForSessionAtIndex:0U]);
371 [[session_controller_ entries] objectAtIndex:0U],
372 [session_controller_ currentEntry]);
375 TEST_F(CRWSessionControllerTest, GoBackFromTheEnd) {
377 addPendingEntry:GURL("http://www.url.com")
378 referrer:MakeReferrer("http://www.referer.com")
379 transition:ui::PAGE_TRANSITION_TYPED
380 rendererInitiated:NO];
381 [session_controller_ commitPendingEntry];
383 addPendingEntry:GURL("http://www.url2.com")
384 referrer:MakeReferrer("http://www.referer.com")
385 transition:ui::PAGE_TRANSITION_TYPED
386 rendererInitiated:NO];
387 [session_controller_ commitPendingEntry];
389 [session_controller_ goBack];
391 EXPECT_EQ(2U, [[session_controller_ entries] count]);
393 GURL("http://www.url.com/"),
394 [session_controller_ URLForSessionAtIndex:0U]);
396 GURL("http://www.url2.com/"),
397 [session_controller_ URLForSessionAtIndex:1U]);
399 [[session_controller_ entries] objectAtIndex:0U],
400 [session_controller_ currentEntry]);
403 TEST_F(CRWSessionControllerTest, GoBackFromTheBeginning) {
405 addPendingEntry:GURL("http://www.url.com")
406 referrer:MakeReferrer("http://www.referer.com")
407 transition:ui::PAGE_TRANSITION_TYPED
408 rendererInitiated:NO];
409 [session_controller_ commitPendingEntry];
411 addPendingEntry:GURL("http://www.url2.com")
412 referrer:MakeReferrer("http://www.referer.com")
413 transition:ui::PAGE_TRANSITION_TYPED
414 rendererInitiated:NO];
415 [session_controller_ commitPendingEntry];
417 [session_controller_ goBack];
418 [session_controller_ goBack];
420 EXPECT_EQ(2U, [[session_controller_ entries] count]);
422 GURL("http://www.url.com/"),
423 [session_controller_ URLForSessionAtIndex:0U]);
425 GURL("http://www.url2.com/"),
426 [session_controller_ URLForSessionAtIndex:1U]);
428 [[session_controller_ entries] objectAtIndex:0U],
429 [session_controller_ currentEntry]);
432 TEST_F(CRWSessionControllerTest, GoBackFromTheMiddle) {
434 addPendingEntry:GURL("http://www.url.com")
435 referrer:MakeReferrer("http://www.referer.com")
436 transition:ui::PAGE_TRANSITION_TYPED
437 rendererInitiated:NO];
438 [session_controller_ commitPendingEntry];
440 addPendingEntry:GURL("http://www.url2.com")
441 referrer:MakeReferrer("http://www.referer.com")
442 transition:ui::PAGE_TRANSITION_TYPED
443 rendererInitiated:NO];
444 [session_controller_ commitPendingEntry];
446 addPendingEntry:GURL("http://www.url3.com")
447 referrer:MakeReferrer("http://www.referer.com")
448 transition:ui::PAGE_TRANSITION_TYPED
449 rendererInitiated:NO];
450 [session_controller_ commitPendingEntry];
452 addPendingEntry:GURL("http://www.url4.com")
453 referrer:MakeReferrer("http://www.referer.com")
454 transition:ui::PAGE_TRANSITION_TYPED
455 rendererInitiated:NO];
456 [session_controller_ commitPendingEntry];
458 [session_controller_ goBack];
459 [session_controller_ goBack];
461 EXPECT_EQ(4U, [[session_controller_ entries] count]);
463 GURL("http://www.url.com/"),
464 [session_controller_ URLForSessionAtIndex:0U]);
466 GURL("http://www.url2.com/"),
467 [session_controller_ URLForSessionAtIndex:1U]);
469 GURL("http://www.url3.com/"),
470 [session_controller_ URLForSessionAtIndex:2U]);
472 GURL("http://www.url4.com/"),
473 [session_controller_ URLForSessionAtIndex:3U]);
475 [[session_controller_ entries] objectAtIndex:1U],
476 [session_controller_ currentEntry]);
479 TEST_F(CRWSessionControllerTest, GoBackAndRemove) {
481 addPendingEntry:GURL("http://www.url.com")
482 referrer:MakeReferrer("http://www.referer.com")
483 transition:ui::PAGE_TRANSITION_TYPED
484 rendererInitiated:NO];
485 [session_controller_ commitPendingEntry];
487 addPendingEntry:GURL("http://www.url2.com")
488 referrer:MakeReferrer("http://www.referer.com")
489 transition:ui::PAGE_TRANSITION_TYPED
490 rendererInitiated:NO];
491 [session_controller_ commitPendingEntry];
493 [session_controller_ goBack];
494 [session_controller_ removeEntryAtIndex:1];
496 EXPECT_EQ(1U, [[session_controller_ entries] count]);
498 GURL("http://www.url.com/"),
499 [session_controller_ URLForSessionAtIndex:0U]);
501 [[session_controller_ entries] objectAtIndex:0U],
502 [session_controller_ currentEntry]);
503 EXPECT_EQ([session_controller_ currentEntry],
504 [session_controller_ previousEntry]);
507 TEST_F(CRWSessionControllerTest, GoForwardWithoutCommitedEntry) {
508 [session_controller_ goForward];
510 EXPECT_EQ(0U, [[session_controller_ entries] count]);
511 EXPECT_EQ(nil, [session_controller_ currentEntry]);
514 TEST_F(CRWSessionControllerTest, GoForwardWithSingleCommitedEntry) {
516 addPendingEntry:GURL("http://www.url.com")
517 referrer:MakeReferrer("http://www.referer.com")
518 transition:ui::PAGE_TRANSITION_TYPED
519 rendererInitiated:NO];
520 [session_controller_ commitPendingEntry];
522 [session_controller_ goForward];
524 EXPECT_EQ(1U, [[session_controller_ entries] count]);
526 GURL("http://www.url.com/"),
527 [session_controller_ URLForSessionAtIndex:0U]);
529 [[session_controller_ entries] objectAtIndex:0U],
530 [session_controller_ currentEntry]);
533 TEST_F(CRWSessionControllerTest, GoForewardFromTheEnd) {
535 addPendingEntry:GURL("http://www.url.com")
536 referrer:MakeReferrer("http://www.referer.com")
537 transition:ui::PAGE_TRANSITION_TYPED
538 rendererInitiated:NO];
539 [session_controller_ commitPendingEntry];
541 addPendingEntry:GURL("http://www.url2.com")
542 referrer:MakeReferrer("http://www.referer.com")
543 transition:ui::PAGE_TRANSITION_TYPED
544 rendererInitiated:NO];
545 [session_controller_ commitPendingEntry];
547 [session_controller_ goForward];
549 EXPECT_EQ(2U, [[session_controller_ entries] count]);
551 GURL("http://www.url.com/"),
552 [session_controller_ URLForSessionAtIndex:0U]);
554 GURL("http://www.url2.com/"),
555 [session_controller_ URLForSessionAtIndex:1U]);
557 [[session_controller_ entries] objectAtIndex:1U],
558 [session_controller_ currentEntry]);
561 TEST_F(CRWSessionControllerTest, GoForewardFromTheBeginning) {
563 addPendingEntry:GURL("http://www.url.com")
564 referrer:MakeReferrer("http://www.referer.com")
565 transition:ui::PAGE_TRANSITION_TYPED
566 rendererInitiated:NO];
567 [session_controller_ commitPendingEntry];
569 addPendingEntry:GURL("http://www.url2.com")
570 referrer:MakeReferrer("http://www.referer.com")
571 transition:ui::PAGE_TRANSITION_TYPED
572 rendererInitiated:NO];
573 [session_controller_ commitPendingEntry];
575 [session_controller_ goBack];
576 [session_controller_ goForward];
578 EXPECT_EQ(2U, [[session_controller_ entries] count]);
580 GURL("http://www.url.com/"),
581 [session_controller_ URLForSessionAtIndex:0U]);
583 GURL("http://www.url2.com/"),
584 [session_controller_ URLForSessionAtIndex:1U]);
586 [[session_controller_ entries] objectAtIndex:1U],
587 [session_controller_ currentEntry]);
590 TEST_F(CRWSessionControllerTest, GoForwardFromTheMiddle) {
592 addPendingEntry:GURL("http://www.url.com")
593 referrer:MakeReferrer("http://www.referer.com")
594 transition:ui::PAGE_TRANSITION_TYPED
595 rendererInitiated:NO];
596 [session_controller_ commitPendingEntry];
598 addPendingEntry:GURL("http://www.url2.com")
599 referrer:MakeReferrer("http://www.referer.com")
600 transition:ui::PAGE_TRANSITION_TYPED
601 rendererInitiated:NO];
602 [session_controller_ commitPendingEntry];
604 addPendingEntry:GURL("http://www.url3.com")
605 referrer:MakeReferrer("http://www.referer.com")
606 transition:ui::PAGE_TRANSITION_TYPED
607 rendererInitiated:NO];
608 [session_controller_ commitPendingEntry];
610 addPendingEntry:GURL("http://www.url4.com")
611 referrer:MakeReferrer("http://www.referer.com")
612 transition:ui::PAGE_TRANSITION_TYPED
613 rendererInitiated:NO];
614 [session_controller_ commitPendingEntry];
616 [session_controller_ goBack];
617 [session_controller_ goBack];
618 [session_controller_ goForward];
620 EXPECT_EQ(4U, [[session_controller_ entries] count]);
622 GURL("http://www.url.com/"),
623 [session_controller_ URLForSessionAtIndex:0U]);
625 GURL("http://www.url2.com/"),
626 [session_controller_ URLForSessionAtIndex:1U]);
628 GURL("http://www.url3.com/"),
629 [session_controller_ URLForSessionAtIndex:2U]);
631 GURL("http://www.url4.com/"),
632 [session_controller_ URLForSessionAtIndex:3U]);
634 [[session_controller_ entries] objectAtIndex:2U],
635 [session_controller_ currentEntry]);
638 TEST_F(CRWSessionControllerTest, CanGoBackWithoutCommitedEntry) {
639 EXPECT_FALSE([session_controller_ canGoBack]);
642 TEST_F(CRWSessionControllerTest, CanGoBackWithSingleCommitedEntry) {
644 addPendingEntry:GURL("http://www.url.com")
645 referrer:MakeReferrer("http://www.referer.com")
646 transition:ui::PAGE_TRANSITION_TYPED
647 rendererInitiated:NO];
648 [session_controller_ commitPendingEntry];
650 EXPECT_FALSE([session_controller_ canGoBack]);
653 TEST_F(CRWSessionControllerTest, CanGoBackWithMultipleCommitedEntries) {
655 addPendingEntry:GURL("http://www.url.com")
656 referrer:MakeReferrer("http://www.referer.com")
657 transition:ui::PAGE_TRANSITION_TYPED
658 rendererInitiated:NO];
659 [session_controller_ commitPendingEntry];
661 addPendingEntry:GURL("http://www.url1.com")
662 referrer:MakeReferrer("http://www.referer.com")
663 transition:ui::PAGE_TRANSITION_TYPED
664 rendererInitiated:NO];
665 [session_controller_ commitPendingEntry];
667 addPendingEntry:GURL("http://www.url2.com")
668 referrer:MakeReferrer("http://www.referer.com")
669 transition:ui::PAGE_TRANSITION_TYPED
670 rendererInitiated:NO];
671 [session_controller_ commitPendingEntry];
673 EXPECT_TRUE([session_controller_ canGoBack]);
675 [session_controller_ goBack];
676 EXPECT_TRUE([session_controller_ canGoBack]);
678 [session_controller_ goBack];
679 EXPECT_FALSE([session_controller_ canGoBack]);
681 [session_controller_ goBack];
682 EXPECT_FALSE([session_controller_ canGoBack]);
684 [session_controller_ goForward];
685 EXPECT_TRUE([session_controller_ canGoBack]);
688 TEST_F(CRWSessionControllerTest, CanGoForwardWithoutCommitedEntry) {
689 EXPECT_FALSE([session_controller_ canGoBack]);
692 TEST_F(CRWSessionControllerTest, CanGoForwardWithSingleCommitedEntry) {
694 addPendingEntry:GURL("http://www.url.com")
695 referrer:MakeReferrer("http://www.referer.com")
696 transition:ui::PAGE_TRANSITION_TYPED
697 rendererInitiated:NO];
698 [session_controller_ commitPendingEntry];
700 EXPECT_FALSE([session_controller_ canGoBack]);
703 TEST_F(CRWSessionControllerTest, CanGoForwardWithMultipleCommitedEntries) {
705 addPendingEntry:GURL("http://www.url.com")
706 referrer:MakeReferrer("http://www.referer.com")
707 transition:ui::PAGE_TRANSITION_TYPED
708 rendererInitiated:NO];
709 [session_controller_ commitPendingEntry];
711 addPendingEntry:GURL("http://www.url1.com")
712 referrer:MakeReferrer("http://www.referer.com")
713 transition:ui::PAGE_TRANSITION_TYPED
714 rendererInitiated:NO];
715 [session_controller_ commitPendingEntry];
717 addPendingEntry:GURL("http://www.url2.com")
718 referrer:MakeReferrer("http://www.referer.com")
719 transition:ui::PAGE_TRANSITION_TYPED
720 rendererInitiated:NO];
721 [session_controller_ commitPendingEntry];
723 EXPECT_FALSE([session_controller_ canGoForward]);
725 [session_controller_ goBack];
726 EXPECT_TRUE([session_controller_ canGoForward]);
728 [session_controller_ goBack];
729 EXPECT_TRUE([session_controller_ canGoForward]);
731 [session_controller_ goForward];
732 EXPECT_TRUE([session_controller_ canGoForward]);
734 [session_controller_ goForward];
735 EXPECT_FALSE([session_controller_ canGoForward]);
738 // Helper to create a NavigationItem. Caller is responsible for freeing
740 web::NavigationItem* CreateNavigationItem(const std::string& url,
741 const std::string& referrer,
743 web::Referrer referrer_object(GURL(referrer),
744 web::ReferrerPolicyDefault);
745 web::NavigationItemImpl* navigation_item = new web::NavigationItemImpl();
746 navigation_item->SetURL(GURL(url));
747 navigation_item->SetReferrer(referrer_object);
748 navigation_item->SetTitle(base::SysNSStringToUTF16(title));
749 navigation_item->SetTransitionType(ui::PAGE_TRANSITION_TYPED);
751 return navigation_item;
754 TEST_F(CRWSessionControllerTest, CreateWithEmptyNavigations) {
755 ScopedVector<web::NavigationItem> items;
756 base::scoped_nsobject<CRWSessionController> controller(
757 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
759 browserState:&browser_state_]);
760 EXPECT_EQ(controller.get().entries.count, 0U);
761 EXPECT_EQ(controller.get().currentNavigationIndex, -1);
762 EXPECT_EQ(controller.get().previousNavigationIndex, -1);
763 EXPECT_FALSE(controller.get().currentEntry);
766 TEST_F(CRWSessionControllerTest, CreateWithNavList) {
767 ScopedVector<web::NavigationItem> items;
768 items.push_back(CreateNavigationItem("http://www.google.com",
769 "http://www.referrer.com", @"Google"));
770 items.push_back(CreateNavigationItem("http://www.yahoo.com",
771 "http://www.google.com", @"Yahoo"));
772 items.push_back(CreateNavigationItem("http://www.espn.com",
773 "http://www.nothing.com", @"ESPN"));
774 base::scoped_nsobject<CRWSessionController> controller(
775 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
777 browserState:&browser_state_]);
779 EXPECT_EQ(controller.get().entries.count, 3U);
780 EXPECT_EQ(controller.get().currentNavigationIndex, 1);
781 EXPECT_EQ(controller.get().previousNavigationIndex, -1);
782 // Sanity check the current entry, the CRWSessionEntry unit test will ensure
783 // the entire object is created properly.
784 CRWSessionEntry* current_entry = controller.get().currentEntry;
785 EXPECT_EQ(current_entry.navigationItem->GetURL(),
786 GURL("http://www.yahoo.com"));
787 EXPECT_EQ([[controller openerId] length], 0UL);
790 TEST_F(CRWSessionControllerTest, PreviousNavigationEntry) {
792 addPendingEntry:GURL("http://www.url.com")
793 referrer:MakeReferrer("http://www.referer.com")
794 transition:ui::PAGE_TRANSITION_TYPED
795 rendererInitiated:NO];
796 [session_controller_ commitPendingEntry];
798 addPendingEntry:GURL("http://www.url1.com")
799 referrer:MakeReferrer("http://www.referer.com")
800 transition:ui::PAGE_TRANSITION_TYPED
801 rendererInitiated:NO];
802 [session_controller_ commitPendingEntry];
804 addPendingEntry:GURL("http://www.url2.com")
805 referrer:MakeReferrer("http://www.referer.com")
806 transition:ui::PAGE_TRANSITION_TYPED
807 rendererInitiated:NO];
808 [session_controller_ commitPendingEntry];
810 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 1);
812 [session_controller_ goBack];
813 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 2);
815 [session_controller_ goBack];
816 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 1);
818 [session_controller_ goForward];
819 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 0);
821 [session_controller_ goForward];
822 EXPECT_EQ(session_controller_.get().previousNavigationIndex, 1);
825 TEST_F(CRWSessionControllerTest, PushNewEntry) {
826 ScopedVector<web::NavigationItem> items;
827 items.push_back(CreateNavigationItem("http://www.firstpage.com",
828 "http://www.starturl.com", @"First"));
829 items.push_back(CreateNavigationItem("http://www.secondpage.com",
830 "http://www.firstpage.com", @"Second"));
831 items.push_back(CreateNavigationItem("http://www.thirdpage.com",
832 "http://www.secondpage.com", @"Third"));
833 base::scoped_nsobject<CRWSessionController> controller(
834 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
836 browserState:&browser_state_]);
838 GURL pushPageGurl1("http://www.firstpage.com/#push1");
839 NSString* stateObject1 = @"{'foo': 1}";
840 [controller pushNewEntryWithURL:pushPageGurl1
841 stateObject:stateObject1
842 transition:ui::PAGE_TRANSITION_LINK];
843 CRWSessionEntry* pushedEntry = [controller currentEntry];
844 web::NavigationItemImpl* pushedItem = pushedEntry.navigationItemImpl;
845 NSUInteger expectedCount = 2;
846 EXPECT_EQ(expectedCount, controller.get().entries.count);
847 EXPECT_EQ(pushPageGurl1, pushedEntry.navigationItem->GetURL());
848 EXPECT_TRUE(pushedItem->IsCreatedFromPushState());
849 EXPECT_NSEQ(stateObject1, pushedItem->GetSerializedStateObject());
850 EXPECT_EQ(GURL("http://www.firstpage.com/"),
851 pushedEntry.navigationItem->GetReferrer().url);
853 // Add another new entry and check size and fields again.
854 GURL pushPageGurl2("http://www.firstpage.com/push2");
855 [controller pushNewEntryWithURL:pushPageGurl2
857 transition:ui::PAGE_TRANSITION_LINK];
858 pushedEntry = [controller currentEntry];
859 pushedItem = pushedEntry.navigationItemImpl;
861 EXPECT_EQ(expectedCount, controller.get().entries.count);
862 EXPECT_EQ(pushPageGurl2, pushedEntry.navigationItem->GetURL());
863 EXPECT_TRUE(pushedItem->IsCreatedFromPushState());
864 EXPECT_EQ(nil, pushedItem->GetSerializedStateObject());
865 EXPECT_EQ(pushPageGurl1, pushedEntry.navigationItem->GetReferrer().url);
868 TEST_F(CRWSessionControllerTest, IsPushStateNavigation) {
869 ScopedVector<web::NavigationItem> items;
871 CreateNavigationItem("http://foo.com", "http://google.com", @"First"));
872 // Push state navigation.
874 CreateNavigationItem("http://foo.com#bar", "http://foo.com", @"Second"));
875 items.push_back(CreateNavigationItem("http://google.com",
876 "http://foo.com#bar", @"Third"));
878 CreateNavigationItem("http://foo.com", "http://google.com", @"Fourth"));
879 // Push state navigation.
881 CreateNavigationItem("http://foo.com/bar", "http://foo.com", @"Fifth"));
882 // Push state navigation.
883 items.push_back(CreateNavigationItem("http://foo.com/bar#bar",
884 "http://foo.com/bar", @"Sixth"));
885 base::scoped_nsobject<CRWSessionController> controller(
886 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
888 browserState:&browser_state_]);
889 CRWSessionEntry* entry0 = [controller.get().entries objectAtIndex:0];
890 CRWSessionEntry* entry1 = [controller.get().entries objectAtIndex:1];
891 CRWSessionEntry* entry2 = [controller.get().entries objectAtIndex:2];
892 CRWSessionEntry* entry3 = [controller.get().entries objectAtIndex:3];
893 CRWSessionEntry* entry4 = [controller.get().entries objectAtIndex:4];
894 CRWSessionEntry* entry5 = [controller.get().entries objectAtIndex:5];
895 entry1.navigationItemImpl->SetIsCreatedFromPushState(true);
896 entry4.navigationItemImpl->SetIsCreatedFromPushState(true);
897 entry5.navigationItemImpl->SetIsCreatedFromPushState(true);
900 [controller isPushStateNavigationBetweenEntry:entry0 andEntry:entry1]);
902 [controller isPushStateNavigationBetweenEntry:entry5 andEntry:entry3]);
904 [controller isPushStateNavigationBetweenEntry:entry4 andEntry:entry3]);
906 [controller isPushStateNavigationBetweenEntry:entry1 andEntry:entry2]);
908 [controller isPushStateNavigationBetweenEntry:entry0 andEntry:entry5]);
910 [controller isPushStateNavigationBetweenEntry:entry2 andEntry:entry4]);
913 TEST_F(CRWSessionControllerTest, UpdateCurrentEntry) {
914 ScopedVector<web::NavigationItem> items;
915 items.push_back(CreateNavigationItem("http://www.firstpage.com",
916 "http://www.starturl.com", @"First"));
917 items.push_back(CreateNavigationItem("http://www.secondpage.com",
918 "http://www.firstpage.com", @"Second"));
919 items.push_back(CreateNavigationItem("http://www.thirdpage.com",
920 "http://www.secondpage.com", @"Third"));
921 base::scoped_nsobject<CRWSessionController> controller(
922 [[CRWSessionController alloc] initWithNavigationItems:items.Pass()
924 browserState:&browser_state_]);
926 GURL replacePageGurl1("http://www.firstpage.com/#replace1");
927 NSString* stateObject1 = @"{'foo': 1}";
929 // Replace current entry and check the size of history and fields of the
931 [controller updateCurrentEntryWithURL:replacePageGurl1
932 stateObject:stateObject1];
933 CRWSessionEntry* replacedEntry = [controller currentEntry];
934 web::NavigationItemImpl* replacedItem = replacedEntry.navigationItemImpl;
935 NSUInteger expectedCount = 3;
936 EXPECT_EQ(expectedCount, controller.get().entries.count);
937 EXPECT_EQ(replacePageGurl1, replacedEntry.navigationItem->GetURL());
938 EXPECT_FALSE(replacedItem->IsCreatedFromPushState());
939 EXPECT_NSEQ(stateObject1, replacedItem->GetSerializedStateObject());
940 EXPECT_EQ(GURL("http://www.starturl.com/"),
941 replacedEntry.navigationItem->GetReferrer().url);
943 // Replace current entry and check size and fields again.
944 GURL replacePageGurl2("http://www.firstpage.com/#replace2");
945 [controller.get() updateCurrentEntryWithURL:replacePageGurl2 stateObject:nil];
946 replacedEntry = [controller currentEntry];
947 replacedItem = replacedEntry.navigationItemImpl;
948 EXPECT_EQ(expectedCount, controller.get().entries.count);
949 EXPECT_EQ(replacePageGurl2, replacedEntry.navigationItem->GetURL());
950 EXPECT_FALSE(replacedItem->IsCreatedFromPushState());
951 EXPECT_NSEQ(nil, replacedItem->GetSerializedStateObject());
952 EXPECT_EQ(GURL("http://www.starturl.com/"),
953 replacedEntry.navigationItem->GetReferrer().url);
956 TEST_F(CRWSessionControllerTest, TestBackwardForwardEntries) {
957 [session_controller_ addPendingEntry:GURL("http://www.example.com/0")
958 referrer:MakeReferrer("http://www.example.com/a")
959 transition:ui::PAGE_TRANSITION_LINK
960 rendererInitiated:NO];
961 [session_controller_ commitPendingEntry];
962 [session_controller_ addPendingEntry:GURL("http://www.example.com/1")
963 referrer:MakeReferrer("http://www.example.com/b")
964 transition:ui::PAGE_TRANSITION_LINK
965 rendererInitiated:NO];
966 [session_controller_ commitPendingEntry];
967 [session_controller_ addPendingEntry:GURL("http://www.example.com/redirect")
968 referrer:MakeReferrer("http://www.example.com/r")
969 transition:ui::PAGE_TRANSITION_IS_REDIRECT_MASK
970 rendererInitiated:NO];
971 [session_controller_ commitPendingEntry];
972 [session_controller_ addPendingEntry:GURL("http://www.example.com/2")
973 referrer:MakeReferrer("http://www.example.com/c")
974 transition:ui::PAGE_TRANSITION_LINK
975 rendererInitiated:NO];
976 [session_controller_ commitPendingEntry];
978 EXPECT_EQ(3, session_controller_.get().currentNavigationIndex);
979 NSArray* backEntries = [session_controller_ backwardEntries];
980 EXPECT_EQ(2U, [backEntries count]);
981 EXPECT_EQ(0U, [[session_controller_ forwardEntries] count]);
982 EXPECT_EQ("http://www.example.com/1",
983 [[backEntries objectAtIndex:0] navigationItem]->GetURL().spec());
985 [session_controller_ goBack];
986 EXPECT_EQ(1U, [[session_controller_ backwardEntries] count]);
987 EXPECT_EQ(1U, [[session_controller_ forwardEntries] count]);
989 [session_controller_ goBack];
990 NSArray* forwardEntries = [session_controller_ forwardEntries];
991 EXPECT_EQ(0U, [[session_controller_ backwardEntries] count]);
992 EXPECT_EQ(2U, [forwardEntries count]);
993 EXPECT_EQ("http://www.example.com/2",
994 [[forwardEntries objectAtIndex:1] navigationItem]->GetURL().spec());
997 TEST_F(CRWSessionControllerTest, GoToEntry) {
998 [session_controller_ addPendingEntry:GURL("http://www.example.com/0")
999 referrer:MakeReferrer("http://www.example.com/a")
1000 transition:ui::PAGE_TRANSITION_LINK
1001 rendererInitiated:NO];
1002 [session_controller_ commitPendingEntry];
1003 [session_controller_ addPendingEntry:GURL("http://www.example.com/1")
1004 referrer:MakeReferrer("http://www.example.com/b")
1005 transition:ui::PAGE_TRANSITION_LINK
1006 rendererInitiated:NO];
1007 [session_controller_ commitPendingEntry];
1008 [session_controller_ addPendingEntry:GURL("http://www.example.com/redirect")
1009 referrer:MakeReferrer("http://www.example.com/r")
1010 transition:ui::PAGE_TRANSITION_IS_REDIRECT_MASK
1011 rendererInitiated:NO];
1012 [session_controller_ commitPendingEntry];
1013 [session_controller_ addPendingEntry:GURL("http://www.example.com/2")
1014 referrer:MakeReferrer("http://www.example.com/c")
1015 transition:ui::PAGE_TRANSITION_LINK
1016 rendererInitiated:NO];
1017 [session_controller_ commitPendingEntry];
1018 EXPECT_EQ(3, session_controller_.get().currentNavigationIndex);
1020 CRWSessionEntry* entry1 = [session_controller_.get().entries objectAtIndex:1];
1021 [session_controller_ goToEntry:entry1];
1022 EXPECT_EQ(1, session_controller_.get().currentNavigationIndex);
1024 // Remove an entry and attempt to go it. Ensure it outlives the removal.
1025 base::scoped_nsobject<CRWSessionEntry> entry3(
1026 [[session_controller_.get().entries objectAtIndex:3] retain]);
1027 [session_controller_ removeEntryAtIndex:3];
1028 [session_controller_ goToEntry:entry3];
1029 EXPECT_EQ(1, session_controller_.get().currentNavigationIndex);
1032 } // anonymous namespace