| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ntp_snippets/ntp_snippets_database.h" | 5 #include "components/ntp_snippets/ntp_snippets_database.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // Explicitly destroy any existing database first, so it releases the lock | 73 // Explicitly destroy any existing database first, so it releases the lock |
| 74 // on the file. | 74 // on the file. |
| 75 db_.reset(); | 75 db_.reset(); |
| 76 | 76 |
| 77 db_.reset(new NTPSnippetsDatabase(database_dir_.path(), | 77 db_.reset(new NTPSnippetsDatabase(database_dir_.path(), |
| 78 base::ThreadTaskRunnerHandle::Get())); | 78 base::ThreadTaskRunnerHandle::Get())); |
| 79 } | 79 } |
| 80 | 80 |
| 81 NTPSnippetsDatabase* db() { return db_.get(); } | 81 NTPSnippetsDatabase* db() { return db_.get(); } |
| 82 | 82 |
| 83 bool db_inited() { return db_->database_initialized_; } | |
| 84 | |
| 85 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) { | 83 void OnSnippetsLoaded(NTPSnippet::PtrVector snippets) { |
| 86 OnSnippetsLoadedImpl(snippets); | 84 OnSnippetsLoadedImpl(snippets); |
| 87 } | 85 } |
| 88 | 86 |
| 89 MOCK_METHOD1(OnSnippetsLoadedImpl, | 87 MOCK_METHOD1(OnSnippetsLoadedImpl, |
| 90 void(const NTPSnippet::PtrVector& snippets)); | 88 void(const NTPSnippet::PtrVector& snippets)); |
| 91 | 89 |
| 92 private: | 90 private: |
| 93 base::MessageLoop message_loop_; | 91 base::MessageLoop message_loop_; |
| 94 base::ScopedTempDir database_dir_; | 92 base::ScopedTempDir database_dir_; |
| 95 std::unique_ptr<NTPSnippetsDatabase> db_; | 93 std::unique_ptr<NTPSnippetsDatabase> db_; |
| 96 | 94 |
| 97 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabaseTest); | 95 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsDatabaseTest); |
| 98 }; | 96 }; |
| 99 | 97 |
| 100 TEST_F(NTPSnippetsDatabaseTest, Init) { | 98 TEST_F(NTPSnippetsDatabaseTest, Init) { |
| 101 ASSERT_FALSE(db()); | 99 ASSERT_FALSE(db()); |
| 102 | 100 |
| 103 CreateDatabase(); | 101 CreateDatabase(); |
| 104 EXPECT_FALSE(db_inited()); | 102 EXPECT_FALSE(db()->IsInitialized()); |
| 105 | 103 |
| 106 base::RunLoop().RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 107 EXPECT_TRUE(db_inited()); | 105 EXPECT_TRUE(db()->IsInitialized()); |
| 108 } | 106 } |
| 109 | 107 |
| 110 TEST_F(NTPSnippetsDatabaseTest, LoadBeforeInit) { | 108 TEST_F(NTPSnippetsDatabaseTest, LoadBeforeInit) { |
| 111 CreateDatabase(); | 109 CreateDatabase(); |
| 112 EXPECT_FALSE(db_inited()); | 110 EXPECT_FALSE(db()->IsInitialized()); |
| 113 | 111 |
| 114 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 112 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 115 base::Unretained(this))); | 113 base::Unretained(this))); |
| 116 | 114 |
| 117 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); | 115 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); |
| 118 base::RunLoop().RunUntilIdle(); | 116 base::RunLoop().RunUntilIdle(); |
| 119 EXPECT_TRUE(db_inited()); | 117 EXPECT_TRUE(db()->IsInitialized()); |
| 120 } | 118 } |
| 121 | 119 |
| 122 TEST_F(NTPSnippetsDatabaseTest, LoadAfterInit) { | 120 TEST_F(NTPSnippetsDatabaseTest, LoadAfterInit) { |
| 123 CreateDatabase(); | 121 CreateDatabase(); |
| 124 EXPECT_FALSE(db_inited()); | 122 EXPECT_FALSE(db()->IsInitialized()); |
| 125 | 123 |
| 126 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)).Times(0); | 124 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)).Times(0); |
| 127 base::RunLoop().RunUntilIdle(); | 125 base::RunLoop().RunUntilIdle(); |
| 128 EXPECT_TRUE(db_inited()); | 126 EXPECT_TRUE(db()->IsInitialized()); |
| 129 | 127 |
| 130 Mock::VerifyAndClearExpectations(this); | 128 Mock::VerifyAndClearExpectations(this); |
| 131 | 129 |
| 132 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 130 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 133 base::Unretained(this))); | 131 base::Unretained(this))); |
| 134 | 132 |
| 135 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); | 133 EXPECT_CALL(*this, OnSnippetsLoadedImpl(_)); |
| 136 base::RunLoop().RunUntilIdle(); | 134 base::RunLoop().RunUntilIdle(); |
| 137 } | 135 } |
| 138 | 136 |
| 139 TEST_F(NTPSnippetsDatabaseTest, Save) { | 137 TEST_F(NTPSnippetsDatabaseTest, Save) { |
| 140 CreateDatabase(); | 138 CreateDatabase(); |
| 141 base::RunLoop().RunUntilIdle(); | 139 base::RunLoop().RunUntilIdle(); |
| 142 ASSERT_TRUE(db_inited()); | 140 ASSERT_TRUE(db()->IsInitialized()); |
| 143 | 141 |
| 144 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | 142 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 145 | 143 |
| 146 db()->Save(*snippet); | 144 db()->SaveSnippet(*snippet); |
| 147 base::RunLoop().RunUntilIdle(); | 145 base::RunLoop().RunUntilIdle(); |
| 148 | 146 |
| 149 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 147 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 150 base::Unretained(this))); | 148 base::Unretained(this))); |
| 151 | 149 |
| 152 EXPECT_CALL(*this, | 150 EXPECT_CALL(*this, |
| 153 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | 151 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 154 base::RunLoop().RunUntilIdle(); | 152 base::RunLoop().RunUntilIdle(); |
| 155 | 153 |
| 156 Mock::VerifyAndClearExpectations(this); | 154 Mock::VerifyAndClearExpectations(this); |
| 157 | 155 |
| 158 // The snippet should still exist after recreating the database. | 156 // The snippet should still exist after recreating the database. |
| 159 CreateDatabase(); | 157 CreateDatabase(); |
| 160 | 158 |
| 161 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 159 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 162 base::Unretained(this))); | 160 base::Unretained(this))); |
| 163 | 161 |
| 164 EXPECT_CALL(*this, | 162 EXPECT_CALL(*this, |
| 165 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | 163 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 166 base::RunLoop().RunUntilIdle(); | 164 base::RunLoop().RunUntilIdle(); |
| 167 } | 165 } |
| 168 | 166 |
| 169 TEST_F(NTPSnippetsDatabaseTest, Update) { | 167 TEST_F(NTPSnippetsDatabaseTest, Update) { |
| 170 CreateDatabase(); | 168 CreateDatabase(); |
| 171 base::RunLoop().RunUntilIdle(); | 169 base::RunLoop().RunUntilIdle(); |
| 172 ASSERT_TRUE(db_inited()); | 170 ASSERT_TRUE(db()->IsInitialized()); |
| 173 | 171 |
| 174 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | 172 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 175 | 173 |
| 176 db()->Save(*snippet); | 174 db()->SaveSnippet(*snippet); |
| 177 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 178 | 176 |
| 179 const std::string text("some text"); | 177 const std::string text("some text"); |
| 180 snippet->set_snippet(text); | 178 snippet->set_snippet(text); |
| 181 | 179 |
| 182 db()->Save(*snippet); | 180 db()->SaveSnippet(*snippet); |
| 183 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
| 184 | 182 |
| 185 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 183 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 186 base::Unretained(this))); | 184 base::Unretained(this))); |
| 187 | 185 |
| 188 EXPECT_CALL(*this, | 186 EXPECT_CALL(*this, |
| 189 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); | 187 OnSnippetsLoadedImpl(ElementsAre(SnippetEq(snippet.get())))); |
| 190 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
| 191 } | 189 } |
| 192 | 190 |
| 193 TEST_F(NTPSnippetsDatabaseTest, Delete) { | 191 TEST_F(NTPSnippetsDatabaseTest, Delete) { |
| 194 CreateDatabase(); | 192 CreateDatabase(); |
| 195 base::RunLoop().RunUntilIdle(); | 193 base::RunLoop().RunUntilIdle(); |
| 196 ASSERT_TRUE(db_inited()); | 194 ASSERT_TRUE(db()->IsInitialized()); |
| 197 | 195 |
| 198 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); | 196 std::unique_ptr<NTPSnippet> snippet = CreateTestSnippet(); |
| 199 | 197 |
| 200 db()->Save(*snippet); | 198 db()->SaveSnippet(*snippet); |
| 201 base::RunLoop().RunUntilIdle(); | 199 base::RunLoop().RunUntilIdle(); |
| 202 | 200 |
| 203 db()->Delete(snippet->id()); | 201 db()->DeleteSnippet(snippet->id()); |
| 204 base::RunLoop().RunUntilIdle(); | 202 base::RunLoop().RunUntilIdle(); |
| 205 | 203 |
| 206 db()->Load(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, | 204 db()->LoadSnippets(base::Bind(&NTPSnippetsDatabaseTest::OnSnippetsLoaded, |
| 207 base::Unretained(this))); | 205 base::Unretained(this))); |
| 208 | 206 |
| 209 EXPECT_CALL(*this, OnSnippetsLoadedImpl(IsEmpty())); | 207 EXPECT_CALL(*this, OnSnippetsLoadedImpl(IsEmpty())); |
| 210 base::RunLoop().RunUntilIdle(); | 208 base::RunLoop().RunUntilIdle(); |
| 211 } | 209 } |
| 212 | 210 |
| 213 } // namespace ntp_snippets | 211 } // namespace ntp_snippets |
| OLD | NEW |