OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/sync/glue/synced_tab_delegate.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "components/sync_driver/glue/synced_window_delegate.h" | |
9 #include "content/public/browser/navigation_entry.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace browser_sync { | |
13 | |
14 namespace { | |
15 | |
16 class FakeSyncedWindowDelegate : public SyncedWindowDelegate { | |
17 public: | |
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 { | |
26 return false; | |
27 } | |
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; } | |
32 }; | |
33 | |
34 class FakeSyncedTabDelegate : public SyncedTabDelegate { | |
35 public: | |
36 FakeSyncedTabDelegate() : | |
37 pending_index_(-1), | |
38 current_index_(0), | |
39 pending_entry_(NULL), | |
40 window_(new FakeSyncedWindowDelegate()) {} | |
41 | |
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 bool IsInitialBlankNavigation() const override { | |
48 // This differs from NavigationControllerImpl, which has an initial blank | |
49 // NavigationEntry. | |
50 return GetEntryCount() == 0; | |
51 } | |
52 int GetCurrentEntryIndex() const override { return current_index_; } | |
53 int GetEntryCount() const override { return indexed_entries_.size(); } | |
54 int GetPendingEntryIndex() const override { return pending_index_; } | |
55 content::NavigationEntry* GetPendingEntry() const override { | |
56 return pending_entry_; | |
57 } | |
58 content::NavigationEntry* GetEntryAtIndex(int i) const override { | |
59 return indexed_entries_[i]; | |
60 } | |
61 content::NavigationEntry* GetActiveEntry() const override { return NULL; } | |
62 bool ProfileIsSupervised() const override { return false; } | |
63 const std::vector<const content::NavigationEntry*>* | |
64 GetBlockedNavigations() const override { return NULL; } | |
65 bool IsPinned() const override { return false;} | |
66 bool HasWebContents() const override { return false;} | |
67 content::WebContents* GetWebContents() const override { return NULL;} | |
68 int GetSyncId() const override { return 0; } | |
69 void SetSyncId(int sync_id) override {} | |
70 const SyncedWindowDelegate* GetSyncedWindowDelegate() const override { | |
71 return window_.get(); | |
72 } | |
73 | |
74 void SetCurrentEntryIndex(int i) { | |
75 current_index_ = i; | |
76 } | |
77 | |
78 void SetPendingEntryIndex(int i) { | |
79 pending_index_ = i; | |
80 } | |
81 | |
82 void SetPendingEntry(content::NavigationEntry* entry) { | |
83 pending_entry_ = entry; | |
84 } | |
85 | |
86 void AppendIndexedEntry(content::NavigationEntry* entry) { | |
87 indexed_entries_.push_back(entry); | |
88 } | |
89 | |
90 private: | |
91 int pending_index_; | |
92 int current_index_; | |
93 content::NavigationEntry* pending_entry_; | |
94 std::vector<content::NavigationEntry*> indexed_entries_; | |
95 const scoped_ptr<FakeSyncedWindowDelegate> window_; | |
96 }; | |
97 | |
98 class SyncedTabDelegateTest : public testing::Test { | |
99 public: | |
100 SyncedTabDelegateTest() : tab_(), | |
101 entry_(content::NavigationEntry::Create()) {} | |
102 | |
103 protected: | |
104 FakeSyncedTabDelegate tab_; | |
105 const scoped_ptr<content::NavigationEntry> entry_; | |
106 }; | |
107 | |
108 } // namespace | |
109 | |
110 TEST_F(SyncedTabDelegateTest, GetEntryCurrentIsPending) { | |
111 tab_.SetPendingEntryIndex(1); | |
112 tab_.SetCurrentEntryIndex(1); | |
113 | |
114 tab_.SetPendingEntry(entry_.get()); | |
115 | |
116 EXPECT_EQ(entry_.get(), tab_.GetCurrentEntryMaybePending()); | |
117 EXPECT_EQ(entry_.get(), tab_.GetEntryAtIndexMaybePending(1)); | |
118 } | |
119 | |
120 TEST_F(SyncedTabDelegateTest, GetEntryCurrentNotPending) { | |
121 tab_.SetPendingEntryIndex(1); | |
122 tab_.SetCurrentEntryIndex(0); | |
123 | |
124 tab_.AppendIndexedEntry(entry_.get()); | |
125 | |
126 EXPECT_EQ(entry_.get(), tab_.GetCurrentEntryMaybePending()); | |
127 EXPECT_EQ(entry_.get(), tab_.GetEntryAtIndexMaybePending(0)); | |
128 } | |
129 | |
130 TEST_F(SyncedTabDelegateTest, ShouldSyncNoEntries) { | |
131 EXPECT_FALSE(tab_.ShouldSync()); | |
132 } | |
133 | |
134 TEST_F(SyncedTabDelegateTest, ShouldSyncNullEntry) { | |
135 entry_->SetURL(GURL("http://www.google.com")); | |
136 tab_.AppendIndexedEntry(entry_.get()); | |
137 tab_.AppendIndexedEntry(NULL); | |
138 | |
139 EXPECT_FALSE(tab_.ShouldSync()); | |
140 } | |
141 | |
142 TEST_F(SyncedTabDelegateTest, ShouldSyncFile) { | |
143 entry_->SetURL(GURL("file://path")); | |
144 tab_.AppendIndexedEntry(entry_.get()); | |
145 | |
146 EXPECT_FALSE(tab_.ShouldSync()); | |
147 } | |
148 | |
149 TEST_F(SyncedTabDelegateTest, ShouldSyncChrome) { | |
150 entry_->SetURL(GURL("chrome://preferences/")); | |
151 tab_.AppendIndexedEntry(entry_.get()); | |
152 | |
153 EXPECT_FALSE(tab_.ShouldSync()); | |
154 } | |
155 | |
156 TEST_F(SyncedTabDelegateTest, ShouldSyncHistory) { | |
157 entry_->SetURL(GURL("chrome://history/")); | |
158 tab_.AppendIndexedEntry(entry_.get()); | |
159 | |
160 EXPECT_TRUE(tab_.ShouldSync()); | |
161 } | |
162 | |
163 TEST_F(SyncedTabDelegateTest, ShouldSyncValid) { | |
164 entry_->SetURL(GURL("http://www.google.com")); | |
165 tab_.AppendIndexedEntry(entry_.get()); | |
166 | |
167 EXPECT_TRUE(tab_.ShouldSync()); | |
168 } | |
169 | |
170 TEST_F(SyncedTabDelegateTest, ShouldSyncMultiple) { | |
171 entry_->SetURL(GURL("file://path")); | |
172 tab_.AppendIndexedEntry(entry_.get()); | |
173 | |
174 scoped_ptr<content::NavigationEntry> | |
175 entry2(content::NavigationEntry::Create()); | |
176 entry2->SetURL(GURL("chrome://preferences/")); | |
177 tab_.AppendIndexedEntry(entry2.get()); | |
178 | |
179 // As long as they're all invalid, expect false. | |
180 EXPECT_FALSE(tab_.ShouldSync()); | |
181 | |
182 scoped_ptr<content::NavigationEntry> | |
183 entry3(content::NavigationEntry::Create()); | |
184 entry3->SetURL(GURL("http://www.google.com")); | |
185 tab_.AppendIndexedEntry(entry3.get()); | |
186 | |
187 // With one valid, expect true. | |
188 EXPECT_TRUE(tab_.ShouldSync()); | |
189 } | |
190 | |
191 } // namespace browser_sync | |
OLD | NEW |