Get foreground tab on Android
[chromium-blink-merge.git] / sync / notifier / non_blocking_invalidator_unittest.cc
blobe9cf31e121f83b2f83050bdac2859651bbeaaf98
1 // Copyright (c) 2012 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.
5 #include "sync/notifier/non_blocking_invalidator.h"
7 #include "base/bind_helpers.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/threading/thread.h"
13 #include "google/cacheinvalidation/types.pb.h"
14 #include "jingle/notifier/base/fake_base_task.h"
15 #include "net/url_request/url_request_test_util.h"
16 #include "sync/internal_api/public/util/weak_handle.h"
17 #include "sync/notifier/fake_invalidation_handler.h"
18 #include "sync/notifier/invalidation_state_tracker.h"
19 #include "sync/notifier/invalidator_test_template.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace syncer {
24 namespace {
26 class NonBlockingInvalidatorTestDelegate {
27 public:
28 NonBlockingInvalidatorTestDelegate() : io_thread_("IO thread") {}
30 ~NonBlockingInvalidatorTestDelegate() {
31 DestroyInvalidator();
34 void CreateInvalidator(
35 const std::string& invalidator_client_id,
36 const std::string& initial_state,
37 const base::WeakPtr<InvalidationStateTracker>&
38 invalidation_state_tracker) {
39 DCHECK(!invalidator_.get());
40 base::Thread::Options options;
41 options.message_loop_type = base::MessageLoop::TYPE_IO;
42 io_thread_.StartWithOptions(options);
43 request_context_getter_ =
44 new net::TestURLRequestContextGetter(io_thread_.message_loop_proxy());
45 notifier::NotifierOptions invalidator_options;
46 invalidator_options.request_context_getter = request_context_getter_;
47 invalidator_.reset(
48 new NonBlockingInvalidator(
49 invalidator_options,
50 invalidator_client_id,
51 InvalidationStateMap(),
52 initial_state,
53 MakeWeakHandle(invalidation_state_tracker),
54 "fake_client_info"));
57 Invalidator* GetInvalidator() {
58 return invalidator_.get();
61 void DestroyInvalidator() {
62 invalidator_.reset();
63 request_context_getter_ = NULL;
64 io_thread_.Stop();
65 message_loop_.RunUntilIdle();
68 void WaitForInvalidator() {
69 base::RunLoop run_loop;
70 ASSERT_TRUE(
71 io_thread_.message_loop_proxy()->PostTaskAndReply(
72 FROM_HERE,
73 base::Bind(&base::DoNothing),
74 run_loop.QuitClosure()));
75 run_loop.Run();
78 void TriggerOnInvalidatorStateChange(InvalidatorState state) {
79 invalidator_->OnInvalidatorStateChange(state);
82 void TriggerOnIncomingInvalidation(
83 const ObjectIdInvalidationMap& invalidation_map) {
84 invalidator_->OnIncomingInvalidation(invalidation_map);
87 private:
88 base::MessageLoop message_loop_;
89 base::Thread io_thread_;
90 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
91 scoped_ptr<NonBlockingInvalidator> invalidator_;
94 INSTANTIATE_TYPED_TEST_CASE_P(
95 NonBlockingInvalidatorTest, InvalidatorTest,
96 NonBlockingInvalidatorTestDelegate);
98 } // namespace
100 } // namespace syncer