OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_sync_data.h" | 5 #include "chrome/browser/extensions/extension_sync_data.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/version.h" | 8 #include "base/version.h" |
| 9 #include "chrome/browser/sync/protocol/app_specifics.pb.h" |
| 10 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" |
9 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
| 16 #if defined(OS_WIN) |
| 17 const FilePath::CharType kExtensionFilePath[] = FILE_PATH_LITERAL("c:\\foo"); |
| 18 #elif defined(OS_POSIX) |
| 19 const FilePath::CharType kExtensionFilePath[] = FILE_PATH_LITERAL("/foo"); |
| 20 #endif |
| 21 |
| 22 const char kValidId[] = "abcdefghijklmnopabcdefghijklmnop"; |
| 23 const char kValidVersion[] = "0.0.0.0"; |
| 24 const char kVersion1[] = "1.0.0.1"; |
| 25 const char kVersion2[] = "1.0.1.0"; |
| 26 const char kVersion3[] = "1.1.0.0"; |
| 27 const char kValidUpdateUrl1[] = |
| 28 "http://clients2.google.com/service/update2/crx"; |
| 29 const char kValidUpdateUrl2[] = |
| 30 "https://clients2.google.com/service/update2/crx"; |
| 31 const char kName[] = "MyExtension"; |
| 32 const char kName2[] = "MyExtension2"; |
| 33 |
14 class ExtensionSyncDataTest : public testing::Test { | 34 class ExtensionSyncDataTest : public testing::Test { |
15 }; | 35 }; |
16 | 36 |
17 bool ExtensionSyncDataEqual(const ExtensionSyncData& a, | 37 TEST_F(ExtensionSyncDataTest, SyncDataToExtensionSyncData) { |
18 const ExtensionSyncData& b) { | 38 sync_pb::EntitySpecifics entity; |
19 return | 39 sync_pb::ExtensionSpecifics* extension_specifics = |
20 (a.id == b.id) && | 40 entity.MutableExtension(sync_pb::extension); |
21 (a.enabled == b.enabled) && | 41 extension_specifics->set_id(kValidId); |
22 (a.incognito_enabled == b.incognito_enabled) && | 42 extension_specifics->set_update_url(kValidUpdateUrl2); |
23 (a.version.Equals(b.version)) && | 43 extension_specifics->set_enabled(false); |
24 (a.update_url == b.update_url) && | 44 extension_specifics->set_incognito_enabled(true); |
25 (a.name == b.name); | 45 extension_specifics->set_version(kVersion1); |
| 46 extension_specifics->set_name(kName); |
| 47 SyncData sync_data = |
| 48 SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); |
| 49 |
| 50 ExtensionSyncData extension_sync_data(sync_data); |
| 51 EXPECT_EQ(extension_specifics->id(), extension_sync_data.id()); |
| 52 EXPECT_EQ(extension_specifics->version(), |
| 53 extension_sync_data.version().GetString()); |
| 54 EXPECT_EQ(extension_specifics->update_url(), |
| 55 extension_sync_data.update_url().spec()); |
| 56 EXPECT_EQ(extension_specifics->enabled(), extension_sync_data.enabled()); |
| 57 EXPECT_EQ(extension_specifics->incognito_enabled(), |
| 58 extension_sync_data.incognito_enabled()); |
| 59 EXPECT_EQ(extension_specifics->name(), extension_sync_data.name()); |
| 60 EXPECT_FALSE(extension_sync_data.uninstalled()); |
26 } | 61 } |
27 | 62 |
28 TEST_F(ExtensionSyncDataTest, MergeOlder) { | 63 TEST_F(ExtensionSyncDataTest, ExtensionSyncDataToSyncData) { |
29 ExtensionSyncData data; | 64 sync_pb::EntitySpecifics entity; |
30 data.id = "id"; | 65 sync_pb::ExtensionSpecifics* input_extension = |
31 data.enabled = true; | 66 entity.MutableExtension(sync_pb::extension); |
32 data.incognito_enabled = false; | 67 input_extension->set_id(kValidId); |
33 { | 68 input_extension->set_update_url(kValidUpdateUrl2); |
34 scoped_ptr<Version> version(Version::GetVersionFromString("1.2.0.0")); | 69 input_extension->set_enabled(true); |
35 data.version = *version; | 70 input_extension->set_incognito_enabled(false); |
36 } | 71 input_extension->set_version(kVersion1); |
37 data.update_url = GURL("http://www.old.com"); | 72 input_extension->set_name(kName); |
38 data.name = "data"; | 73 SyncData sync_data = |
| 74 SyncData::CreateLocalData("sync_tag", "non_unique_title", entity); |
| 75 ExtensionSyncData extension_sync_data(sync_data); |
39 | 76 |
40 ExtensionSyncData new_data; | 77 SyncData output_sync_data = extension_sync_data.GetSyncData(); |
41 new_data.id = "id"; | 78 const sync_pb::ExtensionSpecifics& output_specifics = |
42 new_data.enabled = false; | 79 output_sync_data.GetSpecifics().GetExtension(sync_pb::extension); |
43 new_data.incognito_enabled = true; | 80 EXPECT_EQ(extension_sync_data.id(), output_specifics.id()); |
44 { | 81 EXPECT_EQ(extension_sync_data.update_url().spec(), |
45 scoped_ptr<Version> version(Version::GetVersionFromString("1.1.0.0")); | 82 output_specifics.update_url()); |
46 new_data.version = *version; | 83 EXPECT_EQ(extension_sync_data.enabled(), output_specifics.enabled()); |
47 } | 84 EXPECT_EQ(extension_sync_data.incognito_enabled(), |
48 new_data.update_url = GURL("http://www.new.com"); | 85 output_specifics.incognito_enabled()); |
49 new_data.name = "new_data"; | 86 EXPECT_EQ(extension_sync_data.version().GetString(), |
50 | 87 output_specifics.version()); |
51 ExtensionSyncData expected_data = data; | 88 EXPECT_EQ(extension_sync_data.name(), output_specifics.name()); |
52 expected_data.enabled = new_data.enabled; | |
53 expected_data.incognito_enabled = new_data.incognito_enabled; | |
54 | |
55 data.Merge(new_data); | |
56 EXPECT_TRUE(ExtensionSyncDataEqual(data, expected_data)); | |
57 } | |
58 | |
59 TEST_F(ExtensionSyncDataTest, MergeNewer) { | |
60 ExtensionSyncData data; | |
61 data.id = "id"; | |
62 data.enabled = true; | |
63 data.incognito_enabled = false; | |
64 { | |
65 scoped_ptr<Version> version(Version::GetVersionFromString("1.2.0.0")); | |
66 data.version = *version; | |
67 } | |
68 data.update_url = GURL("http://www.old.com"); | |
69 data.name = "data"; | |
70 | |
71 ExtensionSyncData new_data; | |
72 new_data.id = "id"; | |
73 new_data.enabled = false; | |
74 new_data.incognito_enabled = true; | |
75 { | |
76 scoped_ptr<Version> version(Version::GetVersionFromString("1.3.0.0")); | |
77 new_data.version = *version; | |
78 } | |
79 new_data.update_url = GURL("http://www.new.com"); | |
80 new_data.name = "new_data"; | |
81 | |
82 data.Merge(new_data); | |
83 EXPECT_TRUE(ExtensionSyncDataEqual(data, new_data)); | |
84 } | 89 } |
85 | 90 |
86 } // namespace | 91 } // namespace |
OLD | NEW |