1// Copyright 2013 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/session_sync_test_helper.h"
6
7#include "base/strings/utf_string_conversions.h"
8#include "chrome/browser/sync/glue/synced_session.h"
9#include "sync/protocol/session_specifics.pb.h"
10#include "sync/protocol/sync_enums.pb.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13
14namespace browser_sync {
15
16static const char* kClientName = "name";
17static const char* kAppId = "app_id";
18static const char* kVirtualUrl = "http://foo/1";
19static const char* kReferrer = "referrer";
20static const char* kTitle = "title";
21
22// static
23void SessionSyncTestHelper::BuildSessionSpecifics(
24    const std::string& tag,
25    sync_pb::SessionSpecifics* meta) {
26  meta->set_session_tag(tag);
27  sync_pb::SessionHeader* header = meta->mutable_header();
28  header->set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
29  header->set_client_name(kClientName);
30}
31
32// static
33void SessionSyncTestHelper::AddWindowSpecifics(
34    int window_id,
35    const std::vector<int>& tab_list,
36    sync_pb::SessionSpecifics* meta) {
37  sync_pb::SessionHeader* header = meta->mutable_header();
38  sync_pb::SessionWindow* window = header->add_window();
39  window->set_window_id(window_id);
40  window->set_selected_tab_index(0);
41  window->set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED);
42  for (std::vector<int>::const_iterator iter = tab_list.begin();
43       iter != tab_list.end(); ++iter) {
44    window->add_tab(*iter);
45  }
46}
47
48// static
49void SessionSyncTestHelper::VerifySyncedSession(
50    const std::string& tag,
51    const std::vector<std::vector<SessionID::id_type> >& windows,
52    const SyncedSession& session) {
53  ASSERT_EQ(tag, session.session_tag);
54  ASSERT_EQ(SyncedSession::TYPE_LINUX, session.device_type);
55  ASSERT_EQ(kClientName, session.session_name);
56  ASSERT_EQ(windows.size(), session.windows.size());
57
58  // We assume the window id's are in increasing order.
59  int i = 0;
60  for (std::vector<std::vector<int> >::const_iterator win_iter =
61           windows.begin();
62       win_iter != windows.end(); ++win_iter, ++i) {
63    SessionWindow* win_ptr;
64    SyncedSession::SyncedWindowMap::const_iterator map_iter =
65        session.windows.find(i);
66    if (map_iter != session.windows.end())
67      win_ptr = map_iter->second;
68    else
69      FAIL();
70    ASSERT_EQ(win_iter->size(), win_ptr->tabs.size());
71    ASSERT_EQ(0, win_ptr->selected_tab_index);
72    ASSERT_EQ(1, win_ptr->type);
73    int j = 0;
74    for (std::vector<int>::const_iterator tab_iter = (*win_iter).begin();
75         tab_iter != (*win_iter).end(); ++tab_iter, ++j) {
76      SessionTab* tab = win_ptr->tabs[j];
77      ASSERT_EQ(*tab_iter, tab->tab_id.id());
78      ASSERT_EQ(1U, tab->navigations.size());
79      ASSERT_EQ(1, tab->tab_visual_index);
80      ASSERT_EQ(0, tab->current_navigation_index);
81      ASSERT_TRUE(tab->pinned);
82      ASSERT_EQ(kAppId, tab->extension_app_id);
83      ASSERT_EQ(1U, tab->navigations.size());
84      ASSERT_EQ(tab->navigations[0].virtual_url(), GURL(kVirtualUrl));
85      ASSERT_EQ(tab->navigations[0].referrer().url, GURL(kReferrer));
86      ASSERT_EQ(tab->navigations[0].title(),
87                base::ASCIIToUTF16(kTitle));
88      ASSERT_EQ(tab->navigations[0].transition_type(),
89                ui::PAGE_TRANSITION_TYPED);
90    }
91  }
92}
93
94void SessionSyncTestHelper::BuildTabSpecifics(
95    const std::string& tag,
96    int window_id,
97    int tab_id,
98    sync_pb::SessionSpecifics* tab_base) {
99  tab_base->set_session_tag(tag);
100  tab_base->set_tab_node_id(++max_tab_node_id_);
101  sync_pb::SessionTab* tab = tab_base->mutable_tab();
102  tab->set_tab_id(tab_id);
103  tab->set_tab_visual_index(1);
104  tab->set_current_navigation_index(0);
105  tab->set_pinned(true);
106  tab->set_extension_app_id(kAppId);
107  sync_pb::TabNavigation* navigation = tab->add_navigation();
108  navigation->set_virtual_url(kVirtualUrl);
109  navigation->set_referrer(kReferrer);
110  navigation->set_title(kTitle);
111  navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED);
112}
113
114void SessionSyncTestHelper::Reset() {
115  max_tab_node_id_ = 0;
116}
117
118sync_pb::SessionSpecifics SessionSyncTestHelper::BuildForeignSession(
119    const std::string& tag,
120    const std::vector<SessionID::id_type>& tab_list,
121    std::vector<sync_pb::SessionSpecifics>* tabs) {
122  sync_pb::SessionSpecifics meta;
123  BuildSessionSpecifics(tag, &meta);
124  AddWindowSpecifics(0, tab_list, &meta);
125  std::vector<sync_pb::SessionSpecifics> tabs1;
126  tabs1.resize(tab_list.size());
127  for (size_t i = 0; i < tab_list.size(); ++i) {
128    BuildTabSpecifics(tag, 0, tab_list[i], &tabs1[i]);
129  }
130
131  if (tabs)
132    tabs->swap(tabs1);
133  return meta;
134}
135
136}  // namespace browser_sync
137