Home | History | Annotate | Download | only in test
      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 downloader object to use in tests.
     16 
     17 #ifndef I18N_ADDRESSINPUT_TEST_MOCK_DOWNLOADER_H_
     18 #define I18N_ADDRESSINPUT_TEST_MOCK_DOWNLOADER_H_
     19 
     20 #include <libaddressinput/downloader.h>
     21 
     22 #include <map>
     23 #include <string>
     24 
     25 #include <libaddressinput/util/basictypes.h>
     26 
     27 namespace i18n {
     28 namespace addressinput {
     29 
     30 // "Downloads" serialized validation rules from a key-value map. Sample usage:
     31 //    class MyClass {
     32 //     public:
     33 //      MyClass() : downloader_(),
     34 //                  callback_(BuildCallback(this, &MyClass::OnDownloaded)) {
     35 //        downloader_.data_.insert(
     36 //            std::make_pair("data/XA", "{\"id\":\"data/XA\"}"));
     37 //      }
     38 //
     39 //      ~MyClass() {}
     40 //
     41 //      void GetData(const std::string& key) {
     42 //        downloader_.Download(std::string(MockDownloader::kMockDataUrl) + key,
     43 //                             *callback_);
     44 //      }
     45 //
     46 //     private:
     47 //      void OnDownloaded(bool success,
     48 //                        const std::string& url,
     49 //                        std::string* data) {
     50 //        ...
     51 //        delete data;
     52 //      }
     53 //
     54 //      MockDownloader downloader_;
     55 //      const scoped_ptr<const Downloader::Callback> callback_;
     56 //
     57 //      DISALLOW_COPY_AND_ASSIGN(MyClass);
     58 //    };
     59 class MockDownloader : public Downloader {
     60  public:
     61   // The mock data URL to be used in tests.
     62   static const char kMockDataUrl[];
     63 
     64   MockDownloader();
     65   virtual ~MockDownloader();
     66 
     67   // Downloader implementation.
     68   virtual void Download(const std::string& url,
     69                         const Callback& downloaded) const;
     70 
     71   std::map<std::string, std::string> data_;
     72 
     73  private:
     74   DISALLOW_COPY_AND_ASSIGN(MockDownloader);
     75 };
     76 
     77 }  // namespace addressinput
     78 }  // namespace i18n
     79 
     80 #endif  // I18N_ADDRESSINPUT_TEST_MOCK_DOWNLOADER_H_
     81