Home | History | Annotate | Download | only in invalidation
      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 "components/invalidation/invalidation_notifier.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "components/invalidation/fake_invalidation_handler.h"
     10 #include "components/invalidation/fake_invalidation_state_tracker.h"
     11 #include "components/invalidation/invalidation_state_tracker.h"
     12 #include "components/invalidation/invalidator_test_template.h"
     13 #include "components/invalidation/push_client_channel.h"
     14 #include "jingle/notifier/base/fake_base_task.h"
     15 #include "jingle/notifier/base/notifier_options.h"
     16 #include "jingle/notifier/listener/fake_push_client.h"
     17 #include "net/url_request/url_request_test_util.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 
     20 namespace syncer {
     21 
     22 namespace {
     23 
     24 class InvalidationNotifierTestDelegate {
     25  public:
     26   InvalidationNotifierTestDelegate() {}
     27 
     28   ~InvalidationNotifierTestDelegate() {
     29     DestroyInvalidator();
     30   }
     31 
     32   void CreateInvalidator(
     33       const std::string& invalidator_client_id,
     34       const std::string& initial_state,
     35       const base::WeakPtr<InvalidationStateTracker>&
     36           invalidation_state_tracker) {
     37     DCHECK(!invalidator_.get());
     38     scoped_ptr<notifier::PushClient> push_client(
     39         new notifier::FakePushClient());
     40     scoped_ptr<SyncNetworkChannel> network_channel(
     41         new PushClientChannel(push_client.Pass()));
     42     invalidator_.reset(
     43         new InvalidationNotifier(network_channel.Pass(),
     44                                  invalidator_client_id,
     45                                  UnackedInvalidationsMap(),
     46                                  initial_state,
     47                                  invalidation_state_tracker,
     48                                  base::MessageLoopProxy::current(),
     49                                  "fake_client_info"));
     50   }
     51 
     52   Invalidator* GetInvalidator() {
     53     return invalidator_.get();
     54   }
     55 
     56   void DestroyInvalidator() {
     57     // Stopping the invalidation notifier stops its scheduler, which deletes
     58     // any pending tasks without running them.  Some tasks "run and delete"
     59     // another task, so they must be run in order to avoid leaking the inner
     60     // task.  Stopping does not schedule any tasks, so it's both necessary and
     61     // sufficient to drain the task queue before stopping the notifier.
     62     message_loop_.RunUntilIdle();
     63     invalidator_.reset();
     64   }
     65 
     66   void WaitForInvalidator() {
     67     message_loop_.RunUntilIdle();
     68   }
     69 
     70   void TriggerOnInvalidatorStateChange(InvalidatorState state) {
     71     invalidator_->OnInvalidatorStateChange(state);
     72   }
     73 
     74   void TriggerOnIncomingInvalidation(
     75       const ObjectIdInvalidationMap& invalidation_map) {
     76     invalidator_->OnInvalidate(invalidation_map);
     77   }
     78 
     79  private:
     80   base::MessageLoop message_loop_;
     81   scoped_ptr<InvalidationNotifier> invalidator_;
     82 };
     83 
     84 INSTANTIATE_TYPED_TEST_CASE_P(
     85     InvalidationNotifierTest, InvalidatorTest,
     86     InvalidationNotifierTestDelegate);
     87 
     88 }  // namespace
     89 
     90 }  // namespace syncer
     91