| 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(SessionModelAssociator::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(SessionModelAssociator::SessionWindowHasNoTabsToSync(win)); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 SessionWindow* win = tracker.GetWindow(tag, 0); |
| 90 session->windows.push_back(win); | 86 session->windows[0] = win; |
| 91 SessionModelAssociator::PopulateSessionWindowFromSpecifics( | 87 SessionModelAssociator::PopulateSessionWindowFromSpecifics( |
| 92 tag, window_s, base::Time(), win, &tracker); | 88 tag, window_s, base::Time(), win, &tracker); |
| 93 ASSERT_EQ(1U, win->tabs.size()); | 89 ASSERT_EQ(1U, win->tabs.size()); |
| 94 ASSERT_EQ(1, win->selected_tab_index); | 90 ASSERT_EQ(1, win->selected_tab_index); |
| 95 ASSERT_EQ(1, win->type); | 91 ASSERT_EQ(1, win->type); |
| 96 ASSERT_EQ(1U, tracker.num_synced_sessions()); | 92 ASSERT_EQ(1U, tracker.num_synced_sessions()); |
| 97 ASSERT_EQ(1U, tracker.num_synced_tabs(std::string("tag"))); | 93 ASSERT_EQ(1U, tracker.num_synced_tabs(std::string("tag"))); |
| 98 | 94 |
| 99 // We do this so that when the destructor for the tracker is called, it will | 95 // 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 | 96 // 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 | 97 // ourselves, otherwise we would run into double free errors when the |
| 102 // destructor was invoked (the true argument tells the tracker the tab | 98 // destructor was invoked (the true argument tells the tracker the tab |
| 103 // is now associated with a window). | 99 // is now associated with a window). |
| 104 ASSERT_TRUE(tracker.GetSessionTab(tag, 0, true)); | 100 ASSERT_TRUE(tracker.GetTabForWindow(tag, 0, 0)); |
| 105 } | 101 } |
| 106 | 102 |
| 107 TEST_F(SessionModelAssociatorTest, PopulateSessionTab) { | 103 TEST_F(SessionModelAssociatorTest, PopulateSessionTab) { |
| 108 sync_pb::SessionTab tab_s; | 104 sync_pb::SessionTab tab_s; |
| 105 tab_s.set_tab_id(5); |
| 109 tab_s.set_tab_visual_index(13); | 106 tab_s.set_tab_visual_index(13); |
| 110 tab_s.set_current_navigation_index(3); | 107 tab_s.set_current_navigation_index(3); |
| 111 tab_s.set_pinned(true); | 108 tab_s.set_pinned(true); |
| 112 tab_s.set_extension_app_id("app_id"); | 109 tab_s.set_extension_app_id("app_id"); |
| 113 sync_pb::TabNavigation* navigation = tab_s.add_navigation(); | 110 sync_pb::TabNavigation* navigation = tab_s.add_navigation(); |
| 114 navigation->set_index(12); | 111 navigation->set_index(12); |
| 115 navigation->set_virtual_url("http://foo/1"); | 112 navigation->set_virtual_url("http://foo/1"); |
| 116 navigation->set_referrer("referrer"); | 113 navigation->set_referrer("referrer"); |
| 117 navigation->set_title("title"); | 114 navigation->set_title("title"); |
| 118 navigation->set_page_transition(sync_pb::TabNavigation_PageTransition_TYPED); | 115 navigation->set_page_transition(sync_pb::TabNavigation_PageTransition_TYPED); |
| 119 | 116 |
| 120 SessionTab tab; | 117 SessionTab tab; |
| 118 tab.tab_id.set_id(5); // Expected to be set by the SyncedSessionTracker. |
| 121 SessionModelAssociator::PopulateSessionTabFromSpecifics( | 119 SessionModelAssociator::PopulateSessionTabFromSpecifics( |
| 122 tab_s, base::Time(), &tab); | 120 tab_s, base::Time(), &tab); |
| 121 ASSERT_EQ(5, tab.tab_id.id()); |
| 123 ASSERT_EQ(13, tab.tab_visual_index); | 122 ASSERT_EQ(13, tab.tab_visual_index); |
| 124 ASSERT_EQ(3, tab.current_navigation_index); | 123 ASSERT_EQ(3, tab.current_navigation_index); |
| 125 ASSERT_TRUE(tab.pinned); | 124 ASSERT_TRUE(tab.pinned); |
| 126 ASSERT_EQ("app_id", tab.extension_app_id); | 125 ASSERT_EQ("app_id", tab.extension_app_id); |
| 127 ASSERT_EQ(12, tab.navigations[0].index()); | 126 ASSERT_EQ(12, tab.navigations[0].index()); |
| 128 ASSERT_EQ(GURL("referrer"), tab.navigations[0].referrer()); | 127 ASSERT_EQ(GURL("referrer"), tab.navigations[0].referrer()); |
| 129 ASSERT_EQ(string16(ASCIIToUTF16("title")), tab.navigations[0].title()); | 128 ASSERT_EQ(string16(ASCIIToUTF16("title")), tab.navigations[0].title()); |
| 130 ASSERT_EQ(PageTransition::TYPED, tab.navigations[0].transition()); | 129 ASSERT_EQ(PageTransition::TYPED, tab.navigations[0].transition()); |
| 131 ASSERT_EQ(GURL("http://foo/1"), tab.navigations[0].virtual_url()); | 130 ASSERT_EQ(GURL("http://foo/1"), tab.navigations[0].virtual_url()); |
| 132 } | 131 } |
| 133 | 132 |
| 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) | 133 #if defined(OS_WIN) |
| 189 // The test is somewhat silly, and just verifies that we return a computer name. | 134 // The test is somewhat silly, and just verifies that we return a computer name. |
| 190 TEST(SessionModelAssociatorTest, TestGetComputerName) { | 135 TEST(SessionModelAssociatorTest, TestGetComputerName) { |
| 191 std::string computer_name = SessionModelAssociator::GetComputerName(); | 136 std::string computer_name = SessionModelAssociator::GetComputerName(); |
| 192 EXPECT_TRUE(!computer_name.empty()); | 137 EXPECT_TRUE(!computer_name.empty()); |
| 193 } | 138 } |
| 194 #endif | 139 #endif |
| 195 | 140 |
| 196 #if defined(OS_MACOSX) | 141 #if defined(OS_MACOSX) |
| 197 // The test is somewhat silly, and just verifies that we return a hardware | 142 // The test is somewhat silly, and just verifies that we return a hardware |
| 198 // model name. | 143 // model name. |
| 199 TEST_F(SessionModelAssociatorTest, GetHardwareModelName) { | 144 TEST_F(SessionModelAssociatorTest, GetHardwareModelName) { |
| 200 std::string hardware_model = SessionModelAssociator::GetHardwareModelName(); | 145 std::string hardware_model = SessionModelAssociator::GetHardwareModelName(); |
| 201 EXPECT_TRUE(!hardware_model.empty()); | 146 EXPECT_TRUE(!hardware_model.empty()); |
| 202 } | 147 } |
| 203 #endif | 148 #endif |
| 204 | 149 |
| 205 | 150 |
| 206 } // namespace browser_sync | 151 } // namespace browser_sync |
| OLD | NEW |