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.
5 #include "chrome/browser/extensions/extension_sync_data.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/version.h"
10 #include "sync/protocol/extension_specifics.pb.h"
11 #include "sync/protocol/sync.pb.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 namespace extensions
{
19 const char kValidId
[] = "abcdefghijklmnopabcdefghijklmnop";
20 const char kVersion
[] = "1.0.0.1";
21 const char kValidUpdateUrl
[] =
22 "https://clients2.google.com/service/update2/crx";
23 const char kName
[] = "MyExtension";
25 // Serializes a protobuf structure (entity specifics) into an ExtensionSyncData
26 // and back again, and confirms that the input is the same as the output.
27 void ProtobufToSyncDataEqual(const sync_pb::EntitySpecifics
& entity
) {
28 syncer::SyncData sync_data
=
29 syncer::SyncData::CreateLocalData("sync_tag", "non_unique_title", entity
);
30 ExtensionSyncData
extension_sync_data(sync_data
);
31 syncer::SyncData output_sync_data
= extension_sync_data
.GetSyncData();
32 const sync_pb::ExtensionSpecifics
& output
=
33 output_sync_data
.GetSpecifics().extension();
34 const sync_pb::ExtensionSpecifics
& input
= entity
.extension();
36 // Check for field-by-field quality. It'd be nice if we could use
37 // AssertionResults here (instead of EXPECT_EQ) so that we could get valid
38 // line numbers, but it's not worth the ugliness of the verbose comparison.
39 EXPECT_EQ(input
.id(), output
.id());
40 EXPECT_EQ(input
.name(), output
.name());
41 EXPECT_EQ(input
.version(), output
.version());
42 EXPECT_EQ(input
.update_url(), output
.update_url());
43 EXPECT_EQ(input
.enabled(), output
.enabled());
44 EXPECT_EQ(input
.incognito_enabled(), output
.incognito_enabled());
45 EXPECT_EQ(input
.remote_install(), output
.remote_install());
46 EXPECT_EQ(input
.installed_by_custodian(), output
.installed_by_custodian());
47 EXPECT_EQ(input
.has_all_urls_enabled(), output
.has_all_urls_enabled());
48 if (input
.has_all_urls_enabled())
49 EXPECT_EQ(input
.all_urls_enabled(), output
.all_urls_enabled());
52 // Serializes an ExtensionSyncData into a protobuf structure and back again, and
53 // confirms that the input is the same as the output.
54 void SyncDataToProtobufEqual(const ExtensionSyncData
& input
) {
55 syncer::SyncData sync_data
= input
.GetSyncData();
56 ExtensionSyncData
output(sync_data
);
58 EXPECT_EQ(input
.id(), output
.id());
59 EXPECT_EQ(input
.uninstalled(), output
.uninstalled());
60 EXPECT_EQ(input
.enabled(), output
.enabled());
61 EXPECT_EQ(input
.incognito_enabled(), output
.incognito_enabled());
62 EXPECT_EQ(input
.remote_install(), output
.remote_install());
63 EXPECT_EQ(input
.installed_by_custodian(), output
.installed_by_custodian());
64 EXPECT_EQ(input
.all_urls_enabled(), output
.all_urls_enabled());
65 EXPECT_TRUE(input
.version().Equals(output
.version()));
66 EXPECT_EQ(input
.update_url(), output
.update_url());
67 EXPECT_EQ(input
.name(), output
.name());
72 class ExtensionSyncDataTest
: public testing::Test
{
75 // Tests the conversion process from a protobuf to an ExtensionSyncData and vice
77 TEST_F(ExtensionSyncDataTest
, ExtensionSyncDataForExtension
) {
78 sync_pb::EntitySpecifics entity
;
79 sync_pb::ExtensionSpecifics
* extension_specifics
= entity
.mutable_extension();
80 extension_specifics
->set_id(kValidId
);
81 extension_specifics
->set_update_url(kValidUpdateUrl
);
82 extension_specifics
->set_enabled(false);
83 extension_specifics
->set_incognito_enabled(true);
84 extension_specifics
->set_remote_install(false);
85 extension_specifics
->set_installed_by_custodian(false);
86 extension_specifics
->set_all_urls_enabled(true);
87 extension_specifics
->set_version(kVersion
);
88 extension_specifics
->set_name(kName
);
90 // Check the serialize-deserialize process for proto to ExtensionSyncData.
91 ProtobufToSyncDataEqual(entity
);
93 // Explicitly test that conversion to an ExtensionSyncData gets the correct
94 // result (otherwise we just know that conversion to/from a proto gives us
95 // the same result, but don't know that it's right).
96 ExtensionSyncData extension_sync_data
;
97 extension_sync_data
.PopulateFromExtensionSpecifics(*extension_specifics
);
98 EXPECT_EQ(kValidId
, extension_sync_data
.id());
99 EXPECT_EQ(GURL(kValidUpdateUrl
), extension_sync_data
.update_url());
100 EXPECT_FALSE(extension_sync_data
.enabled());
101 EXPECT_EQ(true, extension_sync_data
.incognito_enabled());
102 EXPECT_FALSE(extension_sync_data
.remote_install());
103 EXPECT_EQ(ExtensionSyncData::BOOLEAN_TRUE
,
104 extension_sync_data
.all_urls_enabled());
105 EXPECT_TRUE(Version(kVersion
).Equals(extension_sync_data
.version()));
106 EXPECT_EQ(std::string(kName
), extension_sync_data
.name());
108 // Check the serialize-deserialize process for ExtensionSyncData to proto.
109 SyncDataToProtobufEqual(extension_sync_data
);
111 // The most important thing to test is the "all urls" bit, since it is a
112 // tri-state boolean (and thus has more logic). Also flip another bit for a
114 extension_specifics
->set_all_urls_enabled(false);
115 extension_specifics
->set_incognito_enabled(false);
116 ProtobufToSyncDataEqual(entity
);
118 extension_sync_data
.PopulateFromExtensionSpecifics(*extension_specifics
);
119 EXPECT_EQ(ExtensionSyncData::BOOLEAN_FALSE
,
120 extension_sync_data
.all_urls_enabled());
121 EXPECT_FALSE(extension_sync_data
.incognito_enabled());
123 SyncDataToProtobufEqual(extension_sync_data
);
125 extension_specifics
->clear_all_urls_enabled();
126 ProtobufToSyncDataEqual(entity
);
128 extension_sync_data
.PopulateFromExtensionSpecifics(*extension_specifics
);
129 EXPECT_FALSE(extension_specifics
->has_all_urls_enabled());
130 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
,
131 extension_sync_data
.all_urls_enabled());
133 SyncDataToProtobufEqual(extension_sync_data
);
136 } // namespace extensions