1 // Copyright (c) 2012 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/sessions/session_types.h" 6 7 #include <cstddef> 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/utf_string_conversions.h" 13 #include "base/time/time.h" 14 #include "chrome/browser/search/search.h" 15 #include "components/sessions/serialized_navigation_entry_test_helper.h" 16 #include "sync/protocol/session_specifics.pb.h" 17 #include "sync/util/time.h" 18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "ui/base/page_transition_types.h" 20 #include "url/gurl.h" 21 22 namespace { 23 24 // Create a typical SessionTab protocol buffer and set an existing 25 // SessionTab from it. The data from the protocol buffer should 26 // clobber the existing data. 27 TEST(SessionTab, FromSyncData) { 28 sync_pb::SessionTab sync_data; 29 sync_data.set_tab_id(5); 30 sync_data.set_window_id(10); 31 sync_data.set_tab_visual_index(13); 32 sync_data.set_current_navigation_index(3); 33 sync_data.set_pinned(true); 34 sync_data.set_extension_app_id("app_id"); 35 for (int i = 0; i < 5; ++i) { 36 sync_pb::TabNavigation* navigation = sync_data.add_navigation(); 37 navigation->set_virtual_url("http://foo/" + base::IntToString(i)); 38 navigation->set_referrer("referrer"); 39 navigation->set_title("title"); 40 navigation->set_page_transition(sync_pb::SyncEnums_PageTransition_TYPED); 41 } 42 43 SessionTab tab; 44 tab.window_id.set_id(100); 45 tab.tab_id.set_id(100); 46 tab.tab_visual_index = 100; 47 tab.current_navigation_index = 1000; 48 tab.pinned = false; 49 tab.extension_app_id = "fake"; 50 tab.user_agent_override = "fake"; 51 tab.timestamp = base::Time::FromInternalValue(100); 52 tab.navigations.resize(100); 53 tab.session_storage_persistent_id = "fake"; 54 55 tab.SetFromSyncData(sync_data, base::Time::FromInternalValue(5u)); 56 EXPECT_EQ(10, tab.window_id.id()); 57 EXPECT_EQ(5, tab.tab_id.id()); 58 EXPECT_EQ(13, tab.tab_visual_index); 59 EXPECT_EQ(3, tab.current_navigation_index); 60 EXPECT_TRUE(tab.pinned); 61 EXPECT_EQ("app_id", tab.extension_app_id); 62 EXPECT_TRUE(tab.user_agent_override.empty()); 63 EXPECT_EQ(5u, tab.timestamp.ToInternalValue()); 64 ASSERT_EQ(5u, tab.navigations.size()); 65 for (int i = 0; i < 5; ++i) { 66 EXPECT_EQ(i, tab.navigations[i].index()); 67 EXPECT_EQ(GURL("referrer"), tab.navigations[i].referrer().url); 68 EXPECT_EQ(base::ASCIIToUTF16("title"),tab.navigations[i].title()); 69 EXPECT_EQ(ui::PAGE_TRANSITION_TYPED, 70 tab.navigations[i].transition_type()); 71 EXPECT_EQ(GURL("http://foo/" + base::IntToString(i)), 72 tab.navigations[i].virtual_url()); 73 } 74 EXPECT_TRUE(tab.session_storage_persistent_id.empty()); 75 } 76 77 TEST(SessionTab, ToSyncData) { 78 SessionTab tab; 79 tab.window_id.set_id(10); 80 tab.tab_id.set_id(5); 81 tab.tab_visual_index = 13; 82 tab.current_navigation_index = 3; 83 tab.pinned = true; 84 tab.extension_app_id = "app_id"; 85 tab.user_agent_override = "fake"; 86 tab.timestamp = base::Time::FromInternalValue(100); 87 for (int i = 0; i < 5; ++i) { 88 tab.navigations.push_back( 89 sessions::SerializedNavigationEntryTestHelper::CreateNavigation( 90 "http://foo/" + base::IntToString(i), "title")); 91 } 92 tab.session_storage_persistent_id = "fake"; 93 94 const sync_pb::SessionTab& sync_data = tab.ToSyncData(); 95 EXPECT_EQ(5, sync_data.tab_id()); 96 EXPECT_EQ(10, sync_data.window_id()); 97 EXPECT_EQ(13, sync_data.tab_visual_index()); 98 EXPECT_EQ(3, sync_data.current_navigation_index()); 99 EXPECT_TRUE(sync_data.pinned()); 100 EXPECT_EQ("app_id", sync_data.extension_app_id()); 101 ASSERT_EQ(5, sync_data.navigation_size()); 102 for (int i = 0; i < 5; ++i) { 103 EXPECT_EQ(tab.navigations[i].virtual_url().spec(), 104 sync_data.navigation(i).virtual_url()); 105 EXPECT_EQ(base::UTF16ToUTF8(tab.navigations[i].title()), 106 sync_data.navigation(i).title()); 107 } 108 EXPECT_FALSE(sync_data.has_favicon()); 109 EXPECT_FALSE(sync_data.has_favicon_type()); 110 EXPECT_FALSE(sync_data.has_favicon_source()); 111 } 112 113 } // namespace 114