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

content / public / test / test_utils_unittest.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/test/test_utils.h"

#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/threading/platform_thread.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

// Regression test for crbug.com/1035189.
TEST(ContentTestUtils, NestedRunAllTasksUntilIdleWithPendingThreadPoolWork) {
  base::test::TaskEnvironment task_environment;

  bool thread_pool_task_completed = false;
  base::ThreadPool::PostTask(
      FROM_HERE, {}, base::BindLambdaForTesting([&]() {
        base::PlatformThread::Sleep(base::Milliseconds(100));
        thread_pool_task_completed = true;
      }));

  base::RunLoop run_loop;

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindLambdaForTesting([&]() {
        // Nested RunAllTasksUntilIdle() (i.e. crbug.com/1035189).
        content::RunAllTasksUntilIdle();
        EXPECT_TRUE(thread_pool_task_completed);
        run_loop.Quit();
      }));

  run_loop.Run();
  EXPECT_TRUE(thread_pool_task_completed);
}

// Regression test for crbug.com/1035604.
TEST(ContentTestUtils, FlushRealIOThread) {
  content::BrowserTaskEnvironment task_environment{
      content::BrowserTaskEnvironment::REAL_IO_THREAD};

  bool io_task_completed = false;
  content::GetIOThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindLambdaForTesting([&]() {
        base::PlatformThread::Sleep(base::Milliseconds(100));
        io_task_completed = true;
      }));

  content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
  EXPECT_TRUE(io_task_completed);
}

TEST(ContentTestUtils, NestedFlushRealIOThread) {
  content::BrowserTaskEnvironment task_environment{
      content::BrowserTaskEnvironment::REAL_IO_THREAD};

  bool io_task_completed = false;
  content::GetIOThreadTaskRunner({})->PostTask(
      FROM_HERE, base::BindLambdaForTesting([&]() {
        base::PlatformThread::Sleep(base::Milliseconds(100));
        io_task_completed = true;
      }));

  base::RunLoop run_loop;

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindLambdaForTesting([&]() {
        content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
        EXPECT_TRUE(io_task_completed);
        run_loop.Quit();
      }));

  run_loop.Run();
  EXPECT_TRUE(io_task_completed);
}

TEST(ContentTestUtils, FlushRealIOThreadWithPendingBestEffortTask) {
  content::BrowserTaskEnvironment task_environment{
      content::BrowserTaskEnvironment::REAL_IO_THREAD};

  bool io_task_completed = false;
  content::GetIOThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
      ->PostTask(FROM_HERE, base::BindLambdaForTesting([&]() {
                   base::PlatformThread::Sleep(base::Milliseconds(100));
                   io_task_completed = true;
                 }));

  content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
  EXPECT_TRUE(io_task_completed);
}

// Same as FlushRealIOThreadWithPendingBestEffortTask but when BrowserThread::IO
// is multiplexed with BrowserThread::UI on the main thread (i.e. the default
// under BrowserTaskEnvironment).
TEST(ContentTestUtils, FlushFakeIOThread) {
  content::BrowserTaskEnvironment task_environment;

  bool io_task_completed = false;
  content::GetIOThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
      ->PostTask(FROM_HERE, base::BindLambdaForTesting([&]() {
                   base::PlatformThread::Sleep(base::Milliseconds(100));
                   io_task_completed = true;
                 }));

  content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
  EXPECT_TRUE(io_task_completed);
}

TEST(ContentTestUtils, FlushUIThread) {
  content::BrowserTaskEnvironment task_environment;

  bool ui_task_completed = false;
  content::GetUIThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
      ->PostTask(FROM_HERE, base::BindLambdaForTesting([&]() {
                   base::PlatformThread::Sleep(base::Milliseconds(100));
                   ui_task_completed = true;
                 }));

  content::RunAllPendingInMessageLoop(content::BrowserThread::UI);
  EXPECT_TRUE(ui_task_completed);
}