1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
23345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Use of this source code is governed by a BSD-style license that can be
33345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// found in the LICENSE file.
43345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
53345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/basictypes.h"
6ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/memory/scoped_ptr.h"
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/task.h"
83345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/task_queue.h"
93345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "testing/gtest/include/gtest/gtest.h"
103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merricknamespace {
123345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Sets bools according to whether Run or the destructor were called.
143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickclass TrackCallsTask : public Task {
153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick public:
163345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  TrackCallsTask(bool* ran, bool* deleted)
173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick      : ran_(ran),
183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick        deleted_(deleted) {
193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    *ran_ = false;
203345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    *deleted_ = false;
213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
223345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual ~TrackCallsTask() {
243345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    *deleted_ = true;
253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
263345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
273345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual void Run() {
283345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    *ran_ = true;
293345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
313345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick private:
323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool* ran_;
333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool* deleted_;
343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  DISALLOW_COPY_AND_ASSIGN(TrackCallsTask);
363345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick};
373345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
383345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Adds a given task to the queue when run.
393345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrickclass TaskQueuerTask : public Task {
403345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick public:
413345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  TaskQueuerTask(TaskQueue* queue, Task* task_to_queue)
423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick      : queue_(queue),
433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick        task_to_queue_(task_to_queue) {
443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
453345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  virtual void Run() {
473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick    queue_->Push(task_to_queue_);
483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  }
493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick private:
513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  TaskQueue* queue_;
523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  Task* task_to_queue_;
533345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
543345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  DISALLOW_COPY_AND_ASSIGN(TaskQueuerTask);
553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick};
563345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
573345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}  // namespace
583345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
593345a6884c488ff3a535c2c9acdd33d74b37e311Iain MerrickTEST(TaskQueueTest, RunNoTasks) {
603345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  TaskQueue queue;
613345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(queue.IsEmpty());
623345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
633345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Run();
643345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(queue.IsEmpty());
653345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}
663345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
673345a6884c488ff3a535c2c9acdd33d74b37e311Iain MerrickTEST(TaskQueueTest, RunTasks) {
683345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  TaskQueue queue;
693345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
703345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool ran_task1 = false;
713345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool deleted_task1 = false;
723345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Push(new TrackCallsTask(&ran_task1, &deleted_task1));
733345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
743345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool ran_task2 = false;
753345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool deleted_task2 = false;
763345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Push(new TrackCallsTask(&ran_task2, &deleted_task2));
773345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
783345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Run();
793345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
803345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(ran_task1);
813345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(deleted_task1);
823345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(ran_task2);
833345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(deleted_task2);
843345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(queue.IsEmpty());
853345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}
863345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
873345a6884c488ff3a535c2c9acdd33d74b37e311Iain MerrickTEST(TaskQueueTest, ClearTasks) {
883345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  TaskQueue queue;
893345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
903345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool ran_task1 = false;
913345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool deleted_task1 = false;
923345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Push(new TrackCallsTask(&ran_task1, &deleted_task1));
933345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
943345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool ran_task2 = false;
953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool deleted_task2 = false;
963345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Push(new TrackCallsTask(&ran_task2, &deleted_task2));
973345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
983345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Clear();
993345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1003345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(queue.IsEmpty());
1013345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1023345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  queue.Run();
1033345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1043345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_FALSE(ran_task1);
1053345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(deleted_task1);
1063345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_FALSE(ran_task2);
1073345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(deleted_task2);
1083345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(queue.IsEmpty());
1093345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}
1103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1113345a6884c488ff3a535c2c9acdd33d74b37e311Iain MerrickTEST(TaskQueueTest, OneTaskQueuesMore) {
1123345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  TaskQueue main_queue;
1133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Build a task which will queue two more when run.
1153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  scoped_ptr<TaskQueue> nested_queue(new TaskQueue());
1163345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool ran_task1 = false;
1173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool deleted_task1 = false;
1183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  nested_queue->Push(
1193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick      new TaskQueuerTask(&main_queue,
1203345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick                         new TrackCallsTask(&ran_task1, &deleted_task1)));
1213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool ran_task2 = false;
1223345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  bool deleted_task2 = false;
1233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  nested_queue->Push(
1243345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick      new TaskQueuerTask(&main_queue,
1253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick                         new TrackCallsTask(&ran_task2, &deleted_task2)));
1263345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1273345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  main_queue.Push(nested_queue.release());
1283345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1293345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Run the task which pushes two more tasks.
1303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  main_queue.Run();
1313345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1323345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // None of the pushed tasks shoudl have run yet.
1333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_FALSE(ran_task1);
1343345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_FALSE(deleted_task1);
1353345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_FALSE(ran_task2);
1363345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_FALSE(deleted_task2);
1373345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_FALSE(main_queue.IsEmpty());
1383345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1393345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  // Now run the nested tasks.
1403345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  main_queue.Run();
1413345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(ran_task1);
1433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(deleted_task1);
1443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(ran_task2);
1453345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(deleted_task2);
1463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick  EXPECT_TRUE(main_queue.IsEmpty());
1473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}
148