summaryrefslogtreecommitdiffstats
path: root/third_party
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-02 21:41:20 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-02 21:41:20 +0000
commit7978ca630e74ce0ed6ec2562180230103d1b7ed0 (patch)
tree88c0da52d3f40000d034c37687df51328853dcf5 /third_party
parent011a3c21f0ea8e714aaf56ecf3feed7912e6649f (diff)
downloadchromium_src-7978ca630e74ce0ed6ec2562180230103d1b7ed0.zip
chromium_src-7978ca630e74ce0ed6ec2562180230103d1b7ed0.tar.gz
chromium_src-7978ca630e74ce0ed6ec2562180230103d1b7ed0.tar.bz2
rAc i18n: implement storage interface for libaddressinput
BUG=325539 TBR=thakis@chromium.org Review URL: https://codereview.chromium.org/98623005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@242803 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party')
-rw-r--r--third_party/libaddressinput/chromium/chrome_storage_impl.cc70
-rw-r--r--third_party/libaddressinput/chromium/chrome_storage_impl.h58
-rw-r--r--third_party/libaddressinput/chromium/chrome_storage_impl_unittest.cc34
-rw-r--r--third_party/libaddressinput/chromium/cpp/libaddressinput.gyp1
-rw-r--r--third_party/libaddressinput/chromium/cpp/test/fake_storage_test.cc53
-rw-r--r--third_party/libaddressinput/chromium/cpp/test/storage_test_runner.cc86
-rw-r--r--third_party/libaddressinput/chromium/cpp/test/storage_test_runner.h57
-rw-r--r--third_party/libaddressinput/libaddressinput.gyp8
8 files changed, 320 insertions, 47 deletions
diff --git a/third_party/libaddressinput/chromium/chrome_storage_impl.cc b/third_party/libaddressinput/chromium/chrome_storage_impl.cc
new file mode 100644
index 0000000..1c731a9
--- /dev/null
+++ b/third_party/libaddressinput/chromium/chrome_storage_impl.cc
@@ -0,0 +1,70 @@
+// Copyright 2014 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 "third_party/libaddressinput/chromium/chrome_storage_impl.h"
+
+#include "base/prefs/writeable_pref_store.h"
+#include "base/values.h"
+
+ChromeStorageImpl::ChromeStorageImpl(WriteablePrefStore* store)
+ : backing_store_(store),
+ scoped_observer_(this) {
+ scoped_observer_.Add(backing_store_);
+}
+
+ChromeStorageImpl::~ChromeStorageImpl() {
+ // TODO(estade): this shouldn't be necessary.
+ for (std::vector<Request*>::iterator iter =
+ outstanding_requests_.begin();
+ iter != outstanding_requests_.end(); ++iter) {
+ (*(*iter)->callback)(false, (*iter)->key, std::string());
+ }
+}
+
+void ChromeStorageImpl::Put(const std::string& key, const std::string& data) {
+ backing_store_->SetValue(key, new base::StringValue(data));
+}
+
+void ChromeStorageImpl::Get(
+ const std::string& key,
+ scoped_ptr<Storage::Callback> data_ready) const {
+ // |Get()| should not be const, so this is just a thunk that fixes that.
+ const_cast<ChromeStorageImpl*>(this)->DoGet(key, data_ready.Pass());
+}
+
+void ChromeStorageImpl::OnPrefValueChanged(const std::string& key) {}
+
+void ChromeStorageImpl::OnInitializationCompleted(bool succeeded) {
+ for (std::vector<Request*>::iterator iter =
+ outstanding_requests_.begin();
+ iter != outstanding_requests_.end(); ++iter) {
+ DoGet((*iter)->key, (*iter)->callback.Pass());
+ }
+
+ outstanding_requests_.clear();
+}
+
+void ChromeStorageImpl::DoGet(
+ const std::string& key,
+ scoped_ptr<Storage::Callback> data_ready) {
+ if (!backing_store_->IsInitializationComplete()) {
+ outstanding_requests_.push_back(
+ new Request(key, data_ready.Pass()));
+ return;
+ }
+
+ const base::Value* value;
+ std::string result;
+ if (backing_store_->GetValue(key, &value) &&
+ value->GetAsString(&result)) {
+ (*data_ready)(true, key, result);
+ } else {
+ (*data_ready)(false, key, std::string());
+ }
+}
+
+ChromeStorageImpl::Request::Request(const std::string& key,
+ scoped_ptr<Storage::Callback> callback)
+ : key(key),
+ callback(callback.Pass()) {}
diff --git a/third_party/libaddressinput/chromium/chrome_storage_impl.h b/third_party/libaddressinput/chromium/chrome_storage_impl.h
new file mode 100644
index 0000000..11da948
--- /dev/null
+++ b/third_party/libaddressinput/chromium/chrome_storage_impl.h
@@ -0,0 +1,58 @@
+// Copyright 2014 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 THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_STORAGE_IMPL_H_
+#define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_STORAGE_IMPL_H_
+
+#include <list>
+#include <string>
+
+#include "base/memory/scoped_vector.h"
+#include "base/prefs/pref_store.h"
+#include "base/scoped_observer.h"
+#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/storage.h"
+#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/util/scoped_ptr.h"
+
+class WriteablePrefStore;
+
+// An implementation of the Storage interface which passes through to an
+// underlying WriteablePrefStore.
+class ChromeStorageImpl : public i18n::addressinput::Storage,
+ public PrefStore::Observer {
+ public:
+ // |store| must outlive |this|.
+ explicit ChromeStorageImpl(WriteablePrefStore* store);
+ virtual ~ChromeStorageImpl();
+
+ // i18n::addressinput::Storage implementation.
+ virtual void Put(const std::string& key, const std::string& data) OVERRIDE;
+ virtual void Get(const std::string& key, scoped_ptr<Callback> data_ready)
+ const OVERRIDE;
+
+ // PrefStore::Observer implementation.
+ virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
+ virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;
+
+ private:
+ struct Request {
+ Request(const std::string& key, scoped_ptr<Callback> callback);
+
+ std::string key;
+ scoped_ptr<Callback> callback;
+ };
+
+ // Non-const version of Get().
+ void DoGet(const std::string& key, scoped_ptr<Callback> data_ready);
+
+ WriteablePrefStore* backing_store_; // weak
+
+ // Get requests that haven't yet been serviced.
+ ScopedVector<Request> outstanding_requests_;
+
+ ScopedObserver<PrefStore, ChromeStorageImpl> scoped_observer_;
+
+ DISALLOW_COPY_AND_ASSIGN(ChromeStorageImpl);
+};
+
+#endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_STORAGE_IMPL_H_
diff --git a/third_party/libaddressinput/chromium/chrome_storage_impl_unittest.cc b/third_party/libaddressinput/chromium/chrome_storage_impl_unittest.cc
new file mode 100644
index 0000000..12ac2ef
--- /dev/null
+++ b/third_party/libaddressinput/chromium/chrome_storage_impl_unittest.cc
@@ -0,0 +1,34 @@
+// Copyright 2014 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 "third_party/libaddressinput/chromium/chrome_storage_impl.h"
+
+#include <string>
+
+#include "base/prefs/value_map_pref_store.h"
+#include "cpp/test/storage_test_runner.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// Tests for ChromeStorageImpl object.
+class ChromeStorageImplTest : public testing::Test {
+ protected:
+ ChromeStorageImplTest()
+ : store_(new ValueMapPrefStore()),
+ storage_(store_.get()),
+ runner_(&storage_) {}
+
+ virtual ~ChromeStorageImplTest() {}
+
+ scoped_refptr<ValueMapPrefStore> store_;
+ ChromeStorageImpl storage_;
+ i18n::addressinput::StorageTestRunner runner_;
+};
+
+TEST_F(ChromeStorageImplTest, StandardStorageTests) {
+ EXPECT_NO_FATAL_FAILURE(runner_.RunAllTests());
+}
+
+} // namespace
diff --git a/third_party/libaddressinput/chromium/cpp/libaddressinput.gyp b/third_party/libaddressinput/chromium/cpp/libaddressinput.gyp
index 4fd414b..c20637a 100644
--- a/third_party/libaddressinput/chromium/cpp/libaddressinput.gyp
+++ b/third_party/libaddressinput/chromium/cpp/libaddressinput.gyp
@@ -71,6 +71,7 @@
'test/region_data_constants_test.cc',
'test/retriever_test.cc',
'test/rule_test.cc',
+ 'test/storage_test_runner.cc',
'test/util/json_test.cc',
'test/util/md5_unittest.cc',
'test/util/scoped_ptr_unittest.cc',
diff --git a/third_party/libaddressinput/chromium/cpp/test/fake_storage_test.cc b/third_party/libaddressinput/chromium/cpp/test/fake_storage_test.cc
index 8abc3b9..ee4137e 100644
--- a/third_party/libaddressinput/chromium/cpp/test/fake_storage_test.cc
+++ b/third_party/libaddressinput/chromium/cpp/test/fake_storage_test.cc
@@ -14,14 +14,12 @@
#include "fake_storage.h"
-#include <libaddressinput/callback.h>
-#include <libaddressinput/storage.h>
-#include <libaddressinput/util/scoped_ptr.h>
-
#include <string>
#include <gtest/gtest.h>
+#include "storage_test_runner.h"
+
namespace i18n {
namespace addressinput {
@@ -30,54 +28,15 @@ namespace {
// Tests for FakeStorage object.
class FakeStorageTest : public testing::Test {
protected:
- FakeStorageTest() : storage_(), success_(false), key_(), data_() {}
+ FakeStorageTest() : storage_(), runner_(&storage_) {}
virtual ~FakeStorageTest() {}
- scoped_ptr<Storage::Callback> BuildCallback() {
- return ::i18n::addressinput::BuildCallback(
- this, &FakeStorageTest::OnDataReady);
- }
-
FakeStorage storage_;
- bool success_;
- std::string key_;
- std::string data_;
-
- private:
- void OnDataReady(bool success,
- const std::string& key,
- const std::string& data) {
- success_ = success;
- key_ = key;
- data_ = data;
- }
+ StorageTestRunner runner_;
};
-TEST_F(FakeStorageTest, GetWithoutPutReturnsEmptyData) {
- storage_.Get("key", BuildCallback());
-
- EXPECT_FALSE(success_);
- EXPECT_EQ("key", key_);
- EXPECT_TRUE(data_.empty());
-}
-
-TEST_F(FakeStorageTest, GetReturnsWhatWasPut) {
- storage_.Put("key", "value");
- storage_.Get("key", BuildCallback());
-
- EXPECT_TRUE(success_);
- EXPECT_EQ("key", key_);
- EXPECT_EQ("value", data_);
-}
-
-TEST_F(FakeStorageTest, SecondPutOverwritesData) {
- storage_.Put("key", "bad-value");
- storage_.Put("key", "good-value");
- storage_.Get("key", BuildCallback());
-
- EXPECT_TRUE(success_);
- EXPECT_EQ("key", key_);
- EXPECT_EQ("good-value", data_);
+TEST_F(FakeStorageTest, StandardStorageTests) {
+ EXPECT_NO_FATAL_FAILURE(runner_.RunAllTests());
}
} // namespace
diff --git a/third_party/libaddressinput/chromium/cpp/test/storage_test_runner.cc b/third_party/libaddressinput/chromium/cpp/test/storage_test_runner.cc
new file mode 100644
index 0000000..aac52e1
--- /dev/null
+++ b/third_party/libaddressinput/chromium/cpp/test/storage_test_runner.cc
@@ -0,0 +1,86 @@
+// Copyright (C) 2013 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "storage_test_runner.h"
+
+#include <libaddressinput/callback.h>
+
+#include <gtest/gtest.h>
+
+namespace i18n {
+namespace addressinput {
+
+StorageTestRunner::StorageTestRunner(Storage* storage)
+ : storage_(storage),
+ success_(false),
+ key_(),
+ data_() {}
+
+void StorageTestRunner::RunAllTests() {
+ EXPECT_NO_FATAL_FAILURE(GetWithoutPutReturnsEmptyData());
+ EXPECT_NO_FATAL_FAILURE(GetReturnsWhatWasPut());
+ EXPECT_NO_FATAL_FAILURE(SecondPutOverwritesData());
+}
+
+void StorageTestRunner::ClearValues() {
+ success_ = false;
+ key_.clear();
+ data_.clear();
+}
+
+scoped_ptr<Storage::Callback> StorageTestRunner::BuildCallback() {
+ return ::i18n::addressinput::BuildCallback(
+ this, &StorageTestRunner::OnDataReady);
+}
+
+void StorageTestRunner::OnDataReady(bool success,
+ const std::string& key,
+ const std::string& data) {
+ success_ = success;
+ key_ = key;
+ data_ = data;
+}
+
+void StorageTestRunner::GetWithoutPutReturnsEmptyData() {
+ ClearValues();
+ storage_->Get("key", BuildCallback());
+
+ EXPECT_FALSE(success_);
+ EXPECT_EQ("key", key_);
+ EXPECT_TRUE(data_.empty());
+}
+
+void StorageTestRunner::GetReturnsWhatWasPut() {
+ ClearValues();
+ storage_->Put("key", "value");
+ storage_->Get("key", BuildCallback());
+
+ EXPECT_TRUE(success_);
+ EXPECT_EQ("key", key_);
+ EXPECT_EQ("value", data_);
+}
+
+void StorageTestRunner::SecondPutOverwritesData() {
+ ClearValues();
+ storage_->Put("key", "bad-value");
+ storage_->Put("key", "good-value");
+ storage_->Get("key", BuildCallback());
+
+ EXPECT_TRUE(success_);
+ EXPECT_EQ("key", key_);
+ EXPECT_EQ("good-value", data_);
+}
+
+} // addressinput
+} // i18n
diff --git a/third_party/libaddressinput/chromium/cpp/test/storage_test_runner.h b/third_party/libaddressinput/chromium/cpp/test/storage_test_runner.h
new file mode 100644
index 0000000..7963dfb
--- /dev/null
+++ b/third_party/libaddressinput/chromium/cpp/test/storage_test_runner.h
@@ -0,0 +1,57 @@
+// Copyright (C) 2013 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef I18N_ADDRESSINPUT_TEST_STORAGE_TEST_RUNNER_H_
+#define I18N_ADDRESSINPUT_TEST_STORAGE_TEST_RUNNER_H_
+
+#include <libaddressinput/storage.h>
+#include <libaddressinput/util/basictypes.h>
+#include <libaddressinput/util/scoped_ptr.h>
+
+#include <string>
+
+namespace i18n {
+namespace addressinput {
+
+class StorageTestRunner {
+ public:
+ explicit StorageTestRunner(Storage* storage);
+
+ // Runs all the tests from the standard test suite.
+ void RunAllTests();
+
+ private:
+ void ClearValues();
+ scoped_ptr<Storage::Callback> BuildCallback();
+ void OnDataReady(bool success,
+ const std::string& key,
+ const std::string& data);
+
+ // Test suite.
+ void GetWithoutPutReturnsEmptyData();
+ void GetReturnsWhatWasPut();
+ void SecondPutOverwritesData();
+
+ Storage* storage_; // weak
+ bool success_;
+ std::string key_;
+ std::string data_;
+
+ DISALLOW_COPY_AND_ASSIGN(StorageTestRunner);
+};
+
+} // namespace addressinput
+} // namespace i18n
+
+#endif // I18N_ADDRESSINPUT_TEST_STORAGE_TEST_RUNNER_H_
diff --git a/third_party/libaddressinput/libaddressinput.gyp b/third_party/libaddressinput/libaddressinput.gyp
index e76285f..f19857b 100644
--- a/third_party/libaddressinput/libaddressinput.gyp
+++ b/third_party/libaddressinput/libaddressinput.gyp
@@ -49,6 +49,8 @@
'<(SHARED_INTERMEDIATE_DIR)/libaddressinput/',
],
'sources': [
+ 'chromium/chrome_storage_impl.cc',
+ 'chromium/chrome_storage_impl.h',
'chromium/json.cc',
'<(libaddressinput_dir)/cpp/include/libaddressinput/address_data.h',
'<(libaddressinput_dir)/cpp/include/libaddressinput/address_field.h',
@@ -115,6 +117,9 @@
'<(SHARED_INTERMEDIATE_DIR)/libaddressinput/',
],
'sources': [
+ 'chromium/chrome_storage_impl.cc',
+ 'chromium/chrome_storage_impl.h',
+ 'chromium/chrome_storage_impl_unittest.cc',
'<(libaddressinput_dir)/cpp/test/address_field_util_test.cc',
'<(libaddressinput_dir)/cpp/test/address_ui_test.cc',
'<(libaddressinput_dir)/cpp/test/fake_downloader.cc',
@@ -128,6 +133,8 @@
'<(libaddressinput_dir)/cpp/test/region_data_constants_test.cc',
'<(libaddressinput_dir)/cpp/test/retriever_test.cc',
'<(libaddressinput_dir)/cpp/test/rule_test.cc',
+ '<(libaddressinput_dir)/cpp/test/storage_test_runner.cc',
+ '<(libaddressinput_dir)/cpp/test/storage_test_runner.h',
'<(libaddressinput_dir)/cpp/test/util/json_test.cc',
'<(libaddressinput_dir)/cpp/test/util/md5_unittest.cc',
'<(libaddressinput_dir)/cpp/test/util/scoped_ptr_unittest.cc',
@@ -141,6 +148,7 @@
],
'dependencies': [
'libaddressinput',
+ '<(DEPTH)/base/base.gyp:base_prefs',
'<(DEPTH)/base/base.gyp:run_all_unittests',
'<(DEPTH)/testing/gtest.gyp:gtest',
],