test_synced_window_delegates_getter.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // Copyright 2018 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. #include "components/sync_sessions/test_synced_window_delegates_getter.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/containers/cxx20_erase.h"
  8. #include "components/sessions/core/serialized_navigation_entry_test_helper.h"
  9. #include "components/sync_sessions/synced_session.h"
  10. #include "components/sync_sessions/tab_node_pool.h"
  11. namespace sync_sessions {
  12. namespace {
  13. const char kTitle[] = "title";
  14. } // namespace
  15. TestSyncedTabDelegate::TestSyncedTabDelegate(
  16. SessionID window_id,
  17. SessionID tab_id,
  18. const base::RepeatingCallback<void(SyncedTabDelegate*)>& notify_cb)
  19. : window_id_(window_id), tab_id_(tab_id), notify_cb_(notify_cb) {}
  20. TestSyncedTabDelegate::~TestSyncedTabDelegate() = default;
  21. void TestSyncedTabDelegate::Navigate(const std::string& url,
  22. base::Time time,
  23. ui::PageTransition transition) {
  24. sync_pb::TabNavigation tab_navigation;
  25. tab_navigation.set_virtual_url(url);
  26. tab_navigation.set_title(kTitle);
  27. tab_navigation.set_global_id(time.ToInternalValue());
  28. tab_navigation.set_unique_id(time.ToInternalValue());
  29. tab_navigation.set_http_status_code(200);
  30. auto entry = std::make_unique<sessions::SerializedNavigationEntry>(
  31. SessionNavigationFromSyncData(0, tab_navigation));
  32. sessions::SerializedNavigationEntryTestHelper::SetTimestamp(time,
  33. entry.get());
  34. sessions::SerializedNavigationEntryTestHelper::SetTransitionType(transition,
  35. entry.get());
  36. entries_.push_back(std::move(entry));
  37. page_language_per_index_.emplace_back();
  38. set_current_entry_index(GetCurrentEntryIndex() + 1);
  39. notify_cb_.Run(this);
  40. }
  41. void TestSyncedTabDelegate::set_current_entry_index(int i) {
  42. current_entry_index_ = i;
  43. }
  44. void TestSyncedTabDelegate::set_blocked_navigations(
  45. const std::vector<std::unique_ptr<sessions::SerializedNavigationEntry>>&
  46. navs) {
  47. for (auto& entry : navs) {
  48. blocked_navigations_.push_back(
  49. std::make_unique<sessions::SerializedNavigationEntry>(*entry));
  50. }
  51. }
  52. void TestSyncedTabDelegate::SetPageLanguageAtIndex(
  53. int i,
  54. const std::string& language) {
  55. page_language_per_index_[i] = language;
  56. }
  57. bool TestSyncedTabDelegate::IsInitialBlankNavigation() const {
  58. // This differs from NavigationControllerImpl, which has an initial blank
  59. // NavigationEntry.
  60. return GetEntryCount() == 0;
  61. }
  62. int TestSyncedTabDelegate::GetCurrentEntryIndex() const {
  63. return current_entry_index_;
  64. }
  65. GURL TestSyncedTabDelegate::GetVirtualURLAtIndex(int i) const {
  66. if (static_cast<size_t>(i) >= entries_.size()) {
  67. return GURL();
  68. }
  69. return entries_[i]->virtual_url();
  70. }
  71. std::string TestSyncedTabDelegate::GetPageLanguageAtIndex(int i) const {
  72. DCHECK(static_cast<size_t>(i) < page_language_per_index_.size());
  73. return page_language_per_index_[i];
  74. }
  75. void TestSyncedTabDelegate::GetSerializedNavigationAtIndex(
  76. int i,
  77. sessions::SerializedNavigationEntry* serialized_entry) const {
  78. if (static_cast<size_t>(i) >= entries_.size()) {
  79. return;
  80. }
  81. *serialized_entry = *entries_[i];
  82. }
  83. int TestSyncedTabDelegate::GetEntryCount() const {
  84. return entries_.size();
  85. }
  86. SessionID TestSyncedTabDelegate::GetWindowId() const {
  87. return window_id_;
  88. }
  89. SessionID TestSyncedTabDelegate::GetSessionId() const {
  90. return tab_id_;
  91. }
  92. bool TestSyncedTabDelegate::IsBeingDestroyed() const {
  93. return false;
  94. }
  95. std::string TestSyncedTabDelegate::GetExtensionAppId() const {
  96. return std::string();
  97. }
  98. bool TestSyncedTabDelegate::ProfileHasChildAccount() const {
  99. return has_child_account_;
  100. }
  101. void TestSyncedTabDelegate::set_has_child_account(bool has_child_account) {
  102. has_child_account_ = has_child_account;
  103. }
  104. const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>*
  105. TestSyncedTabDelegate::GetBlockedNavigations() const {
  106. return &blocked_navigations_;
  107. }
  108. bool TestSyncedTabDelegate::IsPlaceholderTab() const {
  109. return false;
  110. }
  111. bool TestSyncedTabDelegate::ShouldSync(SyncSessionsClient* sessions_client) {
  112. // This is just a simple filter that isn't meant to fully reproduce
  113. // the TabContentsTabDelegate's ShouldSync logic.
  114. // Verify all URL's are valid (which will ignore an initial blank page) and
  115. // that there is at least one http:// url.
  116. int http_count = 0;
  117. for (auto& entry : entries_) {
  118. if (!entry->virtual_url().is_valid()) {
  119. return false;
  120. }
  121. if (entry->virtual_url().SchemeIsHTTPOrHTTPS()) {
  122. http_count++;
  123. }
  124. }
  125. return http_count > 0;
  126. }
  127. int64_t TestSyncedTabDelegate::GetTaskIdForNavigationId(int nav_id) const {
  128. // Task IDs are currently not used in the tests. -1 signals an unknown Task
  129. // ID.
  130. return -1;
  131. }
  132. int64_t TestSyncedTabDelegate::GetParentTaskIdForNavigationId(
  133. int nav_id) const {
  134. // Task IDs are currently not used in the tests. -1 signals an unknown Task
  135. // ID.
  136. return -1;
  137. }
  138. int64_t TestSyncedTabDelegate::GetRootTaskIdForNavigationId(int nav_id) const {
  139. // Task IDs are currently not used in the tests. -1 signals an unknown Task
  140. // ID.
  141. return -1;
  142. }
  143. PlaceholderTabDelegate::PlaceholderTabDelegate(SessionID tab_id)
  144. : tab_id_(tab_id) {}
  145. PlaceholderTabDelegate::~PlaceholderTabDelegate() = default;
  146. SessionID PlaceholderTabDelegate::GetSessionId() const {
  147. return tab_id_;
  148. }
  149. bool PlaceholderTabDelegate::IsPlaceholderTab() const {
  150. return true;
  151. }
  152. SessionID PlaceholderTabDelegate::GetWindowId() const {
  153. NOTREACHED();
  154. return SessionID::InvalidValue();
  155. }
  156. bool PlaceholderTabDelegate::IsBeingDestroyed() const {
  157. NOTREACHED();
  158. return false;
  159. }
  160. std::string PlaceholderTabDelegate::GetExtensionAppId() const {
  161. NOTREACHED();
  162. return "";
  163. }
  164. bool PlaceholderTabDelegate::IsInitialBlankNavigation() const {
  165. NOTREACHED();
  166. return false;
  167. }
  168. int PlaceholderTabDelegate::GetCurrentEntryIndex() const {
  169. NOTREACHED();
  170. return 0;
  171. }
  172. int PlaceholderTabDelegate::GetEntryCount() const {
  173. NOTREACHED();
  174. return 0;
  175. }
  176. GURL PlaceholderTabDelegate::GetVirtualURLAtIndex(int i) const {
  177. NOTREACHED();
  178. return GURL();
  179. }
  180. std::string PlaceholderTabDelegate::GetPageLanguageAtIndex(int i) const {
  181. NOTREACHED();
  182. return std::string();
  183. }
  184. void PlaceholderTabDelegate::GetSerializedNavigationAtIndex(
  185. int i,
  186. sessions::SerializedNavigationEntry* serialized_entry) const {
  187. NOTREACHED();
  188. }
  189. bool PlaceholderTabDelegate::ProfileHasChildAccount() const {
  190. NOTREACHED();
  191. return false;
  192. }
  193. const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>*
  194. PlaceholderTabDelegate::GetBlockedNavigations() const {
  195. NOTREACHED();
  196. return nullptr;
  197. }
  198. bool PlaceholderTabDelegate::ShouldSync(SyncSessionsClient* sessions_client) {
  199. NOTREACHED();
  200. return false;
  201. }
  202. int64_t PlaceholderTabDelegate::GetTaskIdForNavigationId(int nav_id) const {
  203. // Task IDs are currently not used in the tests. -1 signals an unknown Task
  204. // ID.
  205. NOTREACHED() << "Task IDs are not used for Placeholder Tabs";
  206. return -1;
  207. }
  208. int64_t PlaceholderTabDelegate::GetParentTaskIdForNavigationId(
  209. int nav_id) const {
  210. // Task IDs are currently not used in the tests. -1 signals an unknown Task
  211. // ID.
  212. NOTREACHED() << "Task IDs are not used for Placeholder Tabs";
  213. return -1;
  214. }
  215. int64_t PlaceholderTabDelegate::GetRootTaskIdForNavigationId(int nav_id) const {
  216. // Task IDs are currently not used in the tests. -1 signals an unknown Task
  217. // ID.
  218. NOTREACHED() << "Task IDs are not used for Placeholder Tabs";
  219. return -1;
  220. }
  221. TestSyncedWindowDelegate::TestSyncedWindowDelegate(
  222. SessionID window_id,
  223. sync_pb::SyncEnums_BrowserType type)
  224. : window_id_(window_id),
  225. window_type_(type),
  226. is_session_restore_in_progress_(false) {}
  227. TestSyncedWindowDelegate::~TestSyncedWindowDelegate() = default;
  228. void TestSyncedWindowDelegate::OverrideTabAt(int index,
  229. SyncedTabDelegate* delegate) {
  230. if (index >= static_cast<int>(tab_delegates_.size())) {
  231. tab_delegates_.resize(index + 1, nullptr);
  232. }
  233. tab_delegates_[index] = delegate;
  234. }
  235. void TestSyncedWindowDelegate::CloseTab(SessionID tab_id) {
  236. base::EraseIf(tab_delegates_, [tab_id](SyncedTabDelegate* tab) {
  237. return tab->GetSessionId() == tab_id;
  238. });
  239. }
  240. void TestSyncedWindowDelegate::SetIsSessionRestoreInProgress(bool value) {
  241. is_session_restore_in_progress_ = value;
  242. }
  243. bool TestSyncedWindowDelegate::HasWindow() const {
  244. return true;
  245. }
  246. SessionID TestSyncedWindowDelegate::GetSessionId() const {
  247. return window_id_;
  248. }
  249. int TestSyncedWindowDelegate::GetTabCount() const {
  250. return tab_delegates_.size();
  251. }
  252. int TestSyncedWindowDelegate::GetActiveIndex() const {
  253. return 0;
  254. }
  255. bool TestSyncedWindowDelegate::IsTypeNormal() const {
  256. return window_type_ == sync_pb::SyncEnums_BrowserType_TYPE_TABBED;
  257. }
  258. bool TestSyncedWindowDelegate::IsTypePopup() const {
  259. return window_type_ == sync_pb::SyncEnums_BrowserType_TYPE_POPUP;
  260. }
  261. bool TestSyncedWindowDelegate::IsTabPinned(const SyncedTabDelegate* tab) const {
  262. return false;
  263. }
  264. SyncedTabDelegate* TestSyncedWindowDelegate::GetTabAt(int index) const {
  265. if (index >= static_cast<int>(tab_delegates_.size())) {
  266. return nullptr;
  267. }
  268. return tab_delegates_[index];
  269. }
  270. SessionID TestSyncedWindowDelegate::GetTabIdAt(int index) const {
  271. SyncedTabDelegate* delegate = GetTabAt(index);
  272. if (!delegate) {
  273. return SessionID::InvalidValue();
  274. }
  275. return delegate->GetSessionId();
  276. }
  277. bool TestSyncedWindowDelegate::IsSessionRestoreInProgress() const {
  278. return is_session_restore_in_progress_;
  279. }
  280. bool TestSyncedWindowDelegate::ShouldSync() const {
  281. return true;
  282. }
  283. TestSyncedWindowDelegatesGetter::TestSyncedWindowDelegatesGetter() = default;
  284. TestSyncedWindowDelegatesGetter::~TestSyncedWindowDelegatesGetter() = default;
  285. void TestSyncedWindowDelegatesGetter::ResetWindows() {
  286. delegates_.clear();
  287. windows_.clear();
  288. }
  289. TestSyncedWindowDelegate* TestSyncedWindowDelegatesGetter::AddWindow(
  290. sync_pb::SyncEnums_BrowserType type,
  291. SessionID window_id) {
  292. windows_.push_back(
  293. std::make_unique<TestSyncedWindowDelegate>(window_id, type));
  294. CHECK_EQ(window_id, windows_.back()->GetSessionId());
  295. delegates_[window_id] = windows_.back().get();
  296. return windows_.back().get();
  297. }
  298. TestSyncedTabDelegate* TestSyncedWindowDelegatesGetter::AddTab(
  299. SessionID window_id,
  300. SessionID tab_id) {
  301. tabs_.push_back(std::make_unique<TestSyncedTabDelegate>(
  302. window_id, tab_id,
  303. base::BindRepeating(&DummyRouter::NotifyNav,
  304. base::Unretained(&router_))));
  305. for (auto& window : windows_) {
  306. if (window->GetSessionId() == window_id) {
  307. int tab_index = window->GetTabCount();
  308. window->OverrideTabAt(tab_index, tabs_.back().get());
  309. }
  310. }
  311. // Simulate the browser firing a tab parented notification, ahead of actual
  312. // navigations.
  313. router_.NotifyNav(tabs_.back().get());
  314. return tabs_.back().get();
  315. }
  316. void TestSyncedWindowDelegatesGetter::CloseTab(SessionID tab_id) {
  317. for (auto& window : windows_) {
  318. // CloseTab() will only take effect with the belonging window, the rest will
  319. // simply ignore the call.
  320. window->CloseTab(tab_id);
  321. }
  322. }
  323. void TestSyncedWindowDelegatesGetter::SessionRestoreComplete() {
  324. for (auto& window : windows_) {
  325. window->SetIsSessionRestoreInProgress(false);
  326. }
  327. router_.NotifySessionRestoreComplete();
  328. }
  329. LocalSessionEventRouter* TestSyncedWindowDelegatesGetter::router() {
  330. return &router_;
  331. }
  332. SyncedWindowDelegatesGetter::SyncedWindowDelegateMap
  333. TestSyncedWindowDelegatesGetter::GetSyncedWindowDelegates() {
  334. return delegates_;
  335. }
  336. const SyncedWindowDelegate* TestSyncedWindowDelegatesGetter::FindById(
  337. SessionID session_id) {
  338. for (const auto& [window_id, delegate] : delegates_) {
  339. if (delegate->GetSessionId() == session_id) {
  340. return delegate;
  341. }
  342. }
  343. return nullptr;
  344. }
  345. TestSyncedWindowDelegatesGetter::DummyRouter::DummyRouter() = default;
  346. TestSyncedWindowDelegatesGetter::DummyRouter::~DummyRouter() = default;
  347. void TestSyncedWindowDelegatesGetter::DummyRouter::StartRoutingTo(
  348. LocalSessionEventHandler* handler) {
  349. handler_ = handler;
  350. }
  351. void TestSyncedWindowDelegatesGetter::DummyRouter::Stop() {
  352. handler_ = nullptr;
  353. }
  354. void TestSyncedWindowDelegatesGetter::DummyRouter::NotifyNav(
  355. SyncedTabDelegate* tab) {
  356. if (handler_) {
  357. handler_->OnLocalTabModified(tab);
  358. }
  359. }
  360. void TestSyncedWindowDelegatesGetter::DummyRouter::
  361. NotifySessionRestoreComplete() {
  362. if (handler_) {
  363. handler_->OnSessionRestoreComplete();
  364. }
  365. }
  366. } // namespace sync_sessions