OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "components/prefs/json_pref_store.h" | 5 #include "components/prefs/json_pref_store.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
20 #include "base/metrics/histogram_samples.h" | 20 #include "base/metrics/histogram_samples.h" |
21 #include "base/path_service.h" | 21 #include "base/path_service.h" |
22 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
23 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
24 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
25 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
26 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
27 #include "base/test/histogram_tester.h" | 27 #include "base/test/histogram_tester.h" |
28 #include "base/test/simple_test_clock.h" | 28 #include "base/test/simple_test_clock.h" |
29 #include "base/threading/sequenced_task_runner_handle.h" | |
29 #include "base/threading/sequenced_worker_pool.h" | 30 #include "base/threading/sequenced_worker_pool.h" |
30 #include "base/threading/thread.h" | 31 #include "base/threading/thread.h" |
31 #include "base/values.h" | 32 #include "base/values.h" |
32 #include "components/prefs/pref_filter.h" | 33 #include "components/prefs/pref_filter.h" |
33 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
34 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
35 | 36 |
36 namespace base { | 37 namespace base { |
37 namespace { | 38 namespace { |
38 | 39 |
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
816 histogram_tester.ExpectTotalCount(histogram_name, 6); | 817 histogram_tester.ExpectTotalCount(histogram_name, 6); |
817 } | 818 } |
818 | 819 |
819 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { | 820 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
820 protected: | 821 protected: |
821 void SetUp() override { | 822 void SetUp() override { |
822 JsonPrefStoreTest::SetUp(); | 823 JsonPrefStoreTest::SetUp(); |
823 test_file_ = temp_dir_.path().AppendASCII("test.json"); | 824 test_file_ = temp_dir_.path().AppendASCII("test.json"); |
824 } | 825 } |
825 | 826 |
826 // Creates a JsonPrefStore with the given |file_writer|. | |
827 scoped_refptr<JsonPrefStore> CreatePrefStore() { | 827 scoped_refptr<JsonPrefStore> CreatePrefStore() { |
828 return new JsonPrefStore(test_file_, message_loop_.task_runner(), | 828 return new JsonPrefStore(test_file_, message_loop_.task_runner(), |
829 std::unique_ptr<PrefFilter>()); | 829 std::unique_ptr<PrefFilter>()); |
830 } | 830 } |
831 | 831 |
832 // Return the ImportantFileWriter for a given JsonPrefStore. | 832 // Return the ImportantFileWriter for a given JsonPrefStore. |
833 ImportantFileWriter* GetImportantFileWriter( | 833 ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) { |
834 scoped_refptr<JsonPrefStore> pref_store) { | |
835 return &(pref_store->writer_); | 834 return &(pref_store->writer_); |
836 } | 835 } |
837 | 836 |
838 // Get the contents of kTestFile. Pumps the message loop before returning the | 837 // Get the contents of kTestFile. Pumps the message loop before returning the |
839 // result. | 838 // result. |
840 std::string GetTestFileContents() { | 839 std::string GetTestFileContents() { |
841 RunLoop().RunUntilIdle(); | 840 RunLoop().RunUntilIdle(); |
842 std::string file_contents; | 841 std::string file_contents; |
843 ReadFileToString(test_file_, &file_contents); | 842 ReadFileToString(test_file_, &file_contents); |
844 return file_contents; | 843 return file_contents; |
845 } | 844 } |
846 | 845 |
847 private: | 846 private: |
848 base::FilePath test_file_; | 847 base::FilePath test_file_; |
849 }; | 848 }; |
850 | 849 |
851 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { | 850 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
852 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 851 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
853 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 852 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
854 | 853 |
855 // Set a normal pref and check that it gets scheduled to be written. | 854 // Set a normal pref and check that it gets scheduled to be written. |
856 ASSERT_FALSE(file_writer->HasPendingWrite()); | 855 ASSERT_FALSE(file_writer->HasPendingWrite()); |
857 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), | 856 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), |
858 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 857 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
859 ASSERT_TRUE(file_writer->HasPendingWrite()); | 858 ASSERT_TRUE(file_writer->HasPendingWrite()); |
860 file_writer->DoScheduledWrite(); | 859 file_writer->DoScheduledWrite(); |
861 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); | 860 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
862 ASSERT_FALSE(file_writer->HasPendingWrite()); | 861 ASSERT_FALSE(file_writer->HasPendingWrite()); |
863 | 862 |
(...skipping 25 matching lines...) Expand all Loading... | |
889 // Call CommitPendingWrite and check that the lossy pref and the normal pref | 888 // Call CommitPendingWrite and check that the lossy pref and the normal pref |
890 // are there with the last values set above. | 889 // are there with the last values set above. |
891 pref_store->CommitPendingWrite(); | 890 pref_store->CommitPendingWrite(); |
892 ASSERT_FALSE(file_writer->HasPendingWrite()); | 891 ASSERT_FALSE(file_writer->HasPendingWrite()); |
893 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 892 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
894 GetTestFileContents()); | 893 GetTestFileContents()); |
895 } | 894 } |
896 | 895 |
897 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { | 896 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
898 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 897 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
899 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 898 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
900 | 899 |
901 // Set a lossy pref and check that it is not scheduled to be written. | 900 // Set a lossy pref and check that it is not scheduled to be written. |
902 ASSERT_FALSE(file_writer->HasPendingWrite()); | 901 ASSERT_FALSE(file_writer->HasPendingWrite()); |
903 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 902 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), |
904 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 903 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
905 ASSERT_FALSE(file_writer->HasPendingWrite()); | 904 ASSERT_FALSE(file_writer->HasPendingWrite()); |
906 | 905 |
907 // Set a normal pref and check that it is scheduled to be written. | 906 // Set a normal pref and check that it is scheduled to be written. |
908 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), | 907 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), |
909 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 908 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
910 ASSERT_TRUE(file_writer->HasPendingWrite()); | 909 ASSERT_TRUE(file_writer->HasPendingWrite()); |
911 | 910 |
912 // Call DoScheduledWrite and check both prefs get written. | 911 // Call DoScheduledWrite and check both prefs get written. |
913 file_writer->DoScheduledWrite(); | 912 file_writer->DoScheduledWrite(); |
914 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 913 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
915 GetTestFileContents()); | 914 GetTestFileContents()); |
916 ASSERT_FALSE(file_writer->HasPendingWrite()); | 915 ASSERT_FALSE(file_writer->HasPendingWrite()); |
917 } | 916 } |
918 | 917 |
919 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { | 918 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
920 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 919 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
921 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 920 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
922 | 921 |
923 // Set a normal pref and check that it is scheduled to be written. | 922 // Set a normal pref and check that it is scheduled to be written. |
924 ASSERT_FALSE(file_writer->HasPendingWrite()); | 923 ASSERT_FALSE(file_writer->HasPendingWrite()); |
925 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), | 924 pref_store->SetValue("normal", base::MakeUnique<base::StringValue>("normal"), |
926 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 925 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
927 ASSERT_TRUE(file_writer->HasPendingWrite()); | 926 ASSERT_TRUE(file_writer->HasPendingWrite()); |
928 | 927 |
929 // Set a lossy pref and check that the write is still scheduled. | 928 // Set a lossy pref and check that the write is still scheduled. |
930 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 929 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), |
931 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 930 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
932 ASSERT_TRUE(file_writer->HasPendingWrite()); | 931 ASSERT_TRUE(file_writer->HasPendingWrite()); |
933 | 932 |
934 // Call DoScheduledWrite and check both prefs get written. | 933 // Call DoScheduledWrite and check both prefs get written. |
935 file_writer->DoScheduledWrite(); | 934 file_writer->DoScheduledWrite(); |
936 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 935 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
937 GetTestFileContents()); | 936 GetTestFileContents()); |
938 ASSERT_FALSE(file_writer->HasPendingWrite()); | 937 ASSERT_FALSE(file_writer->HasPendingWrite()); |
939 } | 938 } |
940 | 939 |
941 TEST_F(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { | 940 TEST_F(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { |
942 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 941 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
943 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 942 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); |
944 | 943 |
945 // Set a lossy pref and check that it is not scheduled to be written. | 944 // Set a lossy pref and check that it is not scheduled to be written. |
946 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), | 945 pref_store->SetValue("lossy", base::MakeUnique<base::StringValue>("lossy"), |
947 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 946 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
948 ASSERT_FALSE(file_writer->HasPendingWrite()); | 947 ASSERT_FALSE(file_writer->HasPendingWrite()); |
949 | 948 |
950 // Schedule pending lossy writes and check that it is scheduled. | 949 // Schedule pending lossy writes and check that it is scheduled. |
951 pref_store->SchedulePendingLossyWrites(); | 950 pref_store->SchedulePendingLossyWrites(); |
952 ASSERT_TRUE(file_writer->HasPendingWrite()); | 951 ASSERT_TRUE(file_writer->HasPendingWrite()); |
953 | 952 |
954 // Call CommitPendingWrite and check that the lossy pref is there with the | 953 // Call CommitPendingWrite and check that the lossy pref is there with the |
955 // last value set above. | 954 // last value set above. |
956 pref_store->CommitPendingWrite(); | 955 pref_store->CommitPendingWrite(); |
957 ASSERT_FALSE(file_writer->HasPendingWrite()); | 956 ASSERT_FALSE(file_writer->HasPendingWrite()); |
958 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); | 957 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
959 } | 958 } |
960 | 959 |
961 } // namespace base | 960 class SuccessfulWriteReplyObserver { |
961 public: | |
962 SuccessfulWriteReplyObserver() {} | |
gab
2016/09/20 01:27:42
s/{}/= default;/
proberge
2016/09/20 15:15:53
Done.
| |
963 | |
964 // Returns true if a successful write was observed via on_successful_write() | |
965 // and resets the observation state to false regardless. | |
966 bool GetAndResetObservationState() { | |
967 bool was_successful_write_observed = successful_write_reply_observed_; | |
968 successful_write_reply_observed_ = false; | |
969 return was_successful_write_observed; | |
970 } | |
971 | |
972 // Register OnWrite() to be called on the next write of |json_pref_store|. | |
973 void ObserveNextWriteCallback(JsonPrefStore* json_pref_store); | |
974 | |
975 void OnSuccessfulWrite() { | |
976 EXPECT_FALSE(successful_write_reply_observed_); | |
977 successful_write_reply_observed_ = true; | |
978 } | |
979 | |
980 private: | |
981 bool successful_write_reply_observed_ = false; | |
982 | |
983 DISALLOW_COPY_AND_ASSIGN(SuccessfulWriteReplyObserver); | |
984 }; | |
985 | |
986 void SuccessfulWriteReplyObserver::ObserveNextWriteCallback( | |
987 JsonPrefStore* json_pref_store) { | |
988 json_pref_store->RegisterOnNextSuccessfulWriteReply( | |
989 base::Bind(&SuccessfulWriteReplyObserver::OnSuccessfulWrite, | |
990 base::Unretained(this))); | |
991 } | |
992 | |
993 enum WriteCallbackObservationState { | |
994 NOT_CALLED, | |
995 CALLED_WITH_ERROR, | |
996 CALLED_WITH_SUCCESS, | |
997 }; | |
998 | |
999 class WriteCallbackObserver { | |
1000 public: | |
1001 WriteCallbackObserver() {} | |
gab
2016/09/20 01:27:42
= default
proberge
2016/09/20 15:15:53
Done.
| |
1002 | |
1003 // Register OnWrite() to be called on the next write of |json_pref_store|. | |
1004 void ObserveNextWriteCallback(JsonPrefStore* json_pref_store); | |
1005 | |
1006 // Returns true if a write was observed via OnWrite() | |
1007 // and resets the observation state to false regardless. | |
1008 WriteCallbackObservationState GetAndResetObservationState(); | |
1009 | |
1010 void OnWrite(bool success) { | |
1011 EXPECT_EQ(NOT_CALLED, observation_state_); | |
1012 observation_state_ = success ? CALLED_WITH_SUCCESS : CALLED_WITH_ERROR; | |
1013 } | |
1014 | |
1015 private: | |
1016 WriteCallbackObservationState observation_state_ = NOT_CALLED; | |
1017 | |
1018 DISALLOW_COPY_AND_ASSIGN(WriteCallbackObserver); | |
1019 }; | |
1020 | |
1021 void WriteCallbackObserver::ObserveNextWriteCallback(JsonPrefStore* writer) { | |
1022 writer->RegisterOnNextWriteCallback( | |
1023 base::Bind(&WriteCallbackObserver::OnWrite, base::Unretained(this))); | |
1024 } | |
1025 | |
1026 WriteCallbackObservationState | |
1027 WriteCallbackObserver::GetAndResetObservationState() { | |
1028 WriteCallbackObservationState state = observation_state_; | |
1029 observation_state_ = NOT_CALLED; | |
1030 return state; | |
1031 } | |
1032 | |
1033 class JsonPrefStoreCallbackTest : public JsonPrefStoreTest { | |
1034 protected: | |
1035 void SetUp() override { | |
1036 JsonPrefStoreTest::SetUp(); | |
1037 test_file_ = temp_dir_.path().AppendASCII("test.json"); | |
1038 } | |
1039 | |
1040 scoped_refptr<JsonPrefStore> CreatePrefStore() { | |
1041 return new JsonPrefStore(test_file_, message_loop_.task_runner(), | |
1042 std::unique_ptr<PrefFilter>()); | |
1043 } | |
1044 | |
1045 // Return the ImportantFileWriter for a given JsonPrefStore. | |
1046 ImportantFileWriter* GetImportantFileWriter(JsonPrefStore* pref_store) { | |
1047 return &(pref_store->writer_); | |
1048 } | |
1049 | |
1050 void TriggerFakeWriteForCallback(JsonPrefStore* pref_store, bool success) { | |
1051 JsonPrefStore::PostWriteCallback( | |
1052 pref_store->AsWeakPtr(), | |
1053 base::Bind(&WriteCallbackObserver::OnWrite, | |
1054 base::Unretained(&write_callback_observer_)), | |
1055 base::SequencedTaskRunnerHandle::Get(), success); | |
1056 } | |
1057 | |
1058 SuccessfulWriteReplyObserver successful_write_reply_observer_; | |
1059 WriteCallbackObserver write_callback_observer_; | |
1060 | |
1061 private: | |
1062 base::FilePath test_file_; | |
1063 }; | |
1064 | |
1065 TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallback) { | |
1066 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | |
1067 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); | |
1068 | |
1069 // Test RegisterOnNextWriteCallback after RegisterOnNextSuccessfulWriteReply. | |
1070 successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1071 write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1072 file_writer->WriteNow(MakeUnique<std::string>("foo")); | |
1073 RunLoop().RunUntilIdle(); | |
1074 EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1075 EXPECT_TRUE(write_callback_observer_.GetAndResetObservationState()); | |
1076 | |
1077 // Test RegisterOnNextSuccessfulWriteReply after RegisterOnNextWriteCallback. | |
1078 successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1079 write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1080 file_writer->WriteNow(MakeUnique<std::string>("foo")); | |
1081 RunLoop().RunUntilIdle(); | |
1082 EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1083 EXPECT_TRUE(write_callback_observer_.GetAndResetObservationState()); | |
1084 | |
1085 // Test RegisterOnNextSuccessfulWriteReply only. | |
1086 successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1087 file_writer->WriteNow(MakeUnique<std::string>("foo")); | |
1088 RunLoop().RunUntilIdle(); | |
1089 EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1090 EXPECT_FALSE(write_callback_observer_.GetAndResetObservationState()); | |
1091 | |
1092 // Test RegisterOnNextWriteCallback only. | |
1093 write_callback_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1094 file_writer->WriteNow(MakeUnique<std::string>("foo")); | |
1095 RunLoop().RunUntilIdle(); | |
1096 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1097 EXPECT_TRUE(write_callback_observer_.GetAndResetObservationState()); | |
1098 } | |
1099 | |
1100 TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbackWithFakeFailure) { | |
1101 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | |
1102 | |
1103 // Confirm that the observers are invoked. | |
1104 successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1105 TriggerFakeWriteForCallback(pref_store.get(), true); | |
1106 RunLoop().RunUntilIdle(); | |
1107 EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1108 EXPECT_EQ(CALLED_WITH_SUCCESS, | |
1109 write_callback_observer_.GetAndResetObservationState()); | |
1110 | |
1111 // Confirm that the observation states were reset. | |
1112 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1113 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); | |
1114 | |
1115 // Confirm that re-installing the observers works for another write. | |
1116 successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1117 TriggerFakeWriteForCallback(pref_store.get(), true); | |
1118 RunLoop().RunUntilIdle(); | |
1119 EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1120 EXPECT_EQ(CALLED_WITH_SUCCESS, | |
1121 write_callback_observer_.GetAndResetObservationState()); | |
1122 | |
1123 // Confirm that the successful observer is not invoked by an unsuccessful | |
1124 // write, and that the synchronous observer is invoked. | |
1125 successful_write_reply_observer_.ObserveNextWriteCallback(pref_store.get()); | |
1126 TriggerFakeWriteForCallback(pref_store.get(), false); | |
1127 RunLoop().RunUntilIdle(); | |
1128 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1129 EXPECT_EQ(CALLED_WITH_ERROR, | |
1130 write_callback_observer_.GetAndResetObservationState()); | |
1131 | |
1132 // Do a real write, and confirm that the successful observer was invoked after | |
1133 // being set by |PostWriteCallback| by the last TriggerFakeWriteCallback. | |
1134 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store.get()); | |
1135 file_writer->WriteNow(MakeUnique<std::string>("foo")); | |
1136 RunLoop().RunUntilIdle(); | |
1137 EXPECT_TRUE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1138 EXPECT_EQ(NOT_CALLED, write_callback_observer_.GetAndResetObservationState()); | |
1139 } | |
1140 | |
1141 TEST_F(JsonPrefStoreCallbackTest, TestPostWriteCallbackDuringProfileDeath) { | |
1142 // Create a JsonPrefStore and attach observers to it, then delete it by making | |
1143 // it go out of scope to simulate profile switch or Chrome shutdown. | |
1144 { | |
1145 scoped_refptr<JsonPrefStore> soon_out_of_scope_pref_store = | |
1146 CreatePrefStore(); | |
1147 ImportantFileWriter* file_writer = | |
1148 GetImportantFileWriter(soon_out_of_scope_pref_store.get()); | |
1149 successful_write_reply_observer_.ObserveNextWriteCallback( | |
1150 soon_out_of_scope_pref_store.get()); | |
1151 write_callback_observer_.ObserveNextWriteCallback( | |
1152 soon_out_of_scope_pref_store.get()); | |
1153 file_writer->WriteNow(MakeUnique<std::string>("foo")); | |
1154 } | |
1155 RunLoop().RunUntilIdle(); | |
1156 EXPECT_FALSE(successful_write_reply_observer_.GetAndResetObservationState()); | |
1157 EXPECT_EQ(CALLED_WITH_SUCCESS, | |
1158 write_callback_observer_.GetAndResetObservationState()); | |
1159 } | |
1160 | |
1161 } // namespace base | |
OLD | NEW |