[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 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/extension_sync_data.h" |
| 6 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 9 | #include "base/version.h" |
[email protected] | 1bcf30e | 2012-03-10 01:06:41 | [diff] [blame] | 10 | #include "sync/protocol/extension_specifics.pb.h" |
| 11 | #include "sync/protocol/sync.pb.h" |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | a6483d2 | 2013-07-03 22:11:00 | [diff] [blame] | 13 | #include "url/gurl.h" |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 14 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 15 | namespace extensions { |
| 16 | |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 17 | namespace { |
| 18 | |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 19 | const char kValidId[] = "abcdefghijklmnopabcdefghijklmnop"; |
[email protected] | 095ccbe4 | 2013-09-26 00:06:42 | [diff] [blame] | 20 | const char kVersion[] = "1.0.0.1"; |
| 21 | const char kValidUpdateUrl[] = |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 22 | "https://clients2.google.com/service/update2/crx"; |
| 23 | const char kName[] = "MyExtension"; |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 24 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 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(); |
| 35 | |
| 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()); |
| 50 | } |
| 51 | |
| 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); |
| 57 | |
| 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()); |
| 68 | } |
| 69 | |
| 70 | } // namespace |
| 71 | |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 72 | class ExtensionSyncDataTest : public testing::Test { |
| 73 | }; |
| 74 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 75 | // Tests the conversion process from a protobuf to an ExtensionSyncData and vice |
| 76 | // versa. |
| 77 | TEST_F(ExtensionSyncDataTest, ExtensionSyncDataForExtension) { |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 78 | sync_pb::EntitySpecifics entity; |
[email protected] | 4557d22 | 2012-03-04 23:33:36 | [diff] [blame] | 79 | sync_pb::ExtensionSpecifics* extension_specifics = entity.mutable_extension(); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 80 | extension_specifics->set_id(kValidId); |
[email protected] | 095ccbe4 | 2013-09-26 00:06:42 | [diff] [blame] | 81 | extension_specifics->set_update_url(kValidUpdateUrl); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 82 | extension_specifics->set_enabled(false); |
| 83 | extension_specifics->set_incognito_enabled(true); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 84 | extension_specifics->set_remote_install(false); |
| 85 | extension_specifics->set_installed_by_custodian(false); |
| 86 | extension_specifics->set_all_urls_enabled(true); |
[email protected] | 095ccbe4 | 2013-09-26 00:06:42 | [diff] [blame] | 87 | extension_specifics->set_version(kVersion); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 88 | extension_specifics->set_name(kName); |
[email protected] | 3bdba0d | 2011-08-23 07:17:30 | [diff] [blame] | 89 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 90 | // Check the serialize-deserialize process for proto to ExtensionSyncData. |
| 91 | ProtobufToSyncDataEqual(entity); |
| 92 | |
| 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()); |
vivek.vg | 3b60d1cd | 2015-02-17 16:54:15 | [diff] [blame^] | 100 | EXPECT_FALSE(extension_sync_data.enabled()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 101 | EXPECT_EQ(true, extension_sync_data.incognito_enabled()); |
vivek.vg | 3b60d1cd | 2015-02-17 16:54:15 | [diff] [blame^] | 102 | EXPECT_FALSE(extension_sync_data.remote_install()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 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()); |
| 107 | |
| 108 | // Check the serialize-deserialize process for ExtensionSyncData to proto. |
| 109 | SyncDataToProtobufEqual(extension_sync_data); |
| 110 | |
| 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 |
| 113 | // sanity check. |
| 114 | extension_specifics->set_all_urls_enabled(false); |
| 115 | extension_specifics->set_incognito_enabled(false); |
| 116 | ProtobufToSyncDataEqual(entity); |
| 117 | |
| 118 | extension_sync_data.PopulateFromExtensionSpecifics(*extension_specifics); |
| 119 | EXPECT_EQ(ExtensionSyncData::BOOLEAN_FALSE, |
| 120 | extension_sync_data.all_urls_enabled()); |
vivek.vg | 3b60d1cd | 2015-02-17 16:54:15 | [diff] [blame^] | 121 | EXPECT_FALSE(extension_sync_data.incognito_enabled()); |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 122 | |
| 123 | SyncDataToProtobufEqual(extension_sync_data); |
| 124 | |
| 125 | extension_specifics->clear_all_urls_enabled(); |
| 126 | ProtobufToSyncDataEqual(entity); |
| 127 | |
| 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()); |
| 132 | |
| 133 | SyncDataToProtobufEqual(extension_sync_data); |
[email protected] | 418e953e | 2011-04-27 21:30:22 | [diff] [blame] | 134 | } |
| 135 | |
rdevlin.cronin | d1aa852 | 2015-02-13 00:25:57 | [diff] [blame] | 136 | } // namespace extensions |