Home | History | Annotate | Download | only in fake_server
      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 "sync/test/fake_server/fake_server_verifier.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/values.h"
      9 #include "sync/internal_api/public/base/model_type.h"
     10 #include "sync/test/fake_server/fake_server.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 using std::string;
     14 using testing::AssertionFailure;
     15 using testing::AssertionResult;
     16 using testing::AssertionSuccess;
     17 
     18 namespace {
     19 
     20 AssertionResult DictionaryCreationAssertionFailure() {
     21   return AssertionFailure() << "FakeServer failed to create an entities "
     22                             << "dictionary.";
     23 }
     24 
     25 AssertionResult VerificationCountAssertionFailure(size_t actual_count,
     26                                                   size_t expected_count) {
     27   return AssertionFailure() << "Actual count: " << actual_count << "; "
     28                             << "Expected count: " << expected_count;
     29 }
     30 
     31 AssertionResult UnknownTypeAssertionFailure(const string& model_type) {
     32   return AssertionFailure() << "Verification not attempted. Unknown ModelType: "
     33                             << model_type;
     34 }
     35 
     36 }  // namespace
     37 
     38 namespace fake_server {
     39 
     40 FakeServerVerifier::FakeServerVerifier(FakeServer* fake_server)
     41     : fake_server_(fake_server) { }
     42 
     43 FakeServerVerifier::~FakeServerVerifier() {}
     44 
     45 AssertionResult FakeServerVerifier::VerifyEntityCountByType(
     46     size_t expected_count,
     47     syncer::ModelType model_type) const {
     48   scoped_ptr<base::DictionaryValue> entities =
     49       fake_server_->GetEntitiesAsDictionaryValue();
     50   if (!entities.get()) {
     51     return DictionaryCreationAssertionFailure();
     52   }
     53 
     54   string model_type_string = ModelTypeToString(model_type);
     55   base::ListValue* entity_list = NULL;
     56   if (!entities->GetList(model_type_string, &entity_list)) {
     57     return UnknownTypeAssertionFailure(model_type_string);
     58   } else if  (expected_count != entity_list->GetSize()) {
     59     return VerificationCountAssertionFailure(entity_list->GetSize(),
     60                                              expected_count);
     61   }
     62 
     63   return AssertionSuccess();
     64 }
     65 
     66 AssertionResult FakeServerVerifier::VerifyEntityCountByTypeAndName(
     67     size_t expected_count,
     68     syncer::ModelType model_type,
     69     const string& name) const {
     70   scoped_ptr<base::DictionaryValue> entities =
     71       fake_server_->GetEntitiesAsDictionaryValue();
     72   if (!entities.get()) {
     73     return DictionaryCreationAssertionFailure();
     74   }
     75 
     76   string model_type_string = ModelTypeToString(model_type);
     77   base::ListValue* entity_list = NULL;
     78   size_t actual_count = 0;
     79   if (entities->GetList(model_type_string, &entity_list)) {
     80     scoped_ptr<base::Value> name_value(new base::StringValue(name));
     81     for (base::ListValue::const_iterator it = entity_list->begin();
     82          it != entity_list->end(); ++it) {
     83       if (name_value->Equals(*it)) {
     84         actual_count++;
     85       }
     86     }
     87   }
     88 
     89   if (!entity_list) {
     90     return UnknownTypeAssertionFailure(model_type_string);
     91   } else if (actual_count != expected_count) {
     92     return VerificationCountAssertionFailure(actual_count, expected_count)
     93         << "; Name: " << name;
     94   }
     95 
     96   return AssertionSuccess();
     97 }
     98 
     99 }  // namespace fake_server
    100