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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
ash / public / cpp / holding_space / holding_space_item_unittest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/holding_space/holding_space_item.h"
#include <memory>
#include <vector>
#include "ash/public/cpp/holding_space/holding_space_file.h"
#include "ash/public/cpp/holding_space/holding_space_image.h"
#include "ash/public/cpp/holding_space/holding_space_progress.h"
#include "ash/public/cpp/holding_space/holding_space_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/test/bind.h"
#include "base/test/scoped_locale.h"
#include "base/values.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/paint_vector_icon.h"
namespace ash {
namespace {
// Aliases
using testing::AllOf;
using testing::Eq;
using testing::Property;
using testing::VariantWith;
std::unique_ptr<HoldingSpaceImage> CreateFakeHoldingSpaceImage(
HoldingSpaceItem::Type type,
const base::FilePath& file_path) {
return std::make_unique<HoldingSpaceImage>(
holding_space_util::GetMaxImageSizeForType(type), file_path,
/*async_bitmap_resolver=*/base::DoNothing());
}
} // namespace
using HoldingSpaceItemTest = testing::TestWithParam<HoldingSpaceItem::Type>;
// Tests round-trip serialization for each holding space item type.
TEST_P(HoldingSpaceItemTest, Serialization) {
const HoldingSpaceFile file(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem:file_system_url"));
const auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(), file,
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
const base::Value::Dict serialized_holding_space_item =
holding_space_item->Serialize();
const auto deserialized_holding_space_item = HoldingSpaceItem::Deserialize(
serialized_holding_space_item,
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
EXPECT_FALSE(deserialized_holding_space_item->IsInitialized());
EXPECT_TRUE(
deserialized_holding_space_item->file().file_system_url.is_empty());
EXPECT_EQ(deserialized_holding_space_item->file().file_system_type,
HoldingSpaceFile::FileSystemType::kUnknown);
deserialized_holding_space_item->Initialize(file);
EXPECT_TRUE(deserialized_holding_space_item->IsInitialized());
EXPECT_EQ(*deserialized_holding_space_item, *holding_space_item);
}
// Tests deserialization of id for each holding space item type.
TEST_P(HoldingSpaceItemTest, DeserializeId) {
const auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem:file_system_url")),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
const base::Value::Dict serialized_holding_space_item =
holding_space_item->Serialize();
const std::string& deserialized_holding_space_id =
HoldingSpaceItem::DeserializeId(serialized_holding_space_item);
EXPECT_EQ(deserialized_holding_space_id, holding_space_item->id());
}
// Tests setting the accessible name for each holding space item type.
TEST_P(HoldingSpaceItemTest, AccessibleName) {
// Force locale since strings are being verified.
base::ScopedLocale scoped_locale("en_US.UTF-8");
// Create a `holding_space_item`.
auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
// Initially the accessible name should be based on the backing file.
EXPECT_EQ(holding_space_item->GetAccessibleName(), u"file_path");
// If primary text is set, that should affect accessible name.
EXPECT_TRUE(holding_space_item->SetText(u"Primary text"));
EXPECT_EQ(holding_space_item->GetAccessibleName(), u"Primary text");
// If secondary text is set, that should affect accessible name.
EXPECT_TRUE(holding_space_item->SetSecondaryText(u"Secondary text"));
EXPECT_EQ(holding_space_item->GetAccessibleName(),
u"Primary text, Secondary text");
// It should be possible to override accessible name.
EXPECT_TRUE(holding_space_item->SetAccessibleName(u"Accessible name"));
EXPECT_EQ(holding_space_item->GetAccessibleName(), u"Accessible name");
// It should no-op to try to override accessible name w/ existing values.
EXPECT_FALSE(holding_space_item->SetAccessibleName(u"Accessible name"));
EXPECT_EQ(holding_space_item->GetAccessibleName(), u"Accessible name");
// It should be possible to remove the accessible name override.
EXPECT_TRUE(holding_space_item->SetAccessibleName(std::nullopt));
EXPECT_EQ(holding_space_item->GetAccessibleName(),
u"Primary text, Secondary text");
}
// Tests in-progress commands for each holding space item type.
TEST_P(HoldingSpaceItemTest, InProgressCommands) {
// Create an in-progress `holding_space_item`.
auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
HoldingSpaceProgress(/*current_bytes=*/50, /*total_bytes=*/100),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
// Initially commands are not set.
EXPECT_TRUE(holding_space_item->in_progress_commands().empty());
// It should be possible to update commands to a new value.
std::vector<HoldingSpaceItem::InProgressCommand> in_progress_commands;
in_progress_commands.push_back(HoldingSpaceItem::InProgressCommand(
HoldingSpaceCommandId::kCancelItem, /*label_id=*/-1, &gfx::kNoneIcon,
/*handler=*/base::DoNothing()));
EXPECT_TRUE(holding_space_item->SetInProgressCommands(in_progress_commands));
EXPECT_EQ(holding_space_item->in_progress_commands(), in_progress_commands);
// It should no-op to try to update pause to its existing value.
EXPECT_FALSE(holding_space_item->SetInProgressCommands(in_progress_commands));
EXPECT_EQ(holding_space_item->in_progress_commands(), in_progress_commands);
// Once progress has been marked completed, commands are not set.
EXPECT_TRUE(holding_space_item->SetProgress(
HoldingSpaceProgress(/*current_bytes=*/100, /*total_bytes=*/100)));
EXPECT_TRUE(holding_space_item->progress().IsComplete());
EXPECT_TRUE(holding_space_item->in_progress_commands().empty());
// It should no-op to try to update commands for items which are not
// in-progress.
EXPECT_FALSE(holding_space_item->SetInProgressCommands(in_progress_commands));
EXPECT_TRUE(holding_space_item->in_progress_commands().empty());
}
// Tests identification of screen capture holding space item types.
TEST_P(HoldingSpaceItemTest, IsScreenCapture) {
const HoldingSpaceItem::Type type = GetParam();
switch (type) {
case HoldingSpaceItem::Type::kScreenRecording:
case HoldingSpaceItem::Type::kScreenRecordingGif:
case HoldingSpaceItem::Type::kScreenshot:
EXPECT_TRUE(HoldingSpaceItem::IsScreenCaptureType(type));
return;
case HoldingSpaceItem::Type::kArcDownload:
case HoldingSpaceItem::Type::kDiagnosticsLog:
case HoldingSpaceItem::Type::kDownload:
case HoldingSpaceItem::Type::kDriveSuggestion:
case HoldingSpaceItem::Type::kLocalSuggestion:
case HoldingSpaceItem::Type::kNearbyShare:
case HoldingSpaceItem::Type::kPhoneHubCameraRoll:
case HoldingSpaceItem::Type::kPhotoshopWeb:
case HoldingSpaceItem::Type::kPinnedFile:
case HoldingSpaceItem::Type::kPrintedPdf:
case HoldingSpaceItem::Type::kScan:
EXPECT_FALSE(HoldingSpaceItem::IsScreenCaptureType(type));
return;
}
}
// Tests progress for each holding space item type.
TEST_P(HoldingSpaceItemTest, Progress) {
// Create a `holding_space_item` w/ explicitly specified progress.
auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
HoldingSpaceProgress(/*current_bytes=*/50, /*total_bytes=*/100),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
// Since explicitly specified during construction, progress should be `0.5f`.
EXPECT_EQ(holding_space_item->progress().GetValue(), 0.5f);
// It should be possible to update progress to a new value.
EXPECT_TRUE(holding_space_item->SetProgress(
HoldingSpaceProgress(/*current_bytes=*/75, /*total_bytes=*/100)));
EXPECT_EQ(holding_space_item->progress().GetValue(), 0.75f);
// It should no-op to try to update progress to its existing value.
EXPECT_FALSE(holding_space_item->SetProgress(
HoldingSpaceProgress(/*current_bytes=*/75, /*total_bytes=*/100)));
EXPECT_EQ(holding_space_item->progress().GetValue(), 0.75f);
// It should be possible to set indeterminate progress.
EXPECT_TRUE(holding_space_item->SetProgress(HoldingSpaceProgress(
/*current_bytes=*/std::nullopt, /*total_bytes=*/100)));
EXPECT_TRUE(holding_space_item->progress().IsIndeterminate());
// It should be possible to set progress complete.
EXPECT_TRUE(holding_space_item->SetProgress(
HoldingSpaceProgress(/*current_bytes=*/100, /*total_bytes=*/100)));
EXPECT_TRUE(holding_space_item->progress().IsComplete());
// Once progress has been marked completed, it should become read-only.
EXPECT_FALSE(holding_space_item->SetProgress(
HoldingSpaceProgress(/*current_bytes=*/75, /*total_bytes=*/100)));
EXPECT_TRUE(holding_space_item->progress().IsComplete());
// Create a `holding_space_item` w/ default progress.
holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
// Since not specified during construction, progress should be complete.
EXPECT_TRUE(holding_space_item->progress().IsComplete());
// Since progress is marked completed, it should be read-only.
EXPECT_FALSE(holding_space_item->SetProgress(
HoldingSpaceProgress(/*current_bytes=*/75, /*total_bytes=*/100)));
EXPECT_TRUE(holding_space_item->progress().IsComplete());
}
// Tests setting the secondary text for each holding space item type.
TEST_P(HoldingSpaceItemTest, SecondaryText) {
// Create a `holding_space_item`.
auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
// Initially the secondary text should be absent.
EXPECT_FALSE(holding_space_item->secondary_text());
// It should be possible to update secondary text to a new value.
EXPECT_TRUE(holding_space_item->SetSecondaryText(u"secondary_text"));
EXPECT_EQ(holding_space_item->secondary_text().value(), u"secondary_text");
// It should no-op to try to update secondary text to its existing value.
EXPECT_FALSE(holding_space_item->SetSecondaryText(u"secondary_text"));
EXPECT_EQ(holding_space_item->secondary_text().value(), u"secondary_text");
// It should be possible to unset secondary text.
EXPECT_TRUE(holding_space_item->SetSecondaryText(std::nullopt));
EXPECT_FALSE(holding_space_item->secondary_text());
}
// Tests setting the secondary text color for each holding space item type.
TEST_P(HoldingSpaceItemTest, SecondaryTextColor) {
// Create a `holding_space_item`.
auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
// Initially the secondary text color variant should be absent.
EXPECT_FALSE(holding_space_item->secondary_text_color_variant());
// It should be possible to update secondary text color to a new color id.
EXPECT_TRUE(holding_space_item->SetSecondaryTextColorVariant(
cros_tokens::kTextColorAlert));
EXPECT_THAT(holding_space_item->secondary_text_color_variant().value(),
VariantWith<ui::ColorId>(cros_tokens::kTextColorAlert));
// It should no-op to try to update secondary text color to existing values.
EXPECT_FALSE(holding_space_item->SetSecondaryTextColorVariant(
cros_tokens::kTextColorAlert));
EXPECT_THAT(holding_space_item->secondary_text_color_variant().value(),
VariantWith<ui::ColorId>(cros_tokens::kTextColorAlert));
// It should be possible to update secondary text color to a new
// `HoldingSpaceColors` instance. NOTE: Use a light/dark text color for
// dark/light modes to improve readability.
EXPECT_TRUE(holding_space_item->SetSecondaryTextColorVariant(
HoldingSpaceColors(/*dark_mode=*/SK_ColorWHITE,
/*light_mode=*/SK_ColorBLACK)));
EXPECT_THAT(
holding_space_item->secondary_text_color_variant().value(),
VariantWith<HoldingSpaceColors>(
AllOf(Property(&HoldingSpaceColors::dark_mode, Eq(SK_ColorWHITE)),
Property(&HoldingSpaceColors::light_mode, Eq(SK_ColorBLACK)))));
// It should be possible to unset secondary text color.
EXPECT_TRUE(holding_space_item->SetSecondaryTextColorVariant(std::nullopt));
EXPECT_FALSE(holding_space_item->secondary_text_color_variant());
}
// Tests setting the text for each holding space item type.
TEST_P(HoldingSpaceItemTest, Text) {
// Create a `holding_space_item`.
auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
/*type=*/GetParam(),
HoldingSpaceFile(base::FilePath("file_path"),
HoldingSpaceFile::FileSystemType::kTest,
GURL("filesystem::file_system_url")),
/*image_resolver=*/base::BindOnce(&CreateFakeHoldingSpaceImage));
// Initially the text should reflect the backing file.
EXPECT_EQ(holding_space_item->GetText(), u"file_path");
// It should be possible to update text to a new value.
EXPECT_TRUE(holding_space_item->SetText(u"text"));
EXPECT_EQ(holding_space_item->GetText(), u"text");
// It should no-op to try to update text to its existing value.
EXPECT_FALSE(holding_space_item->SetText(u"text"));
EXPECT_EQ(holding_space_item->GetText(), u"text");
// It should be possible to unset text which will once again cause text to
// reflect the backing file.
EXPECT_TRUE(holding_space_item->SetText(std::nullopt));
EXPECT_EQ(holding_space_item->GetText(), u"file_path");
}
INSTANTIATE_TEST_SUITE_P(
All,
HoldingSpaceItemTest,
testing::ValuesIn(holding_space_util::GetAllItemTypes()));
} // namespace ash