1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/bind.h"
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/bind_helpers.h"
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/message_loop/message_loop_proxy.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/run_loop.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/single_thread_task_runner.h"
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "cc/base/unique_notifier.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace cc {
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace {
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class UniqueNotifierTest : public testing::Test {
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  UniqueNotifierTest() : notification_count_(0) {}
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void SetUp() OVERRIDE { ResetNotificationCount(); }
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void Notify() { ++notification_count_; }
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int NotificationCount() const { return notification_count_; }
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void ResetNotificationCount() { notification_count_ = 0; }
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) protected:
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int notification_count_;
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)TEST_F(UniqueNotifierTest, Schedule) {
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  {
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    UniqueNotifier notifier(
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        base::MessageLoopProxy::current().get(),
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        base::Bind(&UniqueNotifierTest::Notify, base::Unretained(this)));
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(0, NotificationCount());
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Basic schedule should result in a run.
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    notifier.Schedule();
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(1, NotificationCount());
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Multiple schedules should only result in one run.
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    for (int i = 0; i < 5; ++i)
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      notifier.Schedule();
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::RunLoop().RunUntilIdle();
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    EXPECT_EQ(2, NotificationCount());
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    notifier.Schedule();
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Notifier went out of scope, so we don't expect to get a notification.
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::RunLoop().RunUntilIdle();
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EXPECT_EQ(2, NotificationCount());
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace cc
63