summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chrome_thread_unittest.cc
blob: f322e6d7cbacac2656e9d3fa1d46370040ae2505 (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
// Copyright (c) 2009 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/chrome_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

class ChromeThreadTest : public testing::Test {
 public:
  void Release() {
    CHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
    loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask);
  }

 protected:
  virtual void SetUp() {
    file_thread_.reset(new ChromeThread(ChromeThread::FILE));
    io_thread_.reset(new ChromeThread(ChromeThread::IO));
    file_thread_->Start();
    io_thread_->Start();
  }

  virtual void TearDown() {
    file_thread_->Stop();
    io_thread_->Stop();
  }

  static void BasicFunction(MessageLoop* message_loop) {
    CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
    message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask);
  }

  class DummyTask : public Task {
   public:
    explicit DummyTask(bool* deleted) : deleted_(deleted) { }
    ~DummyTask() {
      *deleted_ = true;
    }

    void Run() {
      CHECK(false);
    }

   private:
    bool* deleted_;
  };

  class DeletedOnIO
      : public base::RefCountedThreadSafe<
            DeletedOnIO, ChromeThread::DeleteOnIOThread> {
   public:
    explicit DeletedOnIO(MessageLoop* message_loop)
        : message_loop_(message_loop) { }

    ~DeletedOnIO() {
      CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
      message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
    }

   private:
    MessageLoop* message_loop_;
  };

  class NeverDeleted
      : public base::RefCountedThreadSafe<
            NeverDeleted, ChromeThread::DeleteOnWebKitThread> {
   public:
    ~NeverDeleted() {
      CHECK(false);
    }
  };

 private:
  scoped_ptr<ChromeThread> file_thread_;
  scoped_ptr<ChromeThread> io_thread_;
  MessageLoop loop_;
};

TEST_F(ChromeThreadTest, PostTask) {
  ChromeThread::PostTask(
      ChromeThread::IO, FROM_HERE,
      NewRunnableFunction(&BasicFunction, MessageLoop::current()));
  MessageLoop::current()->Run();
}

TEST_F(ChromeThreadTest, Release) {
  ChromeThread::ReleaseSoon(ChromeThread::FILE, FROM_HERE, this);
  MessageLoop::current()->Run();
}

TEST_F(ChromeThreadTest, TaskToNonExistentThreadIsDeleted) {
  bool deleted = false;
  ChromeThread::PostTask(
      ChromeThread::WEBKIT, FROM_HERE,
      new DummyTask(&deleted));
  EXPECT_TRUE(deleted);
}

TEST_F(ChromeThreadTest, ReleasedOnCorrectThread) {
  {
    scoped_refptr<DeletedOnIO> test(new DeletedOnIO(MessageLoop::current()));
  }
  MessageLoop::current()->Run();
}

TEST_F(ChromeThreadTest, NotReleasedIfTargetThreadNonExistent) {
  scoped_refptr<NeverDeleted> test(new NeverDeleted());
}