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/extensions/app_sync_data.h" 6 7 #include "sync/api/string_ordinal.h" 8 #include "sync/protocol/app_specifics.pb.h" 9 #include "sync/protocol/sync.pb.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace extensions { 13 14 const char kValidId[] = "abcdefghijklmnopabcdefghijklmnop"; 15 const char kName[] = "MyExtension"; 16 const char kValidVersion[] = "0.0.0.0"; 17 const char kValidUpdateUrl[] = "http://clients2.google.com/service/update2/crx"; 18 19 class AppSyncDataTest : public testing::Test { 20 public: 21 AppSyncDataTest() {} 22 virtual ~AppSyncDataTest() {} 23 24 void SetRequiredExtensionValues( 25 sync_pb::ExtensionSpecifics* extension_specifics) { 26 extension_specifics->set_id(kValidId); 27 extension_specifics->set_update_url(kValidUpdateUrl); 28 extension_specifics->set_version(kValidVersion); 29 extension_specifics->set_enabled(false); 30 extension_specifics->set_incognito_enabled(true); 31 extension_specifics->set_remote_install(false); 32 extension_specifics->set_name(kName); 33 } 34 }; 35 36 TEST_F(AppSyncDataTest, SyncDataToExtensionSyncDataForApp) { 37 sync_pb::EntitySpecifics entity; 38 sync_pb::AppSpecifics* app_specifics = entity.mutable_app(); 39 app_specifics->set_app_launch_ordinal( 40 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); 41 app_specifics->set_page_ordinal( 42 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); 43 44 SetRequiredExtensionValues(app_specifics->mutable_extension()); 45 46 syncer::SyncData sync_data = 47 syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); 48 49 AppSyncData app_sync_data(sync_data); 50 EXPECT_EQ(app_specifics->app_launch_ordinal(), 51 app_sync_data.app_launch_ordinal().ToInternalValue()); 52 EXPECT_EQ(app_specifics->page_ordinal(), 53 app_sync_data.page_ordinal().ToInternalValue()); 54 } 55 56 57 58 TEST_F(AppSyncDataTest, ExtensionSyncDataToSyncDataForApp) { 59 sync_pb::EntitySpecifics entity; 60 sync_pb::AppSpecifics* input_specifics = entity.mutable_app(); 61 input_specifics->set_app_launch_ordinal( 62 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); 63 input_specifics->set_page_ordinal( 64 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue()); 65 66 SetRequiredExtensionValues(input_specifics->mutable_extension()); 67 68 syncer::SyncData sync_data = 69 syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); 70 AppSyncData app_sync_data(sync_data); 71 72 syncer::SyncData output_sync_data = app_sync_data.GetSyncData(); 73 EXPECT_TRUE(sync_data.GetSpecifics().has_app()); 74 const sync_pb::AppSpecifics& output_specifics = 75 output_sync_data.GetSpecifics().app(); 76 EXPECT_EQ(input_specifics->SerializeAsString(), 77 output_specifics.SerializeAsString()); 78 } 79 80 // Ensures that invalid StringOrdinals don't break ExtensionSyncData. 81 TEST_F(AppSyncDataTest, ExtensionSyncDataInvalidOrdinal) { 82 sync_pb::EntitySpecifics entity; 83 sync_pb::AppSpecifics* app_specifics = entity.mutable_app(); 84 // Set the ordinals as invalid. 85 app_specifics->set_app_launch_ordinal(""); 86 app_specifics->set_page_ordinal(""); 87 88 SetRequiredExtensionValues(app_specifics->mutable_extension()); 89 90 syncer::SyncData sync_data = 91 syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); 92 93 // There should be no issue loading the sync data. 94 AppSyncData app_sync_data(sync_data); 95 app_sync_data.GetSyncData(); 96 } 97 98 } // namespace extensions 99