| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "third_party/libaddressinput/chromium/storage_test_runner.h" | |
| 6 | |
| 7 #include <cassert> | |
| 8 #include <cstddef> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" | |
| 13 | |
| 14 namespace autofill { | |
| 15 | |
| 16 using ::i18n::addressinput::Storage; | |
| 17 | |
| 18 StorageTestRunner::StorageTestRunner(Storage* storage) | |
| 19 : storage_(storage), | |
| 20 success_(false), | |
| 21 key_(), | |
| 22 data_() {} | |
| 23 | |
| 24 StorageTestRunner::~StorageTestRunner() {} | |
| 25 | |
| 26 void StorageTestRunner::RunAllTests() { | |
| 27 EXPECT_NO_FATAL_FAILURE(GetWithoutPutReturnsEmptyData()); | |
| 28 EXPECT_NO_FATAL_FAILURE(GetReturnsWhatWasPut()); | |
| 29 EXPECT_NO_FATAL_FAILURE(SecondPutOverwritesData()); | |
| 30 } | |
| 31 | |
| 32 void StorageTestRunner::ClearValues() { | |
| 33 success_ = false; | |
| 34 key_.clear(); | |
| 35 data_.clear(); | |
| 36 } | |
| 37 | |
| 38 scoped_ptr<Storage::Callback> StorageTestRunner::BuildCallback() { | |
| 39 return scoped_ptr<Storage::Callback>(::i18n::addressinput::BuildCallback( | |
| 40 this, &StorageTestRunner::OnDataReady)); | |
| 41 } | |
| 42 | |
| 43 void StorageTestRunner::OnDataReady(bool success, | |
| 44 const std::string& key, | |
| 45 std::string* data) { | |
| 46 assert(!success || data != NULL); | |
| 47 success_ = success; | |
| 48 key_ = key; | |
| 49 if (data != NULL) { | |
| 50 data_ = *data; | |
| 51 delete data; | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 void StorageTestRunner::GetWithoutPutReturnsEmptyData() { | |
| 56 ClearValues(); | |
| 57 scoped_ptr<Storage::Callback> callback(BuildCallback()); | |
| 58 storage_->Get("key", *callback); | |
| 59 | |
| 60 EXPECT_FALSE(success_); | |
| 61 EXPECT_EQ("key", key_); | |
| 62 EXPECT_TRUE(data_.empty()); | |
| 63 } | |
| 64 | |
| 65 void StorageTestRunner::GetReturnsWhatWasPut() { | |
| 66 ClearValues(); | |
| 67 storage_->Put("key", new std::string("value")); | |
| 68 scoped_ptr<Storage::Callback> callback(BuildCallback()); | |
| 69 storage_->Get("key", *callback); | |
| 70 | |
| 71 EXPECT_TRUE(success_); | |
| 72 EXPECT_EQ("key", key_); | |
| 73 EXPECT_EQ("value", data_); | |
| 74 } | |
| 75 | |
| 76 void StorageTestRunner::SecondPutOverwritesData() { | |
| 77 ClearValues(); | |
| 78 storage_->Put("key", new std::string("bad-value")); | |
| 79 storage_->Put("key", new std::string("good-value")); | |
| 80 scoped_ptr<Storage::Callback> callback(BuildCallback()); | |
| 81 storage_->Get("key", *callback); | |
| 82 | |
| 83 EXPECT_TRUE(success_); | |
| 84 EXPECT_EQ("key", key_); | |
| 85 EXPECT_EQ("good-value", data_); | |
| 86 } | |
| 87 | |
| 88 } // namespace autofill | |
| OLD | NEW |