summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/notifier/chrome_sync_notification_bridge_unittest.cc
blob: ffc56f58439ae66dd2a2ec744bc211f26ea20de4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/notifier/chrome_sync_notification_bridge.h"

#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread.h"
#include "chrome/browser/sync/syncable/model_type.h"
#include "chrome/browser/sync/syncable/model_type_payload_map.h"
#include "chrome/browser/sync/notifier/mock_sync_notifier_observer.h"
#include "chrome/browser/sync/notifier/sync_notifier_observer.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/test/base/profile_mock.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_details.h"
#include "content/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace sync_notifier {
namespace {

using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::StrictMock;
using content::BrowserThread;

// Receives a ChromeSyncNotificationBridge to register to, and an expected
// ModelTypePayloadMap. ReceivedProperNotification() will return true only
// if the observer has received a notification with the proper source and
// payload.
// Note: Because this object lives on the IO thread, we use a fake (vs a mock)
// so we don't have to worry about possible thread safety issues within
// GTest/GMock.
class FakeSyncNotifierObserverIO : public SyncNotifierObserver {
 public:
  FakeSyncNotifierObserverIO(
      ChromeSyncNotificationBridge* bridge,
      const syncable::ModelTypePayloadMap& expected_payloads)
      : bridge_(bridge),
        received_improper_notification_(false),
        notification_count_(0),
        expected_payloads_(expected_payloads) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    bridge_->AddObserver(this);
  }
  virtual ~FakeSyncNotifierObserverIO() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    bridge_->RemoveObserver(this);
  }

  // SyncNotifierObserver implementation.
  virtual void OnIncomingNotification(
      const syncable::ModelTypePayloadMap& type_payloads,
      IncomingNotificationSource source) OVERRIDE {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    notification_count_++;
    if (source != LOCAL_NOTIFICATION) {
      LOG(ERROR) << "Received notification with wrong source.";
      received_improper_notification_ = true;
    }
    if (expected_payloads_ != type_payloads) {
      LOG(ERROR) << "Received wrong payload.";
      received_improper_notification_ = true;
    }
  }
  virtual void OnNotificationStateChange(bool notifications_enabled) {
    NOTREACHED();
  }
  virtual void StoreState(const std::string& state) OVERRIDE {
    NOTREACHED();
  }

  bool ReceivedProperNotification() const {
    return (notification_count_ == 1) && !received_improper_notification_;
  }

 private:
  ChromeSyncNotificationBridge* bridge_;
  bool received_improper_notification_;
  size_t notification_count_;
  syncable::ModelTypePayloadMap expected_payloads_;
};

class ChromeSyncNotificationBridgeTest : public testing::Test {
 public:
  ChromeSyncNotificationBridgeTest()
      : ui_thread_(BrowserThread::UI, &ui_loop_),
        io_thread_(BrowserThread::IO),
        io_observer_(NULL),
        io_observer_notification_failure_(false),
        bridge_(&mock_profile_),
        done_(true, false) {
    io_thread_.StartIOThread();
  }
  virtual ~ChromeSyncNotificationBridgeTest() {}

 protected:
  virtual void TearDown() OVERRIDE {
    io_thread_.Stop();
    ui_loop_.RunAllPending();
    EXPECT_EQ(NULL, io_observer_);
    if (io_observer_notification_failure_)
      ADD_FAILURE() << "IO Observer did not receive proper notification.";
  }

  void VerifyAndDestroyObserverOnIOThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    if (!io_observer_) {
      io_observer_notification_failure_ = true;
    } else {
      io_observer_notification_failure_ =
          !io_observer_->ReceivedProperNotification();
      delete io_observer_;
      io_observer_ = NULL;
    }
  }

  void VerifyAndDestroyObserver() {
    ASSERT_TRUE(BrowserThread::PostTask(
        BrowserThread::IO,
        FROM_HERE,
        base::Bind(&ChromeSyncNotificationBridgeTest::
                       VerifyAndDestroyObserverOnIOThread,
                   base::Unretained(this))));
  }

  void CreateObserverOnIOThread(
      syncable::ModelTypePayloadMap expected_payloads) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    io_observer_ = new FakeSyncNotifierObserverIO(&bridge_,
                                                  expected_payloads);
  }

  void CreateObserverWithExpectedPayload(
      syncable::ModelTypePayloadMap expected_payloads) {
    ASSERT_TRUE(BrowserThread::PostTask(
        BrowserThread::IO,
        FROM_HERE,
        base::Bind(&ChromeSyncNotificationBridgeTest::CreateObserverOnIOThread,
                   base::Unretained(this),
                   expected_payloads)));
    BlockForIOThread();
  }

  void SignalOnIOThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    done_.Signal();
  }

  void BlockForIOThread() {
    done_.Reset();
    BrowserThread::PostTask(
        BrowserThread::IO,
        FROM_HERE,
        base::Bind(&ChromeSyncNotificationBridgeTest::SignalOnIOThread,
                   base::Unretained(this)));
    done_.TimedWait(TestTimeouts::action_timeout());
    if (!done_.IsSignaled())
      ADD_FAILURE() << "Timed out waiting for IO thread.";
  }

  void TriggerRefreshNotification() {
    const syncable::ModelType type = syncable::SESSIONS;
    content::NotificationService::current()->Notify(
        chrome::NOTIFICATION_SYNC_REFRESH,
        content::Source<Profile>(&mock_profile_),
        content::Details<const syncable::ModelType>(&type));
  }

  MessageLoop ui_loop_;
  content::TestBrowserThread ui_thread_;
  content::TestBrowserThread io_thread_;
  NiceMock<ProfileMock> mock_profile_;
  // Created/used/destroyed on I/O thread.
  FakeSyncNotifierObserverIO* io_observer_;
  bool io_observer_notification_failure_;
  ChromeSyncNotificationBridge bridge_;
  base::WaitableEvent done_;
};

// Adds an observer on the UI thread, triggers a refresh notification, and
// ensures the bridge posts a LOCAL_NOTIFICATION with the proper payload to it.
TEST_F(ChromeSyncNotificationBridgeTest, Basic) {
  syncable::ModelTypePayloadMap payload_map;
  payload_map[syncable::SESSIONS] = "";
  StrictMock<MockSyncNotifierObserver> observer;
  EXPECT_CALL(observer,
              OnIncomingNotification(payload_map, LOCAL_NOTIFICATION));
  bridge_.AddObserver(&observer);
  TriggerRefreshNotification();
  ui_loop_.RunAllPending();
  Mock::VerifyAndClearExpectations(&observer);
}

// Adds an observer on the I/O thread. Then triggers a refresh notification on
// the UI thread. We finally verify the proper notification was received by the
// observer and destroy it on the I/O thread.
TEST_F(ChromeSyncNotificationBridgeTest, BasicThreaded) {
  syncable::ModelTypePayloadMap payload_map;
  payload_map[syncable::SESSIONS] = "";
  CreateObserverWithExpectedPayload(payload_map);
  TriggerRefreshNotification();
  VerifyAndDestroyObserver();
}

}  // namespace
}  // namespace sync_notifier