1 // Copyright 2015 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 #include "chrome/browser/sync/glue/synced_tab_delegate.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/sync/glue/synced_window_delegate.h"
9 #include "content/public/browser/navigation_entry.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace browser_sync
{
16 class FakeSyncedWindowDelegate
: public SyncedWindowDelegate
{
18 bool HasWindow() const override
{ return false; }
19 SessionID::id_type
GetSessionId() const override
{ return 0; }
20 int GetTabCount() const override
{ return 0; }
21 int GetActiveIndex() const override
{ return 0; }
22 bool IsApp() const override
{ return false; }
23 bool IsTypeTabbed() const override
{ return false; }
24 bool IsTypePopup() const override
{ return false; }
25 bool IsTabPinned(const SyncedTabDelegate
* tab
) const override
{
28 SyncedTabDelegate
* GetTabAt(int index
) const override
{ return NULL
; }
29 SessionID::id_type
GetTabIdAt(int index
) const override
{ return 0; }
30 bool IsSessionRestoreInProgress() const override
{ return false;}
31 bool ShouldSync() const override
{ return false; }
34 class FakeSyncedTabDelegate
: public SyncedTabDelegate
{
36 FakeSyncedTabDelegate() :
40 window_(new FakeSyncedWindowDelegate()) {}
42 SessionID::id_type
GetWindowId() const override
{ return 0; }
43 SessionID::id_type
GetSessionId() const override
{ return 0; }
44 bool IsBeingDestroyed() const override
{ return false; }
45 Profile
* profile() const override
{ return NULL
; }
46 std::string
GetExtensionAppId() const override
{ return ""; }
47 int GetCurrentEntryIndex() const override
{ return current_index_
; }
48 int GetEntryCount() const override
{ return indexed_entries_
.size(); }
49 int GetPendingEntryIndex() const override
{ return pending_index_
; }
50 content::NavigationEntry
* GetPendingEntry() const override
{
51 return pending_entry_
;
53 content::NavigationEntry
* GetEntryAtIndex(int i
) const override
{
54 return indexed_entries_
[i
];
56 content::NavigationEntry
* GetActiveEntry() const override
{ return NULL
; }
57 bool ProfileIsSupervised() const override
{ return false; }
58 const std::vector
<const content::NavigationEntry
*>*
59 GetBlockedNavigations() const override
{ return NULL
; }
60 bool IsPinned() const override
{ return false;}
61 bool HasWebContents() const override
{ return false;}
62 content::WebContents
* GetWebContents() const override
{ return NULL
;}
63 int GetSyncId() const override
{ return 0; }
64 void SetSyncId(int sync_id
) override
{}
65 const SyncedWindowDelegate
* GetSyncedWindowDelegate() const override
{
69 void SetCurrentEntryIndex(int i
) {
73 void SetPendingEntryIndex(int i
) {
77 void SetPendingEntry(content::NavigationEntry
* entry
) {
78 pending_entry_
= entry
;
81 void AppendIndexedEntry(content::NavigationEntry
* entry
) {
82 indexed_entries_
.push_back(entry
);
88 content::NavigationEntry
* pending_entry_
;
89 std::vector
<content::NavigationEntry
*> indexed_entries_
;
90 const scoped_ptr
<FakeSyncedWindowDelegate
> window_
;
93 class SyncedTabDelegateTest
: public testing::Test
{
95 SyncedTabDelegateTest() : tab_(),
96 entry_(content::NavigationEntry::Create()) {}
99 FakeSyncedTabDelegate tab_
;
100 const scoped_ptr
<content::NavigationEntry
> entry_
;
105 TEST_F(SyncedTabDelegateTest
, GetEntryCurrentIsPending
) {
106 tab_
.SetPendingEntryIndex(1);
107 tab_
.SetCurrentEntryIndex(1);
109 tab_
.SetPendingEntry(entry_
.get());
111 EXPECT_EQ(entry_
.get(), tab_
.GetCurrentEntryMaybePending());
112 EXPECT_EQ(entry_
.get(), tab_
.GetEntryAtIndexMaybePending(1));
115 TEST_F(SyncedTabDelegateTest
, GetEntryCurrentNotPending
) {
116 tab_
.SetPendingEntryIndex(1);
117 tab_
.SetCurrentEntryIndex(0);
119 tab_
.AppendIndexedEntry(entry_
.get());
121 EXPECT_EQ(entry_
.get(), tab_
.GetCurrentEntryMaybePending());
122 EXPECT_EQ(entry_
.get(), tab_
.GetEntryAtIndexMaybePending(0));
125 TEST_F(SyncedTabDelegateTest
, ShouldSyncNoEntries
) {
126 EXPECT_FALSE(tab_
.ShouldSync());
129 TEST_F(SyncedTabDelegateTest
, ShouldSyncNullEntry
) {
130 entry_
->SetURL(GURL("http://www.google.com"));
131 tab_
.AppendIndexedEntry(entry_
.get());
132 tab_
.AppendIndexedEntry(NULL
);
134 EXPECT_FALSE(tab_
.ShouldSync());
137 TEST_F(SyncedTabDelegateTest
, ShouldSyncFile
) {
138 entry_
->SetURL(GURL("file://path"));
139 tab_
.AppendIndexedEntry(entry_
.get());
141 EXPECT_FALSE(tab_
.ShouldSync());
144 TEST_F(SyncedTabDelegateTest
, ShouldSyncChrome
) {
145 entry_
->SetURL(GURL("chrome://preferences/"));
146 tab_
.AppendIndexedEntry(entry_
.get());
148 EXPECT_FALSE(tab_
.ShouldSync());
151 TEST_F(SyncedTabDelegateTest
, ShouldSyncHistory
) {
152 entry_
->SetURL(GURL("chrome://history/"));
153 tab_
.AppendIndexedEntry(entry_
.get());
155 EXPECT_TRUE(tab_
.ShouldSync());
158 TEST_F(SyncedTabDelegateTest
, ShouldSyncValid
) {
159 entry_
->SetURL(GURL("http://www.google.com"));
160 tab_
.AppendIndexedEntry(entry_
.get());
162 EXPECT_TRUE(tab_
.ShouldSync());
165 TEST_F(SyncedTabDelegateTest
, ShouldSyncMultiple
) {
166 entry_
->SetURL(GURL("file://path"));
167 tab_
.AppendIndexedEntry(entry_
.get());
169 scoped_ptr
<content::NavigationEntry
>
170 entry2(content::NavigationEntry::Create());
171 entry2
->SetURL(GURL("chrome://preferences/"));
172 tab_
.AppendIndexedEntry(entry2
.get());
174 // As long as they're all invalid, expect false.
175 EXPECT_FALSE(tab_
.ShouldSync());
177 scoped_ptr
<content::NavigationEntry
>
178 entry3(content::NavigationEntry::Create());
179 entry3
->SetURL(GURL("http://www.google.com"));
180 tab_
.AppendIndexedEntry(entry3
.get());
182 // With one valid, expect true.
183 EXPECT_TRUE(tab_
.ShouldSync());
186 } // namespace browser_sync