Add ICU message format support
[chromium-blink-merge.git] / ios / web / navigation / crw_session_controller_unittest.mm
blobbd4e69310b7b65644e328353442cf83d70d49781
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;
26 @end
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();
39 @end
41 namespace {
43 class CRWSessionControllerTest : public PlatformTest {
44  protected:
45   void SetUp() override {
46     session_controller_.reset(
47         [[CRWSessionController alloc] initWithWindowName:@"test window"
48                                                 openerId:@"opener"
49                                              openedByDOM:NO
50                                    openerNavigationIndex:0
51                                             browserState:&browser_state_]);
52   }
54   web::Referrer MakeReferrer(std::string url) {
55     return web::Referrer(GURL(url), web::ReferrerPolicyDefault);
56   }
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) {
72   [session_controller_
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]);
79   EXPECT_EQ(
80       GURL("http://www.url.com/"),
81       [session_controller_ currentURL]);
84 TEST_F(CRWSessionControllerTest, AddPendingEntryWithCommittedEntries) {
85   [session_controller_
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];
92   [session_controller_
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]);
99   EXPECT_EQ(
100       GURL("http://www.committed.url.com/"),
101       [session_controller_ URLForSessionAtIndex:0U]);
102   EXPECT_EQ(
103       GURL("http://www.url.com/"),
104       [session_controller_ currentURL]);
107 TEST_F(CRWSessionControllerTest, AddPendingEntryOverriding) {
108   [session_controller_
109         addPendingEntry:GURL("http://www.url.com")
110                referrer:MakeReferrer("http://www.referer.com")
111              transition:ui::PAGE_TRANSITION_TYPED
112       rendererInitiated:NO];
113   [session_controller_
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]);
120   EXPECT_EQ(
121       GURL("http://www.another.url.com/"),
122       [session_controller_ currentURL]);
125 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommit) {
126   [session_controller_
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]);
134   EXPECT_EQ(
135       GURL("http://www.url.com/"),
136       [session_controller_ URLForSessionAtIndex:0U]);
137   EXPECT_EQ(
138       [[session_controller_ entries] objectAtIndex:0U],
139       [session_controller_ currentEntry]);
142 TEST_F(CRWSessionControllerTest, AddPendingEntryOverridingAndCommit) {
143   [session_controller_
144         addPendingEntry:GURL("http://www.url.com")
145                referrer:MakeReferrer("http://www.referer.com")
146              transition:ui::PAGE_TRANSITION_TYPED
147       rendererInitiated:NO];
148   [session_controller_
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]);
156   EXPECT_EQ(
157       GURL("http://www.another.url.com/"),
158       [session_controller_ URLForSessionAtIndex:0U]);
159   EXPECT_EQ(
160       [[session_controller_ entries] objectAtIndex:0U],
161       [session_controller_ currentEntry]);
164 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommitMultiple) {
165   [session_controller_
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];
172   [session_controller_
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]);
180   EXPECT_EQ(
181       GURL("http://www.url.com/"),
182       [session_controller_ URLForSessionAtIndex:0U]);
183   EXPECT_EQ(
184       GURL("http://www.another.url.com/"),
185       [session_controller_ URLForSessionAtIndex:1U]);
186   EXPECT_EQ(
187       [[session_controller_ entries] objectAtIndex:1U],
188       [session_controller_ currentEntry]);
191 TEST_F(CRWSessionControllerTest, AddPendingEntryAndDiscard) {
192   [session_controller_
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) {
204   [session_controller_
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];
211   [session_controller_
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]);
219   EXPECT_EQ(
220       GURL("http://www.another.url.com/"),
221       [session_controller_ URLForSessionAtIndex:0U]);
222   EXPECT_EQ(
223       [[session_controller_ entries] objectAtIndex:0U],
224       [session_controller_ currentEntry]);
227 TEST_F(CRWSessionControllerTest, AddPendingEntryAndCommitAndAddAndDiscard) {
228   [session_controller_
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];
235   [session_controller_
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]);
243   EXPECT_EQ(
244       GURL("http://www.url.com/"),
245       [session_controller_ URLForSessionAtIndex:0U]);
246   EXPECT_EQ(
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
262   [session_controller_
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]);
273   EXPECT_EQ(
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
289   [session_controller_
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]);
300   EXPECT_EQ(
301       [[session_controller_ entries] objectAtIndex:0U],
302       [session_controller_ currentEntry]);
305 TEST_F(CRWSessionControllerTest, UpdatePendingEntryWithoutPendingEntry) {
306   [session_controller_
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) {
315   [session_controller_
316         addPendingEntry:GURL("http://www.url.com")
317                referrer:MakeReferrer("http://www.referer.com")
318              transition:ui::PAGE_TRANSITION_TYPED
319       rendererInitiated:NO];
320   [session_controller_
321        updatePendingEntry:GURL("http://www.another.url.com")];
323   EXPECT_EQ(
324       GURL("http://www.another.url.com/"),
325       [session_controller_ currentURL]);
328 TEST_F(CRWSessionControllerTest,
329        UpdatePendingEntryWithPendingEntryAlreadyCommited) {
330   [session_controller_
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];
336   [session_controller_
337        updatePendingEntry:GURL("http://www.another.url.com")];
338   [session_controller_ commitPendingEntry];
340   EXPECT_EQ(1U, [[session_controller_ entries] count]);
341   EXPECT_EQ(
342       GURL("http://www.url.com/"),
343       [session_controller_ URLForSessionAtIndex:0U]);
344   EXPECT_EQ(
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) {
357   [session_controller_
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]);
367   EXPECT_EQ(
368       GURL("http://www.url.com/"),
369       [session_controller_ URLForSessionAtIndex:0U]);
370   EXPECT_EQ(
371       [[session_controller_ entries] objectAtIndex:0U],
372       [session_controller_ currentEntry]);
375 TEST_F(CRWSessionControllerTest, GoBackFromTheEnd) {
376   [session_controller_
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];
382   [session_controller_
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]);
392   EXPECT_EQ(
393       GURL("http://www.url.com/"),
394       [session_controller_ URLForSessionAtIndex:0U]);
395   EXPECT_EQ(
396       GURL("http://www.url2.com/"),
397       [session_controller_ URLForSessionAtIndex:1U]);
398   EXPECT_EQ(
399       [[session_controller_ entries] objectAtIndex:0U],
400       [session_controller_ currentEntry]);
403 TEST_F(CRWSessionControllerTest, GoBackFromTheBeginning) {
404   [session_controller_
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];
410   [session_controller_
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]);
421   EXPECT_EQ(
422       GURL("http://www.url.com/"),
423       [session_controller_ URLForSessionAtIndex:0U]);
424   EXPECT_EQ(
425       GURL("http://www.url2.com/"),
426       [session_controller_ URLForSessionAtIndex:1U]);
427   EXPECT_EQ(
428       [[session_controller_ entries] objectAtIndex:0U],
429       [session_controller_ currentEntry]);
432 TEST_F(CRWSessionControllerTest, GoBackFromTheMiddle) {
433   [session_controller_
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];
439   [session_controller_
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];
445   [session_controller_
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];
451   [session_controller_
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]);
462   EXPECT_EQ(
463       GURL("http://www.url.com/"),
464       [session_controller_ URLForSessionAtIndex:0U]);
465   EXPECT_EQ(
466       GURL("http://www.url2.com/"),
467       [session_controller_ URLForSessionAtIndex:1U]);
468   EXPECT_EQ(
469       GURL("http://www.url3.com/"),
470       [session_controller_ URLForSessionAtIndex:2U]);
471   EXPECT_EQ(
472       GURL("http://www.url4.com/"),
473       [session_controller_ URLForSessionAtIndex:3U]);
474   EXPECT_EQ(
475       [[session_controller_ entries] objectAtIndex:1U],
476       [session_controller_ currentEntry]);
479 TEST_F(CRWSessionControllerTest, GoBackAndRemove) {
480   [session_controller_
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];
486   [session_controller_
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]);
497   EXPECT_EQ(
498             GURL("http://www.url.com/"),
499             [session_controller_ URLForSessionAtIndex:0U]);
500   EXPECT_EQ(
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) {
515   [session_controller_
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]);
525   EXPECT_EQ(
526       GURL("http://www.url.com/"),
527       [session_controller_ URLForSessionAtIndex:0U]);
528   EXPECT_EQ(
529       [[session_controller_ entries] objectAtIndex:0U],
530       [session_controller_ currentEntry]);
533 TEST_F(CRWSessionControllerTest, GoForewardFromTheEnd) {
534   [session_controller_
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];
540   [session_controller_
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]);
550   EXPECT_EQ(
551       GURL("http://www.url.com/"),
552       [session_controller_ URLForSessionAtIndex:0U]);
553   EXPECT_EQ(
554       GURL("http://www.url2.com/"),
555       [session_controller_ URLForSessionAtIndex:1U]);
556   EXPECT_EQ(
557       [[session_controller_ entries] objectAtIndex:1U],
558       [session_controller_ currentEntry]);
561 TEST_F(CRWSessionControllerTest, GoForewardFromTheBeginning) {
562   [session_controller_
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];
568   [session_controller_
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]);
579   EXPECT_EQ(
580       GURL("http://www.url.com/"),
581       [session_controller_ URLForSessionAtIndex:0U]);
582   EXPECT_EQ(
583       GURL("http://www.url2.com/"),
584       [session_controller_ URLForSessionAtIndex:1U]);
585   EXPECT_EQ(
586       [[session_controller_ entries] objectAtIndex:1U],
587       [session_controller_ currentEntry]);
590 TEST_F(CRWSessionControllerTest, GoForwardFromTheMiddle) {
591   [session_controller_
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];
597   [session_controller_
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];
603   [session_controller_
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];
609   [session_controller_
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]);
621   EXPECT_EQ(
622       GURL("http://www.url.com/"),
623       [session_controller_ URLForSessionAtIndex:0U]);
624   EXPECT_EQ(
625       GURL("http://www.url2.com/"),
626       [session_controller_ URLForSessionAtIndex:1U]);
627   EXPECT_EQ(
628       GURL("http://www.url3.com/"),
629       [session_controller_ URLForSessionAtIndex:2U]);
630   EXPECT_EQ(
631       GURL("http://www.url4.com/"),
632       [session_controller_ URLForSessionAtIndex:3U]);
633   EXPECT_EQ(
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) {
643   [session_controller_
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) {
654   [session_controller_
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];
660   [session_controller_
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];
666   [session_controller_
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) {
693   [session_controller_
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) {
704   [session_controller_
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];
710   [session_controller_
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];
716   [session_controller_
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
739 // the memory.
740 web::NavigationItem* CreateNavigationItem(const std::string& url,
741                                           const std::string& referrer,
742                                           NSString* title) {
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()
758                                                currentIndex:0
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()
776                                                currentIndex:1
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) {
791   [session_controller_
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];
797   [session_controller_
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];
803   [session_controller_
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()
835                                                currentIndex:0
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
856                       stateObject:nil
857                        transition:ui::PAGE_TRANSITION_LINK];
858   pushedEntry = [controller currentEntry];
859   pushedItem = pushedEntry.navigationItemImpl;
860   expectedCount = 3;
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;
870   items.push_back(
871       CreateNavigationItem("http://foo.com", "http://google.com", @"First"));
872   // Push state navigation.
873   items.push_back(
874       CreateNavigationItem("http://foo.com#bar", "http://foo.com", @"Second"));
875   items.push_back(CreateNavigationItem("http://google.com",
876                                        "http://foo.com#bar", @"Third"));
877   items.push_back(
878       CreateNavigationItem("http://foo.com", "http://google.com", @"Fourth"));
879   // Push state navigation.
880   items.push_back(
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()
887                                                currentIndex:0
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);
899   EXPECT_TRUE(
900       [controller isPushStateNavigationBetweenEntry:entry0 andEntry:entry1]);
901   EXPECT_TRUE(
902       [controller isPushStateNavigationBetweenEntry:entry5 andEntry:entry3]);
903   EXPECT_TRUE(
904       [controller isPushStateNavigationBetweenEntry:entry4 andEntry:entry3]);
905   EXPECT_FALSE(
906       [controller isPushStateNavigationBetweenEntry:entry1 andEntry:entry2]);
907   EXPECT_FALSE(
908       [controller isPushStateNavigationBetweenEntry:entry0 andEntry:entry5]);
909   EXPECT_FALSE(
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()
923                                                currentIndex:0
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
930   // modified entry.
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