| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "google_apis/gcm/engine/gcm_store_impl.h" | 5 #include "google_apis/gcm/engine/gcm_store_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | |
| 16 #include "base/run_loop.h" | |
| 17 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/test/test_simple_task_runner.h" |
| 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "google_apis/gcm/base/fake_encryptor.h" | 18 #include "google_apis/gcm/base/fake_encryptor.h" |
| 19 #include "google_apis/gcm/base/mcs_message.h" | 19 #include "google_apis/gcm/base/mcs_message.h" |
| 20 #include "google_apis/gcm/base/mcs_util.h" | 20 #include "google_apis/gcm/base/mcs_util.h" |
| 21 #include "google_apis/gcm/protocol/mcs.pb.h" | 21 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 namespace gcm { | 24 namespace gcm { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 54 | 54 |
| 55 void PumpLoop(); | 55 void PumpLoop(); |
| 56 | 56 |
| 57 void LoadCallback(scoped_ptr<GCMStore::LoadResult>* result_dst, | 57 void LoadCallback(scoped_ptr<GCMStore::LoadResult>* result_dst, |
| 58 scoped_ptr<GCMStore::LoadResult> result); | 58 scoped_ptr<GCMStore::LoadResult> result); |
| 59 void LoadWithoutCheckCallback(scoped_ptr<GCMStore::LoadResult>* result_dst, | 59 void LoadWithoutCheckCallback(scoped_ptr<GCMStore::LoadResult>* result_dst, |
| 60 scoped_ptr<GCMStore::LoadResult> result); | 60 scoped_ptr<GCMStore::LoadResult> result); |
| 61 void UpdateCallback(bool success); | 61 void UpdateCallback(bool success); |
| 62 | 62 |
| 63 protected: | 63 protected: |
| 64 base::MessageLoop message_loop_; | 64 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 65 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 65 base::ScopedTempDir temp_directory_; | 66 base::ScopedTempDir temp_directory_; |
| 66 bool expected_success_; | 67 bool expected_success_; |
| 67 uint64 next_persistent_id_; | 68 uint64 next_persistent_id_; |
| 68 scoped_ptr<base::RunLoop> run_loop_; | |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 GCMStoreImplTest::GCMStoreImplTest() | 71 GCMStoreImplTest::GCMStoreImplTest() |
| 72 : expected_success_(true), | 72 : task_runner_(new base::TestSimpleTaskRunner()), |
| 73 task_runner_handle_(task_runner_), |
| 74 expected_success_(true), |
| 73 next_persistent_id_(base::Time::Now().ToInternalValue()) { | 75 next_persistent_id_(base::Time::Now().ToInternalValue()) { |
| 74 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); | 76 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); |
| 75 run_loop_.reset(new base::RunLoop()); | |
| 76 } | 77 } |
| 77 | 78 |
| 78 GCMStoreImplTest::~GCMStoreImplTest() {} | 79 GCMStoreImplTest::~GCMStoreImplTest() {} |
| 79 | 80 |
| 80 scoped_ptr<GCMStoreImpl> GCMStoreImplTest::BuildGCMStore() { | 81 scoped_ptr<GCMStoreImpl> GCMStoreImplTest::BuildGCMStore() { |
| 81 return scoped_ptr<GCMStoreImpl>(new GCMStoreImpl( | 82 return scoped_ptr<GCMStoreImpl>(new GCMStoreImpl( |
| 82 // Pass an non-existent directory as store path to match the exact | 83 // Pass an non-existent directory as store path to match the exact |
| 83 // behavior in the production code. Currently GCMStoreImpl checks if | 84 // behavior in the production code. Currently GCMStoreImpl checks if |
| 84 // the directory exist or not to determine the store existence. | 85 // the directory exist or not to determine the store existence. |
| 85 temp_directory_.path().Append(FILE_PATH_LITERAL("GCM Store")), | 86 temp_directory_.path().Append(FILE_PATH_LITERAL("GCM Store")), |
| 86 message_loop_.task_runner(), | 87 task_runner_, |
| 87 make_scoped_ptr<Encryptor>(new FakeEncryptor))); | 88 make_scoped_ptr<Encryptor>(new FakeEncryptor))); |
| 88 } | 89 } |
| 89 | 90 |
| 90 void GCMStoreImplTest::LoadGCMStore( | 91 void GCMStoreImplTest::LoadGCMStore( |
| 91 GCMStoreImpl* gcm_store, scoped_ptr<GCMStore::LoadResult>* result_dst) { | 92 GCMStoreImpl* gcm_store, scoped_ptr<GCMStore::LoadResult>* result_dst) { |
| 92 gcm_store->Load( | 93 gcm_store->Load( |
| 93 GCMStore::CREATE_IF_MISSING, | 94 GCMStore::CREATE_IF_MISSING, |
| 94 base::Bind(&GCMStoreImplTest::LoadCallback, | 95 base::Bind(&GCMStoreImplTest::LoadCallback, |
| 95 base::Unretained(this), | 96 base::Unretained(this), |
| 96 result_dst)); | 97 result_dst)); |
| 97 PumpLoop(); | 98 PumpLoop(); |
| 98 } | 99 } |
| 99 | 100 |
| 100 std::string GCMStoreImplTest::GetNextPersistentId() { | 101 std::string GCMStoreImplTest::GetNextPersistentId() { |
| 101 return base::Uint64ToString(next_persistent_id_++); | 102 return base::Uint64ToString(next_persistent_id_++); |
| 102 } | 103 } |
| 103 | 104 |
| 104 void GCMStoreImplTest::PumpLoop() { message_loop_.RunUntilIdle(); } | 105 void GCMStoreImplTest::PumpLoop() { task_runner_->RunUntilIdle(); } |
| 105 | 106 |
| 106 void GCMStoreImplTest::LoadCallback( | 107 void GCMStoreImplTest::LoadCallback( |
| 107 scoped_ptr<GCMStore::LoadResult>* result_dst, | 108 scoped_ptr<GCMStore::LoadResult>* result_dst, |
| 108 scoped_ptr<GCMStore::LoadResult> result) { | 109 scoped_ptr<GCMStore::LoadResult> result) { |
| 109 ASSERT_TRUE(result->success); | 110 ASSERT_TRUE(result->success); |
| 110 LoadWithoutCheckCallback(result_dst, result.Pass()); | 111 LoadWithoutCheckCallback(result_dst, result.Pass()); |
| 111 } | 112 } |
| 112 | 113 |
| 113 void GCMStoreImplTest::LoadWithoutCheckCallback( | 114 void GCMStoreImplTest::LoadWithoutCheckCallback( |
| 114 scoped_ptr<GCMStore::LoadResult>* result_dst, | 115 scoped_ptr<GCMStore::LoadResult>* result_dst, |
| 115 scoped_ptr<GCMStore::LoadResult> result) { | 116 scoped_ptr<GCMStore::LoadResult> result) { |
| 116 *result_dst = result.Pass(); | 117 *result_dst = result.Pass(); |
| 117 run_loop_->Quit(); | |
| 118 run_loop_.reset(new base::RunLoop()); | |
| 119 } | 118 } |
| 120 | 119 |
| 121 void GCMStoreImplTest::UpdateCallback(bool success) { | 120 void GCMStoreImplTest::UpdateCallback(bool success) { |
| 122 ASSERT_EQ(expected_success_, success); | 121 ASSERT_EQ(expected_success_, success); |
| 123 } | 122 } |
| 124 | 123 |
| 125 // Verify creating a new database and loading it. | 124 // Verify creating a new database and loading it. |
| 126 TEST_F(GCMStoreImplTest, LoadNew) { | 125 TEST_F(GCMStoreImplTest, LoadNew) { |
| 127 scoped_ptr<GCMStoreImpl> gcm_store(BuildGCMStore()); | 126 scoped_ptr<GCMStoreImpl> gcm_store(BuildGCMStore()); |
| 128 scoped_ptr<GCMStore::LoadResult> load_result; | 127 scoped_ptr<GCMStore::LoadResult> load_result; |
| (...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 | 728 |
| 730 ASSERT_EQ(1u, load_result->instance_id_data.size()); | 729 ASSERT_EQ(1u, load_result->instance_id_data.size()); |
| 731 ASSERT_TRUE(load_result->instance_id_data.find(kAppName2) != | 730 ASSERT_TRUE(load_result->instance_id_data.find(kAppName2) != |
| 732 load_result->instance_id_data.end()); | 731 load_result->instance_id_data.end()); |
| 733 EXPECT_EQ(instance_id_data2, load_result->instance_id_data[kAppName2]); | 732 EXPECT_EQ(instance_id_data2, load_result->instance_id_data[kAppName2]); |
| 734 } | 733 } |
| 735 | 734 |
| 736 } // namespace | 735 } // namespace |
| 737 | 736 |
| 738 } // namespace gcm | 737 } // namespace gcm |
| OLD | NEW |