| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "sync/syncable/entry_kernel.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace syncer { | |
| 10 | |
| 11 namespace syncable { | |
| 12 | |
| 13 class EntryKernelTest : public testing::Test {}; | |
| 14 | |
| 15 TEST_F(EntryKernelTest, ToValue) { | |
| 16 EntryKernel kernel; | |
| 17 std::unique_ptr<base::DictionaryValue> value(kernel.ToValue(NULL)); | |
| 18 if (value) { | |
| 19 // Not much to check without repeating the ToValue() code. | |
| 20 EXPECT_TRUE(value->HasKey("isDirty")); | |
| 21 // The extra +2 is for "isDirty" and "modelType". | |
| 22 EXPECT_EQ(BIT_TEMPS_END - BEGIN_FIELDS + 2, | |
| 23 static_cast<int>(value->size())); | |
| 24 } else { | |
| 25 ADD_FAILURE(); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 bool ProtoFieldValuesEqual(const sync_pb::EntitySpecifics& v1, | |
| 30 const sync_pb::EntitySpecifics& v2) { | |
| 31 return v1.SerializeAsString() == v2.SerializeAsString(); | |
| 32 } | |
| 33 | |
| 34 bool ProtoFieldValuesAreSame(const sync_pb::EntitySpecifics& v1, | |
| 35 const sync_pb::EntitySpecifics& v2) { | |
| 36 return &v1 == &v2; | |
| 37 } | |
| 38 | |
| 39 bool ProtoFieldValueIsDefault(const sync_pb::EntitySpecifics& v) { | |
| 40 return ProtoFieldValuesAreSame(v, | |
| 41 sync_pb::EntitySpecifics::default_instance()); | |
| 42 } | |
| 43 | |
| 44 // Tests default value, assignment, and sharing of proto fields. | |
| 45 TEST_F(EntryKernelTest, ProtoFieldTest) { | |
| 46 EntryKernel kernel; | |
| 47 | |
| 48 // Check default values. | |
| 49 EXPECT_TRUE(ProtoFieldValueIsDefault(kernel.ref(SPECIFICS))); | |
| 50 EXPECT_TRUE(ProtoFieldValueIsDefault(kernel.ref(SERVER_SPECIFICS))); | |
| 51 EXPECT_TRUE(ProtoFieldValueIsDefault(kernel.ref(BASE_SERVER_SPECIFICS))); | |
| 52 | |
| 53 sync_pb::EntitySpecifics specifics; | |
| 54 | |
| 55 // Assign empty value and verify that the field still returns the | |
| 56 // default value. | |
| 57 kernel.put(SPECIFICS, specifics); | |
| 58 EXPECT_TRUE(ProtoFieldValueIsDefault(kernel.ref(SPECIFICS))); | |
| 59 EXPECT_TRUE(ProtoFieldValuesEqual(kernel.ref(SPECIFICS), specifics)); | |
| 60 | |
| 61 // Verifies that the kernel holds the copy of the value assigned to it. | |
| 62 specifics.mutable_bookmark()->set_url("http://demo/"); | |
| 63 EXPECT_FALSE(ProtoFieldValuesEqual(kernel.ref(SPECIFICS), specifics)); | |
| 64 | |
| 65 // Put the new value and verify the equality. | |
| 66 kernel.put(SPECIFICS, specifics); | |
| 67 EXPECT_TRUE(ProtoFieldValuesEqual(kernel.ref(SPECIFICS), specifics)); | |
| 68 EXPECT_FALSE(ProtoFieldValueIsDefault(kernel.ref(SPECIFICS))); | |
| 69 | |
| 70 // Copy the value between the fields and verify that exactly the same | |
| 71 // underlying value is shared. | |
| 72 kernel.copy(SPECIFICS, SERVER_SPECIFICS); | |
| 73 EXPECT_TRUE(ProtoFieldValuesEqual(kernel.ref(SERVER_SPECIFICS), specifics)); | |
| 74 EXPECT_TRUE(ProtoFieldValuesAreSame(kernel.ref(SPECIFICS), | |
| 75 kernel.ref(SERVER_SPECIFICS))); | |
| 76 | |
| 77 // Put the new value into SPECIFICS and verify that that stops sharing even | |
| 78 // though the values are still equal. | |
| 79 kernel.put(SPECIFICS, specifics); | |
| 80 EXPECT_FALSE(ProtoFieldValuesAreSame(kernel.ref(SPECIFICS), | |
| 81 kernel.ref(SERVER_SPECIFICS))); | |
| 82 } | |
| 83 | |
| 84 } // namespace syncable | |
| 85 | |
| 86 } // namespace syncer | |
| OLD | NEW |