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 "content/child/worker_task_runner.h"
6
7#include "base/logging.h"
8#include "testing/gmock/include/gmock/gmock.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace content {
12
13class WorkerTaskRunnerTest : public testing::Test {
14 public:
15  void FakeStart() {
16    task_runner_.OnWorkerRunLoopStarted(blink::WebWorkerRunLoop());
17  }
18  void FakeStop() {
19    task_runner_.OnWorkerRunLoopStopped(blink::WebWorkerRunLoop());
20  }
21  WorkerTaskRunner task_runner_;
22};
23
24class MockObserver : public WorkerTaskRunner::Observer {
25 public:
26  MOCK_METHOD0(OnWorkerRunLoopStopped, void());
27  void RemoveSelfOnNotify() {
28    ON_CALL(*this, OnWorkerRunLoopStopped()).WillByDefault(
29        testing::Invoke(this, &MockObserver::RemoveSelf));
30  }
31  void RemoveSelf() {
32    runner_->RemoveStopObserver(this);
33  }
34  WorkerTaskRunner* runner_;
35};
36
37TEST_F(WorkerTaskRunnerTest, BasicObservingAndWorkerId) {
38  ASSERT_EQ(0, task_runner_.CurrentWorkerId());
39  MockObserver o;
40  EXPECT_CALL(o, OnWorkerRunLoopStopped()).Times(1);
41  FakeStart();
42  task_runner_.AddStopObserver(&o);
43  ASSERT_LT(0, task_runner_.CurrentWorkerId());
44  FakeStop();
45}
46
47TEST_F(WorkerTaskRunnerTest, CanRemoveSelfDuringNotification) {
48  MockObserver o;
49  o.RemoveSelfOnNotify();
50  o.runner_ = &task_runner_;
51  EXPECT_CALL(o, OnWorkerRunLoopStopped()).Times(1);
52  FakeStart();
53  task_runner_.AddStopObserver(&o);
54  FakeStop();
55}
56
57}  // namespace content
58