diff options
Diffstat (limited to 'sync/internal_api')
-rw-r--r-- | sync/internal_api/public/test/model_type_store_test_util.h | 29 | ||||
-rw-r--r-- | sync/internal_api/test/model_type_store_test_util.cc | 50 |
2 files changed, 79 insertions, 0 deletions
diff --git a/sync/internal_api/public/test/model_type_store_test_util.h b/sync/internal_api/public/test/model_type_store_test_util.h new file mode 100644 index 0000000..53d010b --- /dev/null +++ b/sync/internal_api/public/test/model_type_store_test_util.h @@ -0,0 +1,29 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef SYNC_INTERNAL_API_PUBLIC_TEST_MODEL_TYPE_STORE_TEST_UTIL_H_ +#define SYNC_INTERNAL_API_PUBLIC_TEST_MODEL_TYPE_STORE_TEST_UTIL_H_ + +#include "base/memory/scoped_ptr.h" +#include "sync/api/model_type_store.h" + +namespace syncer_v2 { + +// Util class with several static methods to facilitate writing unit tests for +// classes that use ModelTypeStore objects. +class ModelTypeStoreTestUtil { + public: + // Creates an in memory store syncronously. Be aware that to do this all + // outstanding tasks will be run as the current message loop is pumped. + static scoped_ptr<ModelTypeStore> CreateInMemoryStoreForTest(); + + // Can be curried with an owned store object to allow passing an already + // created store to a service constructor in a unit test. + static void MoveStoreToCallback(scoped_ptr<ModelTypeStore> store, + ModelTypeStore::InitCallback callback); +}; + +} // namespace syncer_v2 + +#endif // SYNC_INTERNAL_API_PUBLIC_TEST_MODEL_TYPE_STORE_TEST_UTIL_H_ diff --git a/sync/internal_api/test/model_type_store_test_util.cc b/sync/internal_api/test/model_type_store_test_util.cc new file mode 100644 index 0000000..13990d8 --- /dev/null +++ b/sync/internal_api/test/model_type_store_test_util.cc @@ -0,0 +1,50 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "sync/internal_api/public/test/model_type_store_test_util.h" + +#include <utility> + +#include "base/bind.h" +#include "base/run_loop.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace syncer_v2 { + +using Result = ModelTypeStore::Result; + +namespace { + +void MoveStoreToScopedPtr(scoped_ptr<ModelTypeStore>* out_store, + Result result, + scoped_ptr<ModelTypeStore> in_store) { + ASSERT_EQ(Result::SUCCESS, result); + std::swap(*out_store, in_store); +} + +} // namespace + +// static +scoped_ptr<ModelTypeStore> +ModelTypeStoreTestUtil::CreateInMemoryStoreForTest() { + scoped_ptr<ModelTypeStore> store; + ModelTypeStore::CreateInMemoryStoreForTest( + base::Bind(&MoveStoreToScopedPtr, &store)); + + // Force the initialization to run now, synchronously. + base::RunLoop().RunUntilIdle(); + + EXPECT_TRUE(store); + return store; +} + +// static +void ModelTypeStoreTestUtil::MoveStoreToCallback( + scoped_ptr<ModelTypeStore> store, + ModelTypeStore::InitCallback callback) { + ASSERT_TRUE(store); + callback.Run(Result::SUCCESS, std::move(store)); +} + +} // namespace syncer_v2 |