| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/sessions/session_types.h" | 9 #include "chrome/browser/sessions/session_types.h" |
| 11 #include "chrome/browser/sync/glue/session_model_associator.h" | 10 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 12 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 13 #include "content/common/page_transition_types.h" | 12 #include "content/common/page_transition_types.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 14 |
| 16 using browser_sync::SessionModelAssociator; | |
| 17 using browser_sync::SyncedSessionTracker; | |
| 18 | |
| 19 namespace browser_sync { | 15 namespace browser_sync { |
| 20 | 16 |
| 21 typedef testing::Test SessionModelAssociatorTest; | 17 typedef testing::Test SessionModelAssociatorTest; |
| 22 | 18 |
| 23 TEST_F(SessionModelAssociatorTest, SessionWindowHasNoTabsToSync) { | 19 TEST_F(SessionModelAssociatorTest, SessionWindowHasNoTabsToSync) { |
| 24 SessionWindow win; | 20 SessionWindow win; |
| 25 ASSERT_TRUE(SessionModelAssociator::SessionWindowHasNoTabsToSync(win)); | 21 ASSERT_TRUE(SessionWindowHasNoTabsToSync(win)); |
| 26 scoped_ptr<SessionTab> tab(new SessionTab()); | 22 scoped_ptr<SessionTab> tab(new SessionTab()); |
| 27 win.tabs.push_back(tab.release()); | 23 win.tabs.push_back(tab.release()); |
| 28 ASSERT_TRUE(SessionModelAssociator::SessionWindowHasNoTabsToSync(win)); | 24 ASSERT_TRUE(SessionWindowHasNoTabsToSync(win)); |
| 29 TabNavigation nav(0, GURL("about:bubba"), GURL("about:referrer"), | 25 TabNavigation nav(0, GURL("about:bubba"), GURL("about:referrer"), |
| 30 string16(ASCIIToUTF16("title")), | 26 string16(ASCIIToUTF16("title")), |
| 31 std::string("state"), 0U); | 27 std::string("state"), 0U); |
| 32 win.tabs[0]->navigations.push_back(nav); | 28 win.tabs[0]->navigations.push_back(nav); |
| 33 ASSERT_FALSE(SessionModelAssociator::SessionWindowHasNoTabsToSync(win)); | 29 ASSERT_FALSE(SessionWindowHasNoTabsToSync(win)); |
| 34 } | 30 } |
| 35 | 31 |
| 36 TEST_F(SessionModelAssociatorTest, IsValidSessionTab) { | 32 TEST_F(SessionModelAssociatorTest, IsValidSessionTab) { |
| 37 SessionTab tab; | 33 SessionTab tab; |
| 38 ASSERT_FALSE(SessionModelAssociator::IsValidSessionTab(tab)); | 34 ASSERT_FALSE(IsValidSessionTab(tab)); |
| 39 TabNavigation nav(0, GURL(chrome::kChromeUINewTabURL), | 35 TabNavigation nav(0, GURL(chrome::kChromeUINewTabURL), |
| 40 GURL("about:referrer"), | 36 GURL("about:referrer"), |
| 41 string16(ASCIIToUTF16("title")), | 37 string16(ASCIIToUTF16("title")), |
| 42 std::string("state"), 0U); | 38 std::string("state"), 0U); |
| 43 tab.navigations.push_back(nav); | 39 tab.navigations.push_back(nav); |
| 44 // NewTab does not count as valid if it's the only navigation. | 40 // NewTab does not count as valid if it's the only navigation. |
| 45 ASSERT_FALSE(SessionModelAssociator::IsValidSessionTab(tab)); | 41 ASSERT_FALSE(IsValidSessionTab(tab)); |
| 46 TabNavigation nav2(0, GURL("about:bubba"), | 42 TabNavigation nav2(0, GURL("about:bubba"), |
| 47 GURL("about:referrer"), | 43 GURL("about:referrer"), |
| 48 string16(ASCIIToUTF16("title")), | 44 string16(ASCIIToUTF16("title")), |
| 49 std::string("state"), 0U); | 45 std::string("state"), 0U); |
| 50 tab.navigations.push_back(nav2); | 46 tab.navigations.push_back(nav2); |
| 51 // Once there's another navigation, the tab is valid. | 47 // Once there's another navigation, the tab is valid. |
| 52 ASSERT_TRUE(SessionModelAssociator::IsValidSessionTab(tab)); | 48 ASSERT_TRUE(IsValidSessionTab(tab)); |
| 53 } | 49 } |
| 54 | 50 |
| 55 TEST_F(SessionModelAssociatorTest, IsValidSessionTabIgnoresFragmentForNtp) { | 51 TEST_F(SessionModelAssociatorTest, IsValidSessionTabIgnoresFragmentForNtp) { |
| 56 SessionTab tab; | 52 SessionTab tab; |
| 57 ASSERT_FALSE(SessionModelAssociator::IsValidSessionTab(tab)); | 53 ASSERT_FALSE(IsValidSessionTab(tab)); |
| 58 TabNavigation nav(0, GURL(std::string(chrome::kChromeUINewTabURL) + | 54 TabNavigation nav(0, GURL(std::string(chrome::kChromeUINewTabURL) + |
| 59 "#bookmarks"), | 55 "#bookmarks"), |
| 60 GURL("about:referrer"), | 56 GURL("about:referrer"), |
| 61 string16(ASCIIToUTF16("title")), | 57 string16(ASCIIToUTF16("title")), |
| 62 std::string("state"), 0U); | 58 std::string("state"), 0U); |
| 63 tab.navigations.push_back(nav); | 59 tab.navigations.push_back(nav); |
| 64 // NewTab does not count as valid if it's the only navigation. | 60 // NewTab does not count as valid if it's the only navigation. |
| 65 ASSERT_FALSE(SessionModelAssociator::IsValidSessionTab(tab)); | 61 ASSERT_FALSE(IsValidSessionTab(tab)); |
| 66 } | 62 } |
| 67 | 63 |
| 68 TEST_F(SessionModelAssociatorTest, PopulateSessionHeader) { | 64 TEST_F(SessionModelAssociatorTest, PopulateSessionHeader) { |
| 69 sync_pb::SessionHeader header_s; | 65 sync_pb::SessionHeader header_s; |
| 70 header_s.set_client_name("Client 1"); | 66 header_s.set_client_name("Client 1"); |
| 71 header_s.set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_WIN); | 67 header_s.set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_WIN); |
| 72 | 68 |
| 73 SyncedSession session; | 69 SyncedSession session; |
| 74 SessionModelAssociator::PopulateSessionHeaderFromSpecifics( | 70 SessionModelAssociator::PopulateSessionHeaderFromSpecifics( |
| 75 header_s, &session); | 71 header_s, &session); |
| 76 ASSERT_EQ("Client 1", session.session_name); | 72 ASSERT_EQ("Client 1", session.session_name); |
| 77 ASSERT_EQ(SyncedSession::TYPE_WIN, session.device_type); | 73 ASSERT_EQ(SyncedSession::TYPE_WIN, session.device_type); |
| 78 } | 74 } |
| 79 | 75 |
| 80 TEST_F(SessionModelAssociatorTest, PopulateSessionWindow) { | 76 TEST_F(SessionModelAssociatorTest, PopulateSessionWindow) { |
| 81 sync_pb::SessionWindow window_s; | 77 sync_pb::SessionWindow window_s; |
| 82 window_s.add_tab(0); | 78 window_s.add_tab(0); |
| 83 window_s.set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED); | 79 window_s.set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED); |
| 84 window_s.set_selected_tab_index(1); | 80 window_s.set_selected_tab_index(1); |
| 85 | 81 |
| 86 std::string tag = "tag"; | 82 std::string tag = "tag"; |
| 87 SyncedSessionTracker tracker; | 83 SyncedSessionTracker tracker; |
| 88 SyncedSession* session = tracker.GetSession(tag); | 84 SyncedSession* session = tracker.GetSession(tag); |
| 89 SessionWindow* win = new SessionWindow(); | 85 tracker.PutWindowInSession(tag, 0); |
| 90 session->windows.push_back(win); | |
| 91 SessionModelAssociator::PopulateSessionWindowFromSpecifics( | 86 SessionModelAssociator::PopulateSessionWindowFromSpecifics( |
| 92 tag, window_s, base::Time(), win, &tracker); | 87 tag, window_s, base::Time(), session->windows[0], &tracker); |
| 93 ASSERT_EQ(1U, win->tabs.size()); | 88 ASSERT_EQ(1U, session->windows[0]->tabs.size()); |
| 94 ASSERT_EQ(1, win->selected_tab_index); | 89 ASSERT_EQ(1, session->windows[0]->selected_tab_index); |
| 95 ASSERT_EQ(1, win->type); | 90 ASSERT_EQ(1, session->windows[0]->type); |
| 96 ASSERT_EQ(1U, tracker.num_synced_sessions()); | 91 ASSERT_EQ(1U, tracker.num_synced_sessions()); |
| 97 ASSERT_EQ(1U, tracker.num_synced_tabs(std::string("tag"))); | 92 ASSERT_EQ(1U, tracker.num_synced_tabs(std::string("tag"))); |
| 98 | |
| 99 // We do this so that when the destructor for the tracker is called, it will | |
| 100 // be able to delete the session, window, and tab. We can't delete these | |
| 101 // ourselves, otherwise we would run into double free errors when the | |
| 102 // destructor was invoked (the true argument tells the tracker the tab | |
| 103 // is now associated with a window). | |
| 104 ASSERT_TRUE(tracker.GetSessionTab(tag, 0, true)); | |
| 105 } | 93 } |
| 106 | 94 |
| 107 TEST_F(SessionModelAssociatorTest, PopulateSessionTab) { | 95 TEST_F(SessionModelAssociatorTest, PopulateSessionTab) { |
| 108 sync_pb::SessionTab tab_s; | 96 sync_pb::SessionTab tab_s; |
| 97 tab_s.set_tab_id(5); |
| 109 tab_s.set_tab_visual_index(13); | 98 tab_s.set_tab_visual_index(13); |
| 110 tab_s.set_current_navigation_index(3); | 99 tab_s.set_current_navigation_index(3); |
| 111 tab_s.set_pinned(true); | 100 tab_s.set_pinned(true); |
| 112 tab_s.set_extension_app_id("app_id"); | 101 tab_s.set_extension_app_id("app_id"); |
| 113 sync_pb::TabNavigation* navigation = tab_s.add_navigation(); | 102 sync_pb::TabNavigation* navigation = tab_s.add_navigation(); |
| 114 navigation->set_index(12); | 103 navigation->set_index(12); |
| 115 navigation->set_virtual_url("http://foo/1"); | 104 navigation->set_virtual_url("http://foo/1"); |
| 116 navigation->set_referrer("referrer"); | 105 navigation->set_referrer("referrer"); |
| 117 navigation->set_title("title"); | 106 navigation->set_title("title"); |
| 118 navigation->set_page_transition(sync_pb::TabNavigation_PageTransition_TYPED); | 107 navigation->set_page_transition(sync_pb::TabNavigation_PageTransition_TYPED); |
| 119 | 108 |
| 120 SessionTab tab; | 109 SessionTab tab; |
| 110 tab.tab_id.set_id(5); // Expected to be set by the SyncedSessionTracker. |
| 121 SessionModelAssociator::PopulateSessionTabFromSpecifics( | 111 SessionModelAssociator::PopulateSessionTabFromSpecifics( |
| 122 tab_s, base::Time(), &tab); | 112 tab_s, base::Time(), &tab); |
| 113 ASSERT_EQ(5, tab.tab_id.id()); |
| 123 ASSERT_EQ(13, tab.tab_visual_index); | 114 ASSERT_EQ(13, tab.tab_visual_index); |
| 124 ASSERT_EQ(3, tab.current_navigation_index); | 115 ASSERT_EQ(3, tab.current_navigation_index); |
| 125 ASSERT_TRUE(tab.pinned); | 116 ASSERT_TRUE(tab.pinned); |
| 126 ASSERT_EQ("app_id", tab.extension_app_id); | 117 ASSERT_EQ("app_id", tab.extension_app_id); |
| 127 ASSERT_EQ(12, tab.navigations[0].index()); | 118 ASSERT_EQ(12, tab.navigations[0].index()); |
| 128 ASSERT_EQ(GURL("referrer"), tab.navigations[0].referrer()); | 119 ASSERT_EQ(GURL("referrer"), tab.navigations[0].referrer()); |
| 129 ASSERT_EQ(string16(ASCIIToUTF16("title")), tab.navigations[0].title()); | 120 ASSERT_EQ(string16(ASCIIToUTF16("title")), tab.navigations[0].title()); |
| 130 ASSERT_EQ(PageTransition::TYPED, tab.navigations[0].transition()); | 121 ASSERT_EQ(PageTransition::TYPED, tab.navigations[0].transition()); |
| 131 ASSERT_EQ(GURL("http://foo/1"), tab.navigations[0].virtual_url()); | 122 ASSERT_EQ(GURL("http://foo/1"), tab.navigations[0].virtual_url()); |
| 132 } | 123 } |
| 133 | 124 |
| 134 TEST_F(SessionModelAssociatorTest, SyncedSessionTracker) { | |
| 135 const std::string tag1 = "tag"; | |
| 136 const std::string tag2 = "tag2"; | |
| 137 const std::string tag3 = "tag3"; | |
| 138 SyncedSessionTracker tracker; | |
| 139 ASSERT_TRUE(tracker.empty()); | |
| 140 ASSERT_EQ(0U, tracker.num_synced_sessions()); | |
| 141 ASSERT_EQ(0U, tracker.num_synced_tabs(tag1)); | |
| 142 SessionTab* tab = tracker.GetSessionTab(tag1, 0, false); | |
| 143 ASSERT_EQ(1U, tracker.num_synced_tabs(tag1)); | |
| 144 ASSERT_EQ(0U, tracker.num_synced_sessions()); | |
| 145 SessionTab* tab2 = tracker.GetSessionTab(tag1, 0, false); | |
| 146 ASSERT_EQ(1U, tracker.num_synced_tabs(tag1)); | |
| 147 ASSERT_EQ(0U, tracker.num_synced_sessions()); | |
| 148 ASSERT_EQ(tab, tab2); | |
| 149 tab2 = tracker.GetSessionTab(tag2, 0, false); | |
| 150 ASSERT_EQ(1U, tracker.num_synced_tabs(tag1)); | |
| 151 ASSERT_EQ(1U, tracker.num_synced_tabs(tag2)); | |
| 152 ASSERT_EQ(0U, tracker.num_synced_sessions()); | |
| 153 | |
| 154 ASSERT_FALSE(tracker.DeleteSession(tag1)); | |
| 155 ASSERT_FALSE(tracker.DeleteSession(tag3)); | |
| 156 | |
| 157 SyncedSession* session = tracker.GetSession(tag1); | |
| 158 SyncedSession* session2 = tracker.GetSession(tag2); | |
| 159 SyncedSession* session3 = tracker.GetSession(tag3); | |
| 160 ASSERT_EQ(3U, tracker.num_synced_sessions()); | |
| 161 | |
| 162 ASSERT_TRUE(session); | |
| 163 ASSERT_TRUE(session2); | |
| 164 ASSERT_TRUE(session3); | |
| 165 ASSERT_NE(session, session2); | |
| 166 ASSERT_NE(session2, session3); | |
| 167 ASSERT_TRUE(tracker.DeleteSession(tag3)); | |
| 168 ASSERT_EQ(2U, tracker.num_synced_sessions()); | |
| 169 | |
| 170 const SessionTab *tab_ptr; | |
| 171 ASSERT_TRUE(tracker.LookupSessionTab(tag1, 0, &tab_ptr)); | |
| 172 ASSERT_EQ(tab_ptr, tab); | |
| 173 | |
| 174 std::vector<SessionWindow*> windows; | |
| 175 ASSERT_TRUE(tracker.LookupSessionWindows(tag1, &windows)); | |
| 176 ASSERT_EQ(0U, windows.size()); | |
| 177 | |
| 178 // The sessions don't have valid windows, lookup should not succeed. | |
| 179 std::vector<const SyncedSession*> sessions; | |
| 180 ASSERT_FALSE(tracker.LookupAllForeignSessions(&sessions)); | |
| 181 | |
| 182 tracker.clear(); | |
| 183 ASSERT_EQ(0U, tracker.num_synced_tabs(tag1)); | |
| 184 ASSERT_EQ(0U, tracker.num_synced_tabs(tag2)); | |
| 185 ASSERT_EQ(0U, tracker.num_synced_sessions()); | |
| 186 } | |
| 187 | |
| 188 #if defined(OS_WIN) | 125 #if defined(OS_WIN) |
| 189 // The test is somewhat silly, and just verifies that we return a computer name. | 126 // The test is somewhat silly, and just verifies that we return a computer name. |
| 190 TEST(SessionModelAssociatorTest, TestGetComputerName) { | 127 TEST(SessionModelAssociatorTest, TestGetComputerName) { |
| 191 std::string computer_name = SessionModelAssociator::GetComputerName(); | 128 std::string computer_name = SessionModelAssociator::GetComputerName(); |
| 192 EXPECT_TRUE(!computer_name.empty()); | 129 EXPECT_TRUE(!computer_name.empty()); |
| 193 } | 130 } |
| 194 #endif | 131 #endif |
| 195 | 132 |
| 196 #if defined(OS_MACOSX) | 133 #if defined(OS_MACOSX) |
| 197 // The test is somewhat silly, and just verifies that we return a hardware | 134 // The test is somewhat silly, and just verifies that we return a hardware |
| 198 // model name. | 135 // model name. |
| 199 TEST_F(SessionModelAssociatorTest, GetHardwareModelName) { | 136 TEST_F(SessionModelAssociatorTest, GetHardwareModelName) { |
| 200 std::string hardware_model = SessionModelAssociator::GetHardwareModelName(); | 137 std::string hardware_model = SessionModelAssociator::GetHardwareModelName(); |
| 201 EXPECT_TRUE(!hardware_model.empty()); | 138 EXPECT_TRUE(!hardware_model.empty()); |
| 202 } | 139 } |
| 203 #endif | 140 #endif |
| 204 | 141 |
| 205 | 142 |
| 206 } // namespace browser_sync | 143 } // namespace browser_sync |
| OLD | NEW |