1 // Copyright 2015 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.
5 #include "chrome/browser/task_management/providers/browser_process_task_provider.h"
6 #include "chrome/browser/task_management/task_manager_observer.h"
7 #include "chrome/grit/generated_resources.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/base/l10n/l10n_util.h"
11 namespace task_management
{
13 class BrowserProcessTaskProviderTest
14 : public testing::Test
,
15 public TaskProviderObserver
{
17 BrowserProcessTaskProviderTest()
18 : provided_task_(nullptr) {
21 ~BrowserProcessTaskProviderTest() override
{}
23 // task_management::TaskProviderObserver:
24 void TaskAdded(Task
* task
) override
{
25 provided_task_
= task
;
27 void TaskRemoved(Task
* task
) override
{
28 // This will never be called in the case of a browser process task provider.
36 DISALLOW_COPY_AND_ASSIGN(BrowserProcessTaskProviderTest
);
39 // Tests the browser process task provider and browser process task itself.
40 TEST_F(BrowserProcessTaskProviderTest
, TestObserving
) {
41 BrowserProcessTaskProvider provider
;
42 EXPECT_EQ(nullptr, provided_task_
);
43 provider
.SetObserver(this);
44 EXPECT_NE(nullptr, provided_task_
);
45 provider
.ClearObserver();
46 EXPECT_NE(nullptr, provided_task_
);
49 // Testing retrieving the task from the provider using the ids of a URL request.
50 TEST_F(BrowserProcessTaskProviderTest
, GetTaskOfUrlRequest
) {
51 BrowserProcessTaskProvider provider
;
52 EXPECT_EQ(nullptr, provided_task_
);
53 provider
.SetObserver(this);
54 EXPECT_NE(nullptr, provided_task_
);
56 Task
* result
= provider
.GetTaskOfUrlRequest(1, 0, 0);
57 EXPECT_EQ(nullptr, result
);
58 result
= provider
.GetTaskOfUrlRequest(0, -1, 0);
59 EXPECT_EQ(provided_task_
, result
);
62 // Test the provided browser process task itself.
63 TEST_F(BrowserProcessTaskProviderTest
, TestProvidedTask
) {
64 BrowserProcessTaskProvider provider
;
65 EXPECT_EQ(nullptr, provided_task_
);
66 provider
.SetObserver(this);
67 ASSERT_NE(nullptr, provided_task_
);
69 EXPECT_EQ(base::GetCurrentProcessHandle(), provided_task_
->process_handle());
70 EXPECT_EQ(base::GetCurrentProcId(), provided_task_
->process_id());
71 EXPECT_FALSE(provided_task_
->ReportsWebCacheStats());
72 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_WEB_BROWSER_CELL_TEXT
),
73 provided_task_
->title());
74 EXPECT_EQ(Task::BROWSER
, provided_task_
->GetType());
75 EXPECT_EQ(0, provided_task_
->GetChildProcessUniqueID());
76 const int received_bytes
= 1024;
77 EXPECT_FALSE(provided_task_
->ReportsNetworkUsage());
78 EXPECT_EQ(-1, provided_task_
->network_usage());
79 provided_task_
->OnNetworkBytesRead(received_bytes
);
80 // Do a refresh with a 1-second update time.
81 provided_task_
->Refresh(base::TimeDelta::FromSeconds(1),
82 REFRESH_TYPE_NETWORK_USAGE
);
83 EXPECT_TRUE(provided_task_
->ReportsNetworkUsage());
84 EXPECT_EQ(received_bytes
, provided_task_
->network_usage());
87 } // namespace task_management