1 // Copyright (C) 2014 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 // A mock implementation of the Source interface to be used in tests. 16 17 #ifndef I18N_ADDRESSINPUT_TEST_MOCK_SOURCE_H_ 18 #define I18N_ADDRESSINPUT_TEST_MOCK_SOURCE_H_ 19 20 #include <libaddressinput/source.h> 21 #include <libaddressinput/util/basictypes.h> 22 23 #include <map> 24 #include <string> 25 26 namespace i18n { 27 namespace addressinput { 28 29 // Gets address metadata from a key-value map. Sample usage: 30 // class MyClass { 31 // public: 32 // MyClass() : source_(), 33 // data_ready_(BuildCallback(this, &MyClass::OnDataReady)) { 34 // source_.data_.insert( 35 // std::make_pair("data/XA", "{\"id\":\"data/XA\"}")); 36 // } 37 // 38 // ~MyClass() {} 39 // 40 // void GetData(const std::string& key) { 41 // source_.Get(key, *data_ready_); 42 // } 43 // 44 // private: 45 // void OnDataReady(bool success, 46 // const std::string& key, 47 // std::string* data) { 48 // ... 49 // delete data; 50 // } 51 // 52 // MockSource source_; 53 // const scoped_ptr<const Source::Callback> data_ready_; 54 // 55 // DISALLOW_COPY_AND_ASSIGN(MyClass); 56 // }; 57 class MockSource : public Source { 58 public: 59 MockSource(); 60 virtual ~MockSource(); 61 62 // Source implementation. 63 virtual void Get(const std::string& key, const Callback& data_ready) const; 64 65 std::map<std::string, std::string> data_; 66 67 private: 68 DISALLOW_COPY_AND_ASSIGN(MockSource); 69 }; 70 71 } // namespace addressinput 72 } // namespace i18n 73 74 #endif // I18N_ADDRESSINPUT_TEST_MOCK_SOURCE_H_ 75