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 scoped_ptr
<ExtensionSyncData
> extension_sync_data
=
31 ExtensionSyncData::CreateFromSyncData(sync_data
);
32 ASSERT_TRUE(extension_sync_data
.get());
33 syncer::SyncData output_sync_data
= extension_sync_data
->GetSyncData();
34 const sync_pb::ExtensionSpecifics
& output
=
35 output_sync_data
.GetSpecifics().extension();
36 const sync_pb::ExtensionSpecifics
& input
= entity
.extension();
38 // Check for field-by-field quality. It'd be nice if we could use
39 // AssertionResults here (instead of EXPECT_EQ) so that we could get valid
40 // line numbers, but it's not worth the ugliness of the verbose comparison.
41 EXPECT_EQ(input
.id(), output
.id());
42 EXPECT_EQ(input
.name(), output
.name());
43 EXPECT_EQ(input
.version(), output
.version());
44 EXPECT_EQ(input
.update_url(), output
.update_url());
45 EXPECT_EQ(input
.enabled(), output
.enabled());
46 EXPECT_EQ(input
.incognito_enabled(), output
.incognito_enabled());
47 EXPECT_EQ(input
.remote_install(), output
.remote_install());
48 EXPECT_EQ(input
.installed_by_custodian(), output
.installed_by_custodian());
49 EXPECT_EQ(input
.has_all_urls_enabled(), output
.has_all_urls_enabled());
50 if (input
.has_all_urls_enabled())
51 EXPECT_EQ(input
.all_urls_enabled(), output
.all_urls_enabled());
54 // Serializes an ExtensionSyncData into a protobuf structure and back again, and
55 // confirms that the input is the same as the output.
56 void SyncDataToProtobufEqual(const ExtensionSyncData
& input
) {
57 syncer::SyncData sync_data
= input
.GetSyncData();
58 scoped_ptr
<ExtensionSyncData
> output
=
59 ExtensionSyncData::CreateFromSyncData(sync_data
);
60 ASSERT_TRUE(output
.get());
62 EXPECT_EQ(input
.id(), output
->id());
63 EXPECT_EQ(input
.uninstalled(), output
->uninstalled());
64 EXPECT_EQ(input
.enabled(), output
->enabled());
65 EXPECT_EQ(input
.incognito_enabled(), output
->incognito_enabled());
66 EXPECT_EQ(input
.remote_install(), output
->remote_install());
67 EXPECT_EQ(input
.installed_by_custodian(), output
->installed_by_custodian());
68 EXPECT_EQ(input
.all_urls_enabled(), output
->all_urls_enabled());
69 EXPECT_TRUE(input
.version().Equals(output
->version()));
70 EXPECT_EQ(input
.update_url(), output
->update_url());
71 EXPECT_EQ(input
.name(), output
->name());
76 class ExtensionSyncDataTest
: public testing::Test
{
79 // Tests the conversion process from a protobuf to an ExtensionSyncData and vice
81 TEST_F(ExtensionSyncDataTest
, ExtensionSyncDataForExtension
) {
82 sync_pb::EntitySpecifics entity
;
83 sync_pb::ExtensionSpecifics
* extension_specifics
= entity
.mutable_extension();
84 extension_specifics
->set_id(kValidId
);
85 extension_specifics
->set_update_url(kValidUpdateUrl
);
86 extension_specifics
->set_enabled(false);
87 extension_specifics
->set_incognito_enabled(true);
88 extension_specifics
->set_remote_install(false);
89 extension_specifics
->set_installed_by_custodian(false);
90 extension_specifics
->set_all_urls_enabled(true);
91 extension_specifics
->set_version(kVersion
);
92 extension_specifics
->set_name(kName
);
94 // Check the serialize-deserialize process for proto to ExtensionSyncData.
95 ProtobufToSyncDataEqual(entity
);
97 // Explicitly test that conversion to an ExtensionSyncData gets the correct
98 // result (otherwise we just know that conversion to/from a proto gives us
99 // the same result, but don't know that it's right).
100 ExtensionSyncData extension_sync_data
;
101 extension_sync_data
.PopulateFromExtensionSpecifics(*extension_specifics
);
102 EXPECT_EQ(kValidId
, extension_sync_data
.id());
103 EXPECT_EQ(GURL(kValidUpdateUrl
), extension_sync_data
.update_url());
104 EXPECT_FALSE(extension_sync_data
.enabled());
105 EXPECT_EQ(true, extension_sync_data
.incognito_enabled());
106 EXPECT_FALSE(extension_sync_data
.remote_install());
107 EXPECT_EQ(ExtensionSyncData::BOOLEAN_TRUE
,
108 extension_sync_data
.all_urls_enabled());
109 EXPECT_TRUE(Version(kVersion
).Equals(extension_sync_data
.version()));
110 EXPECT_EQ(std::string(kName
), extension_sync_data
.name());
112 // Check the serialize-deserialize process for ExtensionSyncData to proto.
113 SyncDataToProtobufEqual(extension_sync_data
);
115 // The most important thing to test is the "all urls" bit, since it is a
116 // tri-state boolean (and thus has more logic). Also flip another bit for a
118 extension_specifics
->set_all_urls_enabled(false);
119 extension_specifics
->set_incognito_enabled(false);
120 ProtobufToSyncDataEqual(entity
);
122 extension_sync_data
.PopulateFromExtensionSpecifics(*extension_specifics
);
123 EXPECT_EQ(ExtensionSyncData::BOOLEAN_FALSE
,
124 extension_sync_data
.all_urls_enabled());
125 EXPECT_FALSE(extension_sync_data
.incognito_enabled());
127 SyncDataToProtobufEqual(extension_sync_data
);
129 extension_specifics
->clear_all_urls_enabled();
130 ProtobufToSyncDataEqual(entity
);
132 extension_sync_data
.PopulateFromExtensionSpecifics(*extension_specifics
);
133 EXPECT_FALSE(extension_specifics
->has_all_urls_enabled());
134 EXPECT_EQ(ExtensionSyncData::BOOLEAN_UNSET
,
135 extension_sync_data
.all_urls_enabled());
137 SyncDataToProtobufEqual(extension_sync_data
);
140 } // namespace extensions