Skip to content

Commit

Permalink
Boolean method to check whether to offer shared clipboard.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3370,6 +3370,8 @@ jumbo_split_static_library("browser") {
"sharing/shared_clipboard/shared_clipboard_context_menu_observer.h",
"sharing/shared_clipboard/shared_clipboard_ui_controller.cc",
"sharing/shared_clipboard/shared_clipboard_ui_controller.h",
"sharing/shared_clipboard/shared_clipboard_utils.cc",
"sharing/shared_clipboard/shared_clipboard_utils.h",
"sharing/sharing_dialog.h",
"sharing/sharing_ui_controller.cc",
"sharing/sharing_ui_controller.h",
Expand Down
18 changes: 18 additions & 0 deletions chrome/browser/sharing/shared_clipboard/shared_clipboard_utils.cc
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 chrome/browser/sharing/shared_clipboard/shared_clipboard_utils.h
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_
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)));
}
1 change: 1 addition & 0 deletions chrome/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3925,6 +3925,7 @@ test("unit_tests") {
"../browser/sharing/click_to_call/click_to_call_utils_unittest.cc",
"../browser/sharing/shared_clipboard/shared_clipboard_context_menu_observer_unittest.cc",
"../browser/sharing/shared_clipboard/shared_clipboard_ui_controller_unittest.cc",
"../browser/sharing/shared_clipboard/shared_clipboard_utils_unittest.cc",
"../browser/ui/autofill/payments/local_card_migration_bubble_controller_impl_unittest.cc",
"../browser/ui/autofill/payments/save_card_bubble_controller_impl_unittest.cc",
"../browser/ui/bluetooth/bluetooth_chooser_controller_unittest.cc",
Expand Down

0 comments on commit 2b08a10

Please sign in to comment.