File: browser_process_task_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (87 lines) | stat: -rw-r--r-- 3,188 bytes parent folder | download | duplicates (7)
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/raw_ptr.h"
#include "chrome/browser/task_manager/providers/browser_process_task_provider.h"
#include "chrome/browser/task_manager/task_manager_observer.h"
#include "chrome/grit/generated_resources.h"
#include "services/network/public/mojom/network_context.mojom-forward.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"

namespace task_manager {

class BrowserProcessTaskProviderTest
    : public testing::Test,
      public TaskProviderObserver {
 public:
  BrowserProcessTaskProviderTest()
      : provided_task_(nullptr) {
  }

  BrowserProcessTaskProviderTest(const BrowserProcessTaskProviderTest&) =
      delete;
  BrowserProcessTaskProviderTest& operator=(
      const BrowserProcessTaskProviderTest&) = delete;
  ~BrowserProcessTaskProviderTest() override = default;

  // task_manager::TaskProviderObserver:
  void TaskAdded(Task* task) override {
    provided_task_ = task;
  }
  void TaskRemoved(Task* task) override {
    // This will never be called in the case of a browser process task provider.
    FAIL();
  }

 protected:
  raw_ptr<Task> provided_task_;
};

// Tests the browser process task provider and browser process task itself.
TEST_F(BrowserProcessTaskProviderTest, TestObserving) {
  BrowserProcessTaskProvider provider;
  EXPECT_EQ(nullptr, provided_task_.get());
  provider.SetObserver(this);
  EXPECT_NE(nullptr, provided_task_);
  provider.ClearObserver();
  EXPECT_NE(nullptr, provided_task_);
}

// Testing retrieving the task from the provider using the ids of a URL request.
TEST_F(BrowserProcessTaskProviderTest, GetTaskOfUrlRequest) {
  BrowserProcessTaskProvider provider;
  EXPECT_EQ(nullptr, provided_task_.get());
  provider.SetObserver(this);
  EXPECT_NE(nullptr, provided_task_);

  Task* result = provider.GetTaskOfUrlRequest(2, 0);
  EXPECT_EQ(nullptr, result);
  result = provider.GetTaskOfUrlRequest(network::mojom::kBrowserProcessId, 0);
  EXPECT_EQ(provided_task_, result);
}

// Test the provided browser process task itself.
TEST_F(BrowserProcessTaskProviderTest, TestProvidedTask) {
  BrowserProcessTaskProvider provider;
  EXPECT_EQ(nullptr, provided_task_.get());
  provider.SetObserver(this);
  ASSERT_NE(nullptr, provided_task_);

  EXPECT_EQ(base::GetCurrentProcessHandle(), provided_task_->process_handle());
  EXPECT_EQ(base::GetCurrentProcId(), provided_task_->process_id());
  EXPECT_FALSE(provided_task_->ReportsWebCacheStats());
  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_WEB_BROWSER_CELL_TEXT),
            provided_task_->title());
  EXPECT_EQ(Task::BROWSER, provided_task_->GetType());
  EXPECT_EQ(0, provided_task_->GetChildProcessUniqueID());
  const int received_bytes = 1024;
  EXPECT_EQ(0, provided_task_->GetNetworkUsageRate());
  provided_task_->OnNetworkBytesRead(received_bytes);
  // Do a refresh with a 1-second update time.
  provided_task_->Refresh(base::Seconds(1), REFRESH_TYPE_NETWORK_USAGE);
  EXPECT_EQ(received_bytes, provided_task_->GetNetworkUsageRate());
}

}  // namespace task_manager