Home | History | Annotate | Download | only in test
      1 // Copyright (C) 2013 Google Inc.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 // http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #include "storage_test_runner.h"
     16 
     17 #include <libaddressinput/callback.h>
     18 
     19 #include <gtest/gtest.h>
     20 
     21 namespace i18n {
     22 namespace addressinput {
     23 
     24 StorageTestRunner::StorageTestRunner(Storage* storage)
     25     : storage_(storage),
     26       success_(false),
     27       key_(),
     28       data_() {}
     29 
     30 void StorageTestRunner::RunAllTests() {
     31   EXPECT_NO_FATAL_FAILURE(GetWithoutPutReturnsEmptyData());
     32   EXPECT_NO_FATAL_FAILURE(GetReturnsWhatWasPut());
     33   EXPECT_NO_FATAL_FAILURE(SecondPutOverwritesData());
     34 }
     35 
     36 void StorageTestRunner::ClearValues() {
     37   success_ = false;
     38   key_.clear();
     39   data_.clear();
     40 }
     41 
     42 scoped_ptr<Storage::Callback> StorageTestRunner::BuildCallback() {
     43   return ::i18n::addressinput::BuildCallback(
     44       this, &StorageTestRunner::OnDataReady);
     45 }
     46 
     47 void StorageTestRunner::OnDataReady(bool success,
     48                                     const std::string& key,
     49                                     const std::string& data) {
     50   success_ = success;
     51   key_ = key;
     52   data_ = data;
     53 }
     54 
     55 void StorageTestRunner::GetWithoutPutReturnsEmptyData() {
     56   ClearValues();
     57   storage_->Get("key", BuildCallback());
     58 
     59   EXPECT_FALSE(success_);
     60   EXPECT_EQ("key", key_);
     61   EXPECT_TRUE(data_.empty());
     62 }
     63 
     64 void StorageTestRunner::GetReturnsWhatWasPut() {
     65   ClearValues();
     66   storage_->Put("key", make_scoped_ptr(new std::string("value")));
     67   storage_->Get("key", BuildCallback());
     68 
     69   EXPECT_TRUE(success_);
     70   EXPECT_EQ("key", key_);
     71   EXPECT_EQ("value", data_);
     72 }
     73 
     74 void StorageTestRunner::SecondPutOverwritesData() {
     75   ClearValues();
     76   storage_->Put("key", make_scoped_ptr(new std::string("bad-value")));
     77   storage_->Put("key", make_scoped_ptr(new std::string("good-value")));
     78   storage_->Get("key", BuildCallback());
     79 
     80   EXPECT_TRUE(success_);
     81   EXPECT_EQ("key", key_);
     82   EXPECT_EQ("good-value", data_);
     83 }
     84 
     85 }  // addressinput
     86 }  // i18n
     87