Home | History | Annotate | Download | only in engine
      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 #ifndef SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_PROXY_H_
      6 #define SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_PROXY_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/macros.h"
     12 #include "sync/engine/model_type_sync_proxy.h"
     13 #include "sync/internal_api/public/non_blocking_sync_common.h"
     14 
     15 namespace syncer {
     16 
     17 // Mocks the ModelTypeSyncProxy.
     18 //
     19 // This mock is made simpler by not using any threads.  It does still have the
     20 // ability to defer execution if we need to test race conditions, though.
     21 //
     22 // It maintains some state to try to make its behavior more realistic.  It
     23 // updates this state as it creates commit requests or receives update and
     24 // commit responses.
     25 //
     26 // It keeps a log of all received messages so tests can make assertions based
     27 // on their value.
     28 class MockModelTypeSyncProxy : public ModelTypeSyncProxy {
     29  public:
     30   MockModelTypeSyncProxy();
     31   virtual ~MockModelTypeSyncProxy();
     32 
     33   // Implementation of ModelTypeSyncProxy.
     34   virtual void OnCommitCompleted(
     35       const DataTypeState& type_state,
     36       const CommitResponseDataList& response_list) OVERRIDE;
     37   virtual void OnUpdateReceived(
     38       const DataTypeState& type_state,
     39       const UpdateResponseDataList& response_list,
     40       const UpdateResponseDataList& pending_updates) OVERRIDE;
     41 
     42   // By default, this object behaves as if all messages are processed
     43   // immediately.  Sometimes it is useful to defer work until later, as might
     44   // happen in the real world if the model thread's task queue gets backed up.
     45   void SetSynchronousExecution(bool is_synchronous);
     46 
     47   // Runs any work that was deferred while this class was in asynchronous mode.
     48   //
     49   // This function is not useful unless this object is set to synchronous
     50   // execution mode, which is the default.
     51   void RunQueuedTasks();
     52 
     53   // Generate commit or deletion requests to be sent to the server.
     54   // These functions update local state to keep sequence numbers consistent.
     55   //
     56   // A real ModelTypeSyncProxy would forward these kinds of messages
     57   // directly to its attached ModelTypeSyncWorker.  These methods
     58   // return the value to the caller so the test framework can handle them as it
     59   // sees fit.
     60   CommitRequestData CommitRequest(const std::string& tag_hash,
     61                                   const sync_pb::EntitySpecifics& specifics);
     62   CommitRequestData DeleteRequest(const std::string& tag_hash);
     63 
     64   // Getters to access the log of received update responses.
     65   //
     66   // Does not includes repsonses that are in pending tasks.
     67   size_t GetNumUpdateResponses() const;
     68   UpdateResponseDataList GetNthUpdateResponse(size_t n) const;
     69   UpdateResponseDataList GetNthPendingUpdates(size_t n) const;
     70   DataTypeState GetNthTypeStateReceivedInUpdateResponse(size_t n) const;
     71 
     72   // Getters to access the log of received commit responses.
     73   //
     74   // Does not includes repsonses that are in pending tasks.
     75   size_t GetNumCommitResponses() const;
     76   CommitResponseDataList GetNthCommitResponse(size_t n) const;
     77   DataTypeState GetNthTypeStateReceivedInCommitResponse(size_t n) const;
     78 
     79   // Getters to access the lastest update response for a given tag_hash.
     80   bool HasUpdateResponse(const std::string& tag_hash) const;
     81   UpdateResponseData GetUpdateResponse(const std::string& tag_hash) const;
     82 
     83   // Getters to access the lastest commit response for a given tag_hash.
     84   bool HasCommitResponse(const std::string& tag_hash) const;
     85   CommitResponseData GetCommitResponse(const std::string& tag_hash) const;
     86 
     87  private:
     88   // Process a received commit response.
     89   //
     90   // Implemented as an Impl method so we can defer its execution in some cases.
     91   void OnCommitCompletedImpl(const DataTypeState& type_state,
     92                              const CommitResponseDataList& response_list);
     93 
     94   // Process a received update response.
     95   //
     96   // Implemented as an Impl method so we can defer its execution in some cases.
     97   void OnUpdateReceivedImpl(const DataTypeState& type_state,
     98                             const UpdateResponseDataList& response_list,
     99                             const UpdateResponseDataList& pending_updates);
    100 
    101   // Getter and setter for per-item sequence number tracking.
    102   int64 GetCurrentSequenceNumber(const std::string& tag_hash) const;
    103   int64 GetNextSequenceNumber(const std::string& tag_hash);
    104 
    105   // Getter and setter for per-item base version tracking.
    106   int64 GetBaseVersion(const std::string& tag_hash) const;
    107   void SetBaseVersion(const std::string& tag_hash, int64 version);
    108 
    109   // Getters and setter for server-assigned ID values.
    110   bool HasServerAssignedId(const std::string& tag_hash) const;
    111   const std::string& GetServerAssignedId(const std::string& tag_hash) const;
    112   void SetServerAssignedId(const std::string& tag_hash, const std::string& id);
    113 
    114   // State related to the implementation of deferred work.
    115   // See SetSynchronousExecution() for details.
    116   bool is_synchronous_;
    117   std::vector<base::Closure> pending_tasks_;
    118 
    119   // A log of messages received by this object.
    120   std::vector<CommitResponseDataList> received_commit_responses_;
    121   std::vector<UpdateResponseDataList> received_update_responses_;
    122   std::vector<UpdateResponseDataList> received_pending_updates_;
    123   std::vector<DataTypeState> type_states_received_on_update_;
    124   std::vector<DataTypeState> type_states_received_on_commit_;
    125 
    126   // Latest responses received, indexed by tag_hash.
    127   std::map<const std::string, CommitResponseData> commit_response_items_;
    128   std::map<const std::string, UpdateResponseData> update_response_items_;
    129 
    130   // The per-item state maps.
    131   std::map<const std::string, int64> sequence_numbers_;
    132   std::map<const std::string, int64> base_versions_;
    133   std::map<const std::string, std::string> assigned_ids_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(MockModelTypeSyncProxy);
    136 };
    137 
    138 }  // namespace syncer
    139 
    140 #endif  // SYNC_TEST_ENGINE_MOCK_MODEL_TYPE_SYNC_PROXY_H_
    141