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/chromeos/extensions/file_manager/job_event_router.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/run_loop.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace file_manager
{
14 class JobEventRouterImpl
: public JobEventRouter
{
16 JobEventRouterImpl() : JobEventRouter(base::TimeDelta::FromMilliseconds(0)) {}
17 std::vector
<linked_ptr
<base::DictionaryValue
>> events
;
20 GURL
ConvertDrivePathToFileSystemUrl(const base::FilePath
& path
,
21 const std::string
& id
) const override
{
25 void BroadcastEvent(extensions::events::HistogramValue histogram_value
,
26 const std::string
& event_name
,
27 scoped_ptr
<base::ListValue
> event_args
) override
{
28 ASSERT_EQ(1u, event_args
->GetSize());
29 const base::DictionaryValue
* event
;
30 event_args
->GetDictionary(0, &event
);
31 events
.push_back(make_linked_ptr(event
->DeepCopy()));
35 DISALLOW_COPY_AND_ASSIGN(JobEventRouterImpl
);
38 class JobEventRouterTest
: public testing::Test
{
40 void SetUp() override
{ job_event_router
.reset(new JobEventRouterImpl()); }
42 drive::JobInfo
CreateJobInfo(drive::JobID id
,
43 int64 num_completed_bytes
,
44 int64 num_total_bytes
) {
45 drive::JobInfo
job(drive::TYPE_DOWNLOAD_FILE
);
47 job
.num_total_bytes
= num_total_bytes
;
48 job
.num_completed_bytes
= num_completed_bytes
;
52 std::string
GetEventString(size_t index
, const std::string
& name
) {
54 job_event_router
->events
[index
]->GetString(name
, &value
);
58 double GetEventDouble(size_t index
, const std::string
& name
) {
60 job_event_router
->events
[index
]->GetDouble(name
, &value
);
64 scoped_ptr
<JobEventRouterImpl
> job_event_router
;
67 base::MessageLoop message_loop_
;
70 TEST_F(JobEventRouterTest
, Basic
) {
72 job_event_router
->OnJobAdded(CreateJobInfo(0, 0, 100));
73 // Event should be throttled.
74 ASSERT_EQ(0u, job_event_router
->events
.size());
75 base::RunLoop().RunUntilIdle();
76 ASSERT_EQ(1u, job_event_router
->events
.size());
77 EXPECT_EQ("in_progress", GetEventString(0, "transferState"));
78 EXPECT_EQ(0.0f
, GetEventDouble(0, "processed"));
79 EXPECT_EQ(100.0f
, GetEventDouble(0, "total"));
80 job_event_router
->events
.clear();
83 job_event_router
->OnJobUpdated(CreateJobInfo(0, 50, 100));
84 job_event_router
->OnJobUpdated(CreateJobInfo(0, 100, 100));
85 // Event should be throttled.
86 ASSERT_EQ(0u, job_event_router
->events
.size());
87 base::RunLoop().RunUntilIdle();
88 ASSERT_EQ(1u, job_event_router
->events
.size());
89 EXPECT_EQ("in_progress", GetEventString(0, "transferState"));
90 EXPECT_EQ(100.0f
, GetEventDouble(0, "processed"));
91 EXPECT_EQ(100.0f
, GetEventDouble(0, "total"));
92 job_event_router
->events
.clear();
94 // Complete first job.
95 job_event_router
->OnJobDone(CreateJobInfo(0, 100, 100), drive::FILE_ERROR_OK
);
96 // Complete event should not be throttled.
97 ASSERT_EQ(1u, job_event_router
->events
.size());
98 EXPECT_EQ("completed", GetEventString(0, "transferState"));
99 EXPECT_EQ(100.0f
, GetEventDouble(0, "processed"));
100 EXPECT_EQ(100.0f
, GetEventDouble(0, "total"));
101 job_event_router
->events
.clear();
104 TEST_F(JobEventRouterTest
, CompleteWithInvalidCompletedBytes
) {
105 job_event_router
->OnJobDone(CreateJobInfo(0, 50, 100), drive::FILE_ERROR_OK
);
106 ASSERT_EQ(1u, job_event_router
->events
.size());
107 EXPECT_EQ("completed", GetEventString(0, "transferState"));
108 EXPECT_EQ(100.0f
, GetEventDouble(0, "processed"));
109 EXPECT_EQ(100.0f
, GetEventDouble(0, "total"));
112 TEST_F(JobEventRouterTest
, AnotherJobAddedBeforeComplete
) {
113 job_event_router
->OnJobAdded(CreateJobInfo(0, 0, 100));
114 job_event_router
->OnJobUpdated(CreateJobInfo(0, 50, 100));
115 job_event_router
->OnJobAdded(CreateJobInfo(1, 0, 100));
117 // Event should be throttled.
118 ASSERT_EQ(0u, job_event_router
->events
.size());
119 base::RunLoop().RunUntilIdle();
120 ASSERT_EQ(1u, job_event_router
->events
.size());
121 EXPECT_EQ("in_progress", GetEventString(0, "transferState"));
122 EXPECT_EQ(50.0f
, GetEventDouble(0, "processed"));
123 EXPECT_EQ(200.0f
, GetEventDouble(0, "total"));
124 job_event_router
->events
.clear();
126 job_event_router
->OnJobDone(CreateJobInfo(0, 100, 100), drive::FILE_ERROR_OK
);
127 job_event_router
->OnJobDone(CreateJobInfo(1, 100, 100), drive::FILE_ERROR_OK
);
128 // Complete event should not be throttled.
129 ASSERT_EQ(2u, job_event_router
->events
.size());
130 EXPECT_EQ("completed", GetEventString(0, "transferState"));
131 EXPECT_EQ(100.0f
, GetEventDouble(0, "processed"));
132 EXPECT_EQ(200.0f
, GetEventDouble(0, "total"));
133 EXPECT_EQ("completed", GetEventString(1, "transferState"));
134 EXPECT_EQ(200.0f
, GetEventDouble(1, "processed"));
135 EXPECT_EQ(200.0f
, GetEventDouble(1, "total"));
138 TEST_F(JobEventRouterTest
, AnotherJobAddedAfterComplete
) {
139 job_event_router
->OnJobAdded(CreateJobInfo(0, 0, 100));
140 job_event_router
->OnJobUpdated(CreateJobInfo(0, 50, 100));
141 job_event_router
->OnJobDone(CreateJobInfo(0, 100, 100), drive::FILE_ERROR_OK
);
142 job_event_router
->OnJobAdded(CreateJobInfo(1, 0, 100));
143 job_event_router
->OnJobDone(CreateJobInfo(1, 100, 100), drive::FILE_ERROR_OK
);
145 // Complete event should not be throttled.
146 ASSERT_EQ(2u, job_event_router
->events
.size());
147 EXPECT_EQ("completed", GetEventString(0, "transferState"));
148 EXPECT_EQ(100.0f
, GetEventDouble(0, "processed"));
149 // Total byte shold be reset when all tasks complete.
150 EXPECT_EQ(100.0f
, GetEventDouble(0, "total"));
151 EXPECT_EQ("completed", GetEventString(1, "transferState"));
152 EXPECT_EQ(100.0f
, GetEventDouble(1, "processed"));
153 EXPECT_EQ(100.0f
, GetEventDouble(1, "total"));
156 TEST_F(JobEventRouterTest
, UpdateTotalSizeAfterAdded
) {
157 job_event_router
->OnJobAdded(CreateJobInfo(0, 0, 0));
158 base::RunLoop().RunUntilIdle();
159 job_event_router
->OnJobUpdated(CreateJobInfo(0, 0, 100));
160 base::RunLoop().RunUntilIdle();
162 ASSERT_EQ(2u, job_event_router
->events
.size());
164 EXPECT_EQ("in_progress", GetEventString(0, "transferState"));
165 EXPECT_EQ(0.0f
, GetEventDouble(0, "processed"));
166 EXPECT_EQ(0.0f
, GetEventDouble(0, "total"));
168 EXPECT_EQ("in_progress", GetEventString(1, "transferState"));
169 EXPECT_EQ(0.0f
, GetEventDouble(1, "processed"));
170 EXPECT_EQ(100.0f
, GetEventDouble(1, "total"));
174 } // namespace file_manager