Commit ea750d09 authored by Ana Salazar's avatar Ana Salazar Committed by Commit Bot

Add Nearby Shared files into the holding space model

We want to give users a quick shortcut to access recent nearby shared
files within Tote. As user receives a file via nearby share, we should
be able to add them to the holding space model in a similar way we add
downloads as they are completed.

Bug: 1130625
Change-Id: Iba7ab69e7f8d9b7c05e740e8e4331b85a890b423
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2463584Reviewed-by: default avatarToni Baržić <tbarzic@chromium.org>
Reviewed-by: default avatarJosh Nohle <nohle@chromium.org>
Commit-Queue: Ana Salazar <anasalazar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822896}
parent ecf20847
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/nearby_sharing/nearby_notification_manager.h" #include "chrome/browser/nearby_sharing/nearby_notification_manager.h"
#include "ash/public/cpp/ash_features.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/notreached.h" #include "base/notreached.h"
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
...@@ -22,6 +23,8 @@ ...@@ -22,6 +23,8 @@
#include "chrome/browser/notifications/notification_display_service.h" #include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/platform_util.h" #include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_keyed_service.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_keyed_service_factory.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
...@@ -725,6 +728,18 @@ void NearbyNotificationManager::ShowIncomingSuccess( ...@@ -725,6 +728,18 @@ void NearbyNotificationManager::ShowIncomingSuccess(
notification_display_service_->Display( notification_display_service_->Display(
NotificationHandler::Type::NEARBY_SHARE, notification, NotificationHandler::Type::NEARBY_SHARE, notification,
/*metadata=*/nullptr); /*metadata=*/nullptr);
if (ash::features::IsTemporaryHoldingSpaceEnabled()) {
ash::HoldingSpaceKeyedService* holding_space_keyed_service =
ash::HoldingSpaceKeyedServiceFactory::GetInstance()->GetService(
profile_);
if (holding_space_keyed_service) {
for (const auto& file : share_target.file_attachments) {
if (file.file_path().has_value())
holding_space_keyed_service->AddNearbyShare(file.file_path().value());
}
}
}
} }
void NearbyNotificationManager::ShowFailure(const ShareTarget& share_target) { void NearbyNotificationManager::ShowFailure(const ShareTarget& share_target) {
......
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "ash/public/cpp/ash_features.h"
#include "ash/public/cpp/holding_space/holding_space_controller.h"
#include "ash/public/cpp/holding_space/holding_space_item.h"
#include "ash/public/cpp/holding_space/holding_space_model.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
...@@ -19,6 +23,7 @@ ...@@ -19,6 +23,7 @@
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "chrome/app/vector_icons/vector_icons.h" #include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/browser_features.h" #include "chrome/browser/browser_features.h"
#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h" #include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service_factory.h" #include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_core_service_impl.h" #include "chrome/browser/download/download_core_service_impl.h"
...@@ -32,11 +37,21 @@ ...@@ -32,11 +37,21 @@
#include "chrome/browser/nearby_sharing/transfer_metadata_builder.h" #include "chrome/browser/nearby_sharing/transfer_metadata_builder.h"
#include "chrome/browser/notifications/notification_display_service_factory.h" #include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/notifications/notification_display_service_tester.h" #include "chrome/browser/notifications/notification_display_service_tester.h"
#include "chrome/browser/ui/ash/holding_space/fake_holding_space_color_provider.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_keyed_service_factory.h"
#include "chrome/browser/ui/ash/holding_space/scoped_test_downloads_mount_point.h"
#include "chrome/browser/ui/ash/test_session_controller.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/account_id/account_id.h"
#include "components/prefs/testing_pref_service.h" #include "components/prefs/testing_pref_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h" #include "content/public/test/browser_task_environment.h"
#include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h" #include "services/data_decoder/public/cpp/test_support/in_process_data_decoder.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_context.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
...@@ -96,6 +111,13 @@ SkBitmap CreateTestSkBitmap() { ...@@ -96,6 +111,13 @@ SkBitmap CreateTestSkBitmap() {
return bitmap; return bitmap;
} }
std::unique_ptr<TestingProfileManager> CreateTestingProfileManager() {
std::unique_ptr<TestingProfileManager> profile_manager(
new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
EXPECT_TRUE(profile_manager->SetUp());
return profile_manager;
}
class NearbyNotificationManagerTest : public testing::Test { class NearbyNotificationManagerTest : public testing::Test {
public: public:
NearbyNotificationManagerTest() { NearbyNotificationManagerTest() {
...@@ -1118,3 +1140,96 @@ TEST_F(NearbyNotificationManagerTest, ...@@ -1118,3 +1140,96 @@ TEST_F(NearbyNotificationManagerTest,
// Notification should be closed. // Notification should be closed.
EXPECT_EQ(0u, GetDisplayedNotifications().size()); EXPECT_EQ(0u, GetDisplayedNotifications().size());
} }
class NearbyFilesHoldingSpaceTest : public testing::Test {
public:
NearbyFilesHoldingSpaceTest()
: session_controller_(std::make_unique<TestSessionController>()),
user_manager_(new chromeos::FakeChromeUserManager) {
scoped_feature_list_.InitWithFeatures(
{features::kNearbySharing, ash::features::kTemporaryHoldingSpace}, {});
holding_space_controller_ = std::make_unique<ash::HoldingSpaceController>(
std::make_unique<ash::holding_space::FakeHoldingSpaceColorProvider>());
profile_manager_ = CreateTestingProfileManager();
const AccountId account_id(AccountId::FromUserEmail(""));
user_manager_->AddUser(account_id);
user_manager_->LoginUser(account_id);
profile_ = profile_manager_->CreateTestingProfile("");
}
~NearbyFilesHoldingSpaceTest() override = default;
// testing::Test:
void SetUp() override {
manager_ = std::make_unique<NearbyNotificationManager>(
NotificationDisplayServiceFactory::GetForProfile(profile_),
CreateAndUseMockNearbySharingService(profile_), profile_->GetPrefs(),
profile_);
}
NearbyNotificationManager* manager() { return manager_.get(); }
ash::HoldingSpaceModel* GetHoldingSpaceModel() const {
return holding_space_controller_ ? holding_space_controller_->model()
: nullptr;
}
protected:
base::test::ScopedFeatureList scoped_feature_list_;
content::BrowserTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
std::unique_ptr<TestingProfileManager> profile_manager_;
TestingProfile* profile_;
std::unique_ptr<NearbyNotificationManager> manager_;
std::unique_ptr<TestSessionController> session_controller_;
std::unique_ptr<ash::HoldingSpaceController> holding_space_controller_;
chromeos::FakeChromeUserManager* user_manager_;
};
TEST_F(NearbyFilesHoldingSpaceTest, ShowSuccess_Files) {
ash::holding_space::ScopedTestDownloadsMountPoint downloads_mount(profile_);
ASSERT_TRUE(downloads_mount.IsValid());
ShareTarget share_target;
share_target.is_incoming = true;
const base::FilePath file_virtual_path("Sample.txt");
base::FilePath file_path =
downloads_mount.CreateFile(file_virtual_path, "Sample Text");
FileAttachment attachment(file_path);
share_target.file_attachments.push_back(std::move(attachment));
manager()->ShowSuccess(share_target);
ash::HoldingSpaceModel* holding_space_model = GetHoldingSpaceModel();
ASSERT_TRUE(holding_space_model);
ASSERT_EQ(share_target.file_attachments.size(),
holding_space_model->items().size());
for (int i = 0; i < share_target.file_attachments.size(); ++i) {
ash::HoldingSpaceItem* holding_space_item =
holding_space_model->items()[i].get();
EXPECT_EQ(ash::HoldingSpaceItem::Type::kNearbyShare,
holding_space_item->type());
EXPECT_EQ(share_target.file_attachments[i].file_path(),
holding_space_item->file_path());
}
}
TEST_F(NearbyFilesHoldingSpaceTest, ShowSuccess_Text) {
ShareTarget share_target;
share_target.is_incoming = true;
TextAttachment attachment(TextAttachment::Type::kText, "Sample Text");
share_target.text_attachments.push_back(std::move(attachment));
manager()->ShowSuccess(share_target);
ash::HoldingSpaceModel* holding_space_model = GetHoldingSpaceModel();
ASSERT_TRUE(holding_space_model);
EXPECT_TRUE(holding_space_model->items().empty());
}
...@@ -36,8 +36,12 @@ source_set("test_support") { ...@@ -36,8 +36,12 @@ source_set("test_support") {
testonly = true testonly = true
sources = [ sources = [
"//chrome/browser/ui/ash/holding_space/fake_holding_space_color_provider.cc",
"//chrome/browser/ui/ash/holding_space/fake_holding_space_color_provider.h",
"//chrome/browser/ui/ash/holding_space/holding_space_browsertest_base.cc", "//chrome/browser/ui/ash/holding_space/holding_space_browsertest_base.cc",
"//chrome/browser/ui/ash/holding_space/holding_space_browsertest_base.h", "//chrome/browser/ui/ash/holding_space/holding_space_browsertest_base.h",
"//chrome/browser/ui/ash/holding_space/scoped_test_downloads_mount_point.cc",
"//chrome/browser/ui/ash/holding_space/scoped_test_downloads_mount_point.h",
] ]
deps = [ deps = [
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/holding_space/fake_holding_space_color_provider.h"
#include "base/files/file_util.h"
#include "chrome/browser/chromeos/file_manager/app_id.h"
#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "chrome/browser/chromeos/file_manager/path_util.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_context.h"
namespace ash {
namespace holding_space {
SkColor FakeHoldingSpaceColorProvider::GetBackgroundColor() const {
return SkColor();
}
SkColor FakeHoldingSpaceColorProvider::GetFileIconColor() const {
return SkColor();
}
} // namespace holding_space
} // namespace ash
\ No newline at end of file
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_ASH_HOLDING_SPACE_FAKE_HOLDING_SPACE_COLOR_PROVIDER_H_
#define CHROME_BROWSER_UI_ASH_HOLDING_SPACE_FAKE_HOLDING_SPACE_COLOR_PROVIDER_H_
#include "ash/public/cpp/holding_space/holding_space_color_provider.h"
namespace ash {
namespace holding_space {
class FakeHoldingSpaceColorProvider : public ash::HoldingSpaceColorProvider {
public:
FakeHoldingSpaceColorProvider() = default;
FakeHoldingSpaceColorProvider(const FakeHoldingSpaceColorProvider&) = delete;
FakeHoldingSpaceColorProvider& operator=(
const FakeHoldingSpaceColorProvider&) = delete;
~FakeHoldingSpaceColorProvider() override = default;
// ash::HoldingSpaceColorProvider:
SkColor GetBackgroundColor() const override;
SkColor GetFileIconColor() const override;
};
} // namespace holding_space
} // namespace ash
#endif // CHROME_BROWSER_UI_ASH_HOLDING_SPACE_FAKE_HOLDING_SPACE_COLOR_PROVIDER_H_
\ No newline at end of file
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h" #include "base/test/simple_test_clock.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/chromeos/file_manager/app_id.h" #include "chrome/browser/chromeos/file_manager/app_id.h"
#include "chrome/browser/chromeos/file_manager/fileapi_util.h" #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "chrome/browser/chromeos/file_manager/path_util.h" #include "chrome/browser/chromeos/file_manager/path_util.h"
...@@ -32,6 +33,7 @@ ...@@ -32,6 +33,7 @@
#include "chrome/browser/ui/ash/holding_space/holding_space_keyed_service_factory.h" #include "chrome/browser/ui/ash/holding_space/holding_space_keyed_service_factory.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_persistence_delegate.h" #include "chrome/browser/ui/ash/holding_space/holding_space_persistence_delegate.h"
#include "chrome/browser/ui/ash/holding_space/holding_space_util.h" #include "chrome/browser/ui/ash/holding_space/holding_space_util.h"
#include "chrome/browser/ui/ash/holding_space/scoped_test_downloads_mount_point.h"
#include "chrome/test/base/browser_with_test_window_test.h" #include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile_manager.h" #include "chrome/test/base/testing_profile_manager.h"
#include "components/account_id/account_id.h" #include "components/account_id/account_id.h"
...@@ -62,40 +64,6 @@ std::vector<HoldingSpaceItem::Type> GetHoldingSpaceItemTypes() { ...@@ -62,40 +64,6 @@ std::vector<HoldingSpaceItem::Type> GetHoldingSpaceItemTypes() {
return types; return types;
} }
// Utility class that registers the downloads external file system mount point,
// and grants file manager app access permission for the mount point.
class ScopedDownloadsMountPoint {
public:
explicit ScopedDownloadsMountPoint(Profile* profile)
: name_(file_manager::util::GetDownloadsMountPointName(profile)) {
if (!temp_dir_.CreateUniqueTempDir())
return;
storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
name_, storage::kFileSystemTypeNativeLocal,
storage::FileSystemMountOption(), temp_dir_.GetPath());
file_manager::util::GetFileSystemContextForExtensionId(
profile, file_manager::kFileManagerAppId)
->external_backend()
->GrantFileAccessToExtension(file_manager::kFileManagerAppId,
base::FilePath(name_));
}
~ScopedDownloadsMountPoint() {
storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(name_);
}
bool IsValid() const { return temp_dir_.IsValid(); }
const base::FilePath& GetRootPath() const { return temp_dir_.GetPath(); }
const std::string& name() const { return name_; }
private:
base::ScopedTempDir temp_dir_;
std::string name_;
};
// Utility class which can wait until a `HoldingSpaceModel` for a given profile // Utility class which can wait until a `HoldingSpaceModel` for a given profile
// is attached to the `HoldingSpaceController`. // is attached to the `HoldingSpaceController`.
class HoldingSpaceModelAttachedWaiter : public HoldingSpaceControllerObserver { class HoldingSpaceModelAttachedWaiter : public HoldingSpaceControllerObserver {
...@@ -230,28 +198,6 @@ class HoldingSpaceKeyedServiceTest : public BrowserWithTestWindowTest { ...@@ -230,28 +198,6 @@ class HoldingSpaceKeyedServiceTest : public BrowserWithTestWindowTest {
return ash_test_helper()->test_session_controller_client(); return ash_test_helper()->test_session_controller_client();
} }
// Creates a file under path |mount_point|/|relative_path| with the provided
// content. Returns the created file's file path, or an empty path on failure.
base::FilePath CreateFile(const ScopedDownloadsMountPoint& mount_point,
const base::FilePath& relative_path,
const std::string& content) {
const base::FilePath path = mount_point.GetRootPath().Append(relative_path);
if (!base::CreateDirectory(path.DirName()))
return base::FilePath();
if (!base::WriteFile(path, content))
return base::FilePath();
return path;
}
// Creates an arbitrary file under the specified 'mount_point'.
base::FilePath CreateArbitraryFile(
const ScopedDownloadsMountPoint& mount_point) {
return CreateFile(
mount_point,
base::FilePath(base::UnguessableToken::Create().ToString()),
/*content=*/std::string());
}
// Resolves an absolute file path in the file manager's file system context, // Resolves an absolute file path in the file manager's file system context,
// and returns the file's file system URL. // and returns the file's file system URL.
GURL GetFileSystemUrl(Profile* profile, GURL GetFileSystemUrl(Profile* profile,
...@@ -352,7 +298,7 @@ class HoldingSpaceKeyedServiceTest : public BrowserWithTestWindowTest { ...@@ -352,7 +298,7 @@ class HoldingSpaceKeyedServiceTest : public BrowserWithTestWindowTest {
// manager app. // manager app.
TEST_F(HoldingSpaceKeyedServiceTest, AddScreenshotItem) { TEST_F(HoldingSpaceKeyedServiceTest, AddScreenshotItem) {
// Create a test downloads mount point. // Create a test downloads mount point.
ScopedDownloadsMountPoint downloads_mount(GetProfile()); holding_space::ScopedTestDownloadsMountPoint downloads_mount(GetProfile());
ASSERT_TRUE(downloads_mount.IsValid()); ASSERT_TRUE(downloads_mount.IsValid());
// Wait for the holding space model. // Wait for the holding space model.
...@@ -371,7 +317,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddScreenshotItem) { ...@@ -371,7 +317,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddScreenshotItem) {
// test will try to resolve the file's file system URL, which fails if the // test will try to resolve the file's file system URL, which fails if the
// file does not exist. // file does not exist.
const base::FilePath item_1_full_path = const base::FilePath item_1_full_path =
CreateFile(downloads_mount, item_1_virtual_path, "red"); downloads_mount.CreateFile(item_1_virtual_path, "red");
ASSERT_FALSE(item_1_full_path.empty()); ASSERT_FALSE(item_1_full_path.empty());
holding_space_service->AddScreenshot(item_1_full_path); holding_space_service->AddScreenshot(item_1_full_path);
...@@ -382,7 +328,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddScreenshotItem) { ...@@ -382,7 +328,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddScreenshotItem) {
// test will try to resolve the file's file system URL, which fails if the // test will try to resolve the file's file system URL, which fails if the
// file does not exist. // file does not exist.
const base::FilePath item_2_full_path = const base::FilePath item_2_full_path =
CreateFile(downloads_mount, item_2_virtual_path, "blue"); downloads_mount.CreateFile(item_2_virtual_path, "blue");
ASSERT_FALSE(item_2_full_path.empty()); ASSERT_FALSE(item_2_full_path.empty());
holding_space_service->AddScreenshot(item_2_full_path); holding_space_service->AddScreenshot(item_2_full_path);
...@@ -453,7 +399,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, SecondaryUserProfile) { ...@@ -453,7 +399,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, SecondaryUserProfile) {
// Verifies that updates to the holding space model are persisted. // Verifies that updates to the holding space model are persisted.
TEST_F(HoldingSpaceKeyedServiceTest, UpdatePersistentStorage) { TEST_F(HoldingSpaceKeyedServiceTest, UpdatePersistentStorage) {
// Create a file system mount point. // Create a file system mount point.
ScopedDownloadsMountPoint downloads_mount(GetProfile()); holding_space::ScopedTestDownloadsMountPoint downloads_mount(GetProfile());
ASSERT_TRUE(downloads_mount.IsValid()); ASSERT_TRUE(downloads_mount.IsValid());
HoldingSpaceKeyedService* const primary_holding_space_service = HoldingSpaceKeyedService* const primary_holding_space_service =
...@@ -468,7 +414,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, UpdatePersistentStorage) { ...@@ -468,7 +414,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, UpdatePersistentStorage) {
// Verify persistent storage is updated when adding each type of item. // Verify persistent storage is updated when adding each type of item.
for (const HoldingSpaceItem::Type type : GetHoldingSpaceItemTypes()) { for (const HoldingSpaceItem::Type type : GetHoldingSpaceItemTypes()) {
const base::FilePath file_path = CreateArbitraryFile(downloads_mount); const base::FilePath file_path = downloads_mount.CreateArbitraryFile();
const GURL file_system_url = GetFileSystemUrl(GetProfile(), file_path); const GURL file_system_url = GetFileSystemUrl(GetProfile(), file_path);
auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem( auto holding_space_item = HoldingSpaceItem::CreateFileBackedItem(
...@@ -504,7 +450,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, UpdatePersistentStorage) { ...@@ -504,7 +450,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, UpdatePersistentStorage) {
// any stale holding space items are removed. // any stale holding space items are removed.
TEST_F(HoldingSpaceKeyedServiceTest, RestorePersistentStorage) { TEST_F(HoldingSpaceKeyedServiceTest, RestorePersistentStorage) {
// Create file system mount point. // Create file system mount point.
ScopedDownloadsMountPoint downloads_mount(GetProfile()); holding_space::ScopedTestDownloadsMountPoint downloads_mount(GetProfile());
ASSERT_TRUE(downloads_mount.IsValid()); ASSERT_TRUE(downloads_mount.IsValid());
HoldingSpaceKeyedService* const primary_holding_space_service = HoldingSpaceKeyedService* const primary_holding_space_service =
...@@ -521,7 +467,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RestorePersistentStorage) { ...@@ -521,7 +467,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RestorePersistentStorage) {
// Persist some holding space items of each type. // Persist some holding space items of each type.
for (const HoldingSpaceItem::Type type : GetHoldingSpaceItemTypes()) { for (const HoldingSpaceItem::Type type : GetHoldingSpaceItemTypes()) {
const base::FilePath file = CreateArbitraryFile(downloads_mount); const base::FilePath file = downloads_mount.CreateArbitraryFile();
const GURL file_system_url = GetFileSystemUrl(GetProfile(), file); const GURL file_system_url = GetFileSystemUrl(GetProfile(), file);
auto fresh_holding_space_item = auto fresh_holding_space_item =
...@@ -603,7 +549,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RestorePersistentStorage) { ...@@ -603,7 +549,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RestorePersistentStorage) {
// kMaxFileAge. // kMaxFileAge.
TEST_F(HoldingSpaceKeyedServiceTest, RemoveOlderFilesFromPersistance) { TEST_F(HoldingSpaceKeyedServiceTest, RemoveOlderFilesFromPersistance) {
// Create file system mount point. // Create file system mount point.
ScopedDownloadsMountPoint downloads_mount(GetProfile()); holding_space::ScopedTestDownloadsMountPoint downloads_mount(GetProfile());
ASSERT_TRUE(downloads_mount.IsValid()); ASSERT_TRUE(downloads_mount.IsValid());
HoldingSpaceKeyedService* const primary_holding_space_service = HoldingSpaceKeyedService* const primary_holding_space_service =
...@@ -620,7 +566,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RemoveOlderFilesFromPersistance) { ...@@ -620,7 +566,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RemoveOlderFilesFromPersistance) {
// Persist some holding space items of each type. // Persist some holding space items of each type.
for (const HoldingSpaceItem::Type type : GetHoldingSpaceItemTypes()) { for (const HoldingSpaceItem::Type type : GetHoldingSpaceItemTypes()) {
const base::FilePath file = CreateArbitraryFile(downloads_mount); const base::FilePath file = downloads_mount.CreateArbitraryFile();
const GURL file_system_url = GetFileSystemUrl(GetProfile(), file); const GURL file_system_url = GetFileSystemUrl(GetProfile(), file);
auto fresh_holding_space_item = auto fresh_holding_space_item =
...@@ -704,7 +650,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RemoveOlderFilesFromPersistance) { ...@@ -704,7 +650,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, RemoveOlderFilesFromPersistance) {
TEST_F(HoldingSpaceKeyedServiceTest, AddDownloadItem) { TEST_F(HoldingSpaceKeyedServiceTest, AddDownloadItem) {
TestingProfile* profile = GetProfile(); TestingProfile* profile = GetProfile();
// Create a test downloads mount point. // Create a test downloads mount point.
ScopedDownloadsMountPoint downloads_mount(profile); holding_space::ScopedTestDownloadsMountPoint downloads_mount(profile);
ASSERT_TRUE(downloads_mount.IsValid()); ASSERT_TRUE(downloads_mount.IsValid());
// Create a fake download file on the local file system - later parts of the // Create a fake download file on the local file system - later parts of the
...@@ -712,7 +658,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddDownloadItem) { ...@@ -712,7 +658,7 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddDownloadItem) {
// file does not exist. // file does not exist.
const base::FilePath download_item_virtual_path("Download 1.png"); const base::FilePath download_item_virtual_path("Download 1.png");
const base::FilePath download_item_full_path = const base::FilePath download_item_full_path =
CreateFile(downloads_mount, download_item_virtual_path, "download 1"); downloads_mount.CreateFile(download_item_virtual_path, "download 1");
MockDownloadManager* mock_download_manager = download_manager(); MockDownloadManager* mock_download_manager = download_manager();
std::unique_ptr<download::MockDownloadItem> item( std::unique_ptr<download::MockDownloadItem> item(
...@@ -761,4 +707,94 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddDownloadItem) { ...@@ -761,4 +707,94 @@ TEST_F(HoldingSpaceKeyedServiceTest, AddDownloadItem) {
downloads_mount.name())); downloads_mount.name()));
} }
class HoldingSpaceKeyedServiceNearbySharingTest
: public HoldingSpaceKeyedServiceTest {
public:
HoldingSpaceKeyedServiceNearbySharingTest() {
scoped_feature_list_.InitWithFeatures(
{::features::kNearbySharing, ash::features::kTemporaryHoldingSpace},
{});
}
~HoldingSpaceKeyedServiceNearbySharingTest() override = default;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
TEST_F(HoldingSpaceKeyedServiceNearbySharingTest, AddNearbyShareItem) {
// Create a test downloads mount point.
holding_space::ScopedTestDownloadsMountPoint downloads_mount(GetProfile());
ASSERT_TRUE(downloads_mount.IsValid());
// Wait for the holding space model.
HoldingSpaceModelAttachedWaiter(GetProfile()).Wait();
// Verify that the holding space model gets set even if the holding space
// keyed service is not explicitly created.
HoldingSpaceModel* const initial_model =
HoldingSpaceController::Get()->model();
EXPECT_TRUE(initial_model);
HoldingSpaceKeyedService* const holding_space_service =
HoldingSpaceKeyedServiceFactory::GetInstance()->GetService(GetProfile());
const base::FilePath item_1_virtual_path("File 1.png");
// Create a fake nearby shared file on the local file system - later parts of
// the test will try to resolve the file's file system URL, which fails if the
// file does not exist.
const base::FilePath item_1_full_path =
downloads_mount.CreateFile(item_1_virtual_path, "red");
ASSERT_FALSE(item_1_full_path.empty());
holding_space_service->AddNearbyShare(item_1_full_path);
const base::FilePath item_2_virtual_path = base::FilePath("Alt/File 2.png");
// Create a fake nearby shared file on the local file system - later parts of
// the test will try to resolve the file's file system URL, which fails if the
// file does not exist.
const base::FilePath item_2_full_path =
downloads_mount.CreateFile(item_2_virtual_path, "blue");
ASSERT_FALSE(item_2_full_path.empty());
holding_space_service->AddNearbyShare(item_2_full_path);
EXPECT_EQ(initial_model, HoldingSpaceController::Get()->model());
EXPECT_EQ(HoldingSpaceController::Get()->model(),
holding_space_service->model_for_testing());
HoldingSpaceModel* const model = HoldingSpaceController::Get()->model();
ASSERT_EQ(2u, model->items().size());
const HoldingSpaceItem* item_1 = model->items()[0].get();
EXPECT_EQ(item_1_full_path, item_1->file_path());
EXPECT_TRUE(gfx::BitmapsAreEqual(
*holding_space_util::ResolveImage(
holding_space_service->thumbnail_loader_for_testing(),
HoldingSpaceItem::Type::kNearbyShare, item_1_full_path)
->image_skia()
.bitmap(),
*item_1->image().image_skia().bitmap()));
// Verify the item file system URL resolves to the correct file in the file
// manager's context.
EXPECT_EQ(
item_1_virtual_path,
GetVirtualPathFromUrl(item_1->file_system_url(), downloads_mount.name()));
EXPECT_EQ(base::ASCIIToUTF16("File 1.png"), item_1->text());
const HoldingSpaceItem* item_2 = model->items()[1].get();
EXPECT_EQ(item_2_full_path, item_2->file_path());
EXPECT_TRUE(gfx::BitmapsAreEqual(
*holding_space_util::ResolveImage(
holding_space_service->thumbnail_loader_for_testing(),
HoldingSpaceItem::Type::kNearbyShare, item_2_full_path)
->image_skia()
.bitmap(),
*item_2->image().image_skia().bitmap()));
// Verify the item file system URL resolves to the correct file in the file
// manager's context.
EXPECT_EQ(
item_2_virtual_path,
GetVirtualPathFromUrl(item_2->file_system_url(), downloads_mount.name()));
EXPECT_EQ(base::ASCIIToUTF16("File 2.png"), item_2->text());
}
} // namespace ash } // namespace ash
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/holding_space/scoped_test_downloads_mount_point.h"
#include "base/files/file_util.h"
#include "chrome/browser/chromeos/file_manager/app_id.h"
#include "chrome/browser/chromeos/file_manager/fileapi_util.h"
#include "chrome/browser/chromeos/file_manager/path_util.h"
#include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_context.h"
namespace ash {
namespace holding_space {
ScopedTestDownloadsMountPoint::ScopedTestDownloadsMountPoint(Profile* profile)
: name_(file_manager::util::GetDownloadsMountPointName(profile)) {
if (!temp_dir_.CreateUniqueTempDir())
return;
storage::ExternalMountPoints::GetSystemInstance()->RegisterFileSystem(
name_, storage::kFileSystemTypeNativeLocal,
storage::FileSystemMountOption(), temp_dir_.GetPath());
file_manager::util::GetFileSystemContextForExtensionId(
profile, file_manager::kFileManagerAppId)
->external_backend()
->GrantFileAccessToExtension(file_manager::kFileManagerAppId,
base::FilePath(name_));
}
ScopedTestDownloadsMountPoint::~ScopedTestDownloadsMountPoint() {
storage::ExternalMountPoints::GetSystemInstance()->RevokeFileSystem(name_);
}
base::FilePath ScopedTestDownloadsMountPoint::CreateFile(
const base::FilePath& relative_path,
const std::string& content) {
const base::FilePath path = GetRootPath().Append(relative_path);
if (!base::CreateDirectory(path.DirName()))
return base::FilePath();
if (!base::WriteFile(path, content))
return base::FilePath();
return path;
}
base::FilePath ScopedTestDownloadsMountPoint::CreateArbitraryFile() {
return CreateFile(base::FilePath(base::UnguessableToken::Create().ToString()),
/*content=*/std::string());
}
} // namespace holding_space
} // namespace ash
\ No newline at end of file
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_ASH_HOLDING_SPACE_SCOPED_TEST_DOWNLOADS_MOUNT_POINT_H_
#define CHROME_BROWSER_UI_ASH_HOLDING_SPACE_SCOPED_TEST_DOWNLOADS_MOUNT_POINT_H_
#include <memory>
#include "base/files/scoped_temp_dir.h"
class GURL;
class Profile;
namespace base {
class FilePath;
} // namespace base
namespace ash {
namespace holding_space {
// Utility class that registers the downloads external file system mount point,
// and grants file manager app access permission for the mount point.
class ScopedTestDownloadsMountPoint {
public:
explicit ScopedTestDownloadsMountPoint(Profile* profile);
ScopedTestDownloadsMountPoint(const ScopedTestDownloadsMountPoint&) = delete;
ScopedTestDownloadsMountPoint& operator=(
const ScopedTestDownloadsMountPoint&) = delete;
~ScopedTestDownloadsMountPoint();
bool IsValid() const { return temp_dir_.IsValid(); }
const base::FilePath& GetRootPath() const { return temp_dir_.GetPath(); }
const std::string& name() const { return name_; }
// Creates a file under [mount point root path]/|relative_path| with the
// provided content. Returns the created file's file path, or an empty path on
// failure.
base::FilePath CreateFile(const base::FilePath& relative_path,
const std::string& content);
// Creates an arbitrary file under the 'mount_point'.
base::FilePath CreateArbitraryFile();
private:
base::ScopedTempDir temp_dir_;
std::string name_;
};
} // namespace holding_space
} // namespace ash
#endif // CHROME_BROWSER_UI_ASH_HOLDING_SPACE_SCOPED_TEST_DOWNLOADS_MOUNT_POINT_H_
\ No newline at end of file
...@@ -5017,6 +5017,7 @@ test("unit_tests") { ...@@ -5017,6 +5017,7 @@ test("unit_tests") {
"//chrome/browser/resources/chromeos/zip_archiver/cpp:ziparchiver_unittests", "//chrome/browser/resources/chromeos/zip_archiver/cpp:ziparchiver_unittests",
"//chrome/browser/ui/app_list/search/search_result_ranker:app_launch_predictor_proto", "//chrome/browser/ui/app_list/search/search_result_ranker:app_launch_predictor_proto",
"//chrome/browser/ui/app_list/search/search_result_ranker:recurrence_ranker_proto", "//chrome/browser/ui/app_list/search/search_result_ranker:recurrence_ranker_proto",
"//chrome/browser/ui/ash/holding_space:test_support",
"//chrome/services/printing:pdf_thumbnailer_test", "//chrome/services/printing:pdf_thumbnailer_test",
"//chrome/services/sharing:unit_tests", "//chrome/services/sharing:unit_tests",
"//chrome/services/sharing/nearby:unit_tests", "//chrome/services/sharing/nearby:unit_tests",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment