-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Boolean method to check whether to offer shared clipboard.
Bug: 992355 Change-Id: Ic258df6c77e77f7fe13d68e046e4ae0bce408aa9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1755908 Commit-Queue: Yasmin Molazadeh <yasmo@chromium.org> Reviewed-by: Michael van Ouwerkerk <mvanouwerkerk@chromium.org> Cr-Commit-Position: refs/heads/master@{#687223}
- Loading branch information
Yasmin
authored and
Commit Bot
committed
Aug 15, 2019
1 parent
c1d8fc4
commit 2b08a10
Showing
5 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
chrome/browser/sharing/shared_clipboard/shared_clipboard_utils.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2019 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/sharing/shared_clipboard/shared_clipboard_utils.h" | ||
|
||
#include "chrome/browser/sharing/shared_clipboard/feature_flags.h" | ||
#include "chrome/browser/sharing/sharing_service.h" | ||
#include "chrome/browser/sharing/sharing_service_factory.h" | ||
#include "content/public/browser/browser_context.h" | ||
|
||
bool ShouldOfferSharedClipboard(content::BrowserContext* browser_context, | ||
const base::string16& text) { | ||
SharingService* sharing_service = | ||
SharingServiceFactory::GetForBrowserContext(browser_context); | ||
return sharing_service && base::FeatureList::IsEnabled(kSharedClipboardUI) && | ||
!text.empty(); | ||
} |
17 changes: 17 additions & 0 deletions
17
chrome/browser/sharing/shared_clipboard/shared_clipboard_utils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2019 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_SHARING_SHARED_CLIPBOARD_SHARED_CLIPBOARD_UTILS_H_ | ||
#define CHROME_BROWSER_SHARING_SHARED_CLIPBOARD_SHARED_CLIPBOARD_UTILS_H_ | ||
|
||
#include "base/strings/string16.h" | ||
|
||
namespace content { | ||
class BrowserContext; | ||
} // namespace content | ||
|
||
bool ShouldOfferSharedClipboard(content::BrowserContext* browser_context, | ||
const base::string16& text); | ||
|
||
#endif // CHROME_BROWSER_SHARING_SHARED_CLIPBOARD_SHARED_CLIPBOARD_UTILS_H_ |
116 changes: 116 additions & 0 deletions
116
chrome/browser/sharing/shared_clipboard/shared_clipboard_utils_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright 2019 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/sharing/shared_clipboard/shared_clipboard_context_menu_observer.h" | ||
|
||
#include <memory> | ||
|
||
#include "base/strings/utf_string_conversions.h" | ||
#include "base/test/scoped_feature_list.h" | ||
#include "chrome/browser/sharing/shared_clipboard/feature_flags.h" | ||
#include "chrome/browser/sharing/shared_clipboard/shared_clipboard_utils.h" | ||
#include "chrome/browser/sharing/sharing_fcm_handler.h" | ||
#include "chrome/browser/sharing/sharing_fcm_sender.h" | ||
#include "chrome/browser/sharing/sharing_service.h" | ||
#include "chrome/browser/sharing/sharing_service_factory.h" | ||
#include "chrome/browser/sharing/sharing_sync_preference.h" | ||
#include "chrome/browser/sharing/vapid_key_manager.h" | ||
#include "chrome/test/base/testing_profile.h" | ||
#include "content/public/test/test_browser_thread_bundle.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
using ::testing::_; | ||
using ::testing::ByMove; | ||
using ::testing::Eq; | ||
using ::testing::NiceMock; | ||
using ::testing::Return; | ||
|
||
namespace { | ||
|
||
const char kEmptyText[] = ""; | ||
const char kText[] = "Some text to copy to phone device."; | ||
|
||
class MockSharingService : public SharingService { | ||
public: | ||
explicit MockSharingService(std::unique_ptr<SharingFCMHandler> fcm_handler) | ||
: SharingService(/* sync_prefs= */ nullptr, | ||
/* vapid_key_manager= */ nullptr, | ||
/* sharing_device_registration= */ nullptr, | ||
/* fcm_sender= */ nullptr, | ||
std::move(fcm_handler), | ||
/* gcm_driver= */ nullptr, | ||
/* device_info_tracker= */ nullptr, | ||
/* local_device_info_provider= */ nullptr, | ||
/* sync_service */ nullptr) {} | ||
|
||
~MockSharingService() override = default; | ||
|
||
MOCK_CONST_METHOD0(GetState, State()); | ||
|
||
private: | ||
DISALLOW_COPY_AND_ASSIGN(MockSharingService); | ||
}; | ||
|
||
class SharedClipboardUtilsTest : public testing::Test { | ||
public: | ||
SharedClipboardUtilsTest() = default; | ||
|
||
~SharedClipboardUtilsTest() override = default; | ||
|
||
void SetUp() override { | ||
SharingServiceFactory::GetInstance()->SetTestingFactory( | ||
&profile_, base::BindRepeating(&SharedClipboardUtilsTest::CreateService, | ||
base::Unretained(this))); | ||
} | ||
|
||
protected: | ||
std::unique_ptr<KeyedService> CreateService( | ||
content::BrowserContext* context) { | ||
if (!create_service_) | ||
return nullptr; | ||
|
||
return std::make_unique<NiceMock<MockSharingService>>( | ||
std::make_unique<SharingFCMHandler>(nullptr, nullptr)); | ||
} | ||
|
||
base::test::ScopedFeatureList scoped_feature_list_; | ||
content::TestBrowserThreadBundle thread_bundle_; | ||
TestingProfile profile_; | ||
bool create_service_ = true; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(SharedClipboardUtilsTest); | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_F(SharedClipboardUtilsTest, UIFlagDisabled_DoNotShowMenu) { | ||
scoped_feature_list_.InitAndDisableFeature(kSharedClipboardUI); | ||
EXPECT_FALSE( | ||
ShouldOfferSharedClipboard(&profile_, base::ASCIIToUTF16(kText))); | ||
} | ||
|
||
TEST_F(SharedClipboardUtilsTest, IncognitoProfile_DoNotShowMenu) { | ||
scoped_feature_list_.InitAndEnableFeature(kSharedClipboardUI); | ||
EXPECT_FALSE(ShouldOfferSharedClipboard(profile_.GetOffTheRecordProfile(), | ||
base::ASCIIToUTF16(kText))); | ||
} | ||
|
||
TEST_F(SharedClipboardUtilsTest, EmptyClipboardProtocol_DoNotShowMenu) { | ||
scoped_feature_list_.InitAndEnableFeature(kSharedClipboardUI); | ||
EXPECT_FALSE( | ||
ShouldOfferSharedClipboard(&profile_, base::ASCIIToUTF16(kEmptyText))); | ||
} | ||
|
||
TEST_F(SharedClipboardUtilsTest, ClipboardProtocol_ShowMenu) { | ||
scoped_feature_list_.InitAndEnableFeature(kSharedClipboardUI); | ||
EXPECT_TRUE(ShouldOfferSharedClipboard(&profile_, base::ASCIIToUTF16(kText))); | ||
} | ||
|
||
TEST_F(SharedClipboardUtilsTest, NoSharingService_DoNotShowMenu) { | ||
scoped_feature_list_.InitAndEnableFeature(kSharedClipboardUI); | ||
create_service_ = false; | ||
EXPECT_FALSE( | ||
ShouldOfferSharedClipboard(&profile_, base::ASCIIToUTF16(kText))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters