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:
AppSyncDataTest()21 AppSyncDataTest() {}
~AppSyncDataTest()22 virtual ~AppSyncDataTest() {}
23
SetRequiredExtensionValues(sync_pb::ExtensionSpecifics * extension_specifics)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_name(kName);
32 }
33 };
34
TEST_F(AppSyncDataTest,SyncDataToExtensionSyncDataForApp)35 TEST_F(AppSyncDataTest, SyncDataToExtensionSyncDataForApp) {
36 sync_pb::EntitySpecifics entity;
37 sync_pb::AppSpecifics* app_specifics = entity.mutable_app();
38 app_specifics->set_app_launch_ordinal(
39 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue());
40 app_specifics->set_page_ordinal(
41 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue());
42
43 SetRequiredExtensionValues(app_specifics->mutable_extension());
44
45 syncer::SyncData sync_data =
46 syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity);
47
48 AppSyncData app_sync_data(sync_data);
49 EXPECT_EQ(app_specifics->app_launch_ordinal(),
50 app_sync_data.app_launch_ordinal().ToInternalValue());
51 EXPECT_EQ(app_specifics->page_ordinal(),
52 app_sync_data.page_ordinal().ToInternalValue());
53 }
54
55
56
TEST_F(AppSyncDataTest,ExtensionSyncDataToSyncDataForApp)57 TEST_F(AppSyncDataTest, ExtensionSyncDataToSyncDataForApp) {
58 sync_pb::EntitySpecifics entity;
59 sync_pb::AppSpecifics* input_specifics = entity.mutable_app();
60 input_specifics->set_app_launch_ordinal(
61 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue());
62 input_specifics->set_page_ordinal(
63 syncer::StringOrdinal::CreateInitialOrdinal().ToInternalValue());
64
65 SetRequiredExtensionValues(input_specifics->mutable_extension());
66
67 syncer::SyncData sync_data =
68 syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity);
69 AppSyncData app_sync_data(sync_data);
70
71 syncer::SyncData output_sync_data = app_sync_data.GetSyncData();
72 EXPECT_TRUE(sync_data.GetSpecifics().has_app());
73 const sync_pb::AppSpecifics& output_specifics =
74 output_sync_data.GetSpecifics().app();
75 EXPECT_EQ(input_specifics->SerializeAsString(),
76 output_specifics.SerializeAsString());
77 }
78
79 // Ensures that invalid StringOrdinals don't break ExtensionSyncData.
TEST_F(AppSyncDataTest,ExtensionSyncDataInvalidOrdinal)80 TEST_F(AppSyncDataTest, ExtensionSyncDataInvalidOrdinal) {
81 sync_pb::EntitySpecifics entity;
82 sync_pb::AppSpecifics* app_specifics = entity.mutable_app();
83 // Set the ordinals as invalid.
84 app_specifics->set_app_launch_ordinal("");
85 app_specifics->set_page_ordinal("");
86
87 SetRequiredExtensionValues(app_specifics->mutable_extension());
88
89 syncer::SyncData sync_data =
90 syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity);
91
92 // There should be no issue loading the sync data.
93 AppSyncData app_sync_data(sync_data);
94 app_sync_data.GetSyncData();
95 }
96
97 } // namespace extensions
98