chromium/components/omnibox/browser/clipboard_provider_unittest.cc

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/omnibox/browser/clipboard_provider.h"

#include <memory>
#include <string>
#include <utility>

#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_provider_listener.h"
#include "components/omnibox/browser/mock_autocomplete_provider_client.h"
#include "components/omnibox/browser/omnibox_field_trial.h"
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "components/omnibox/common/omnibox_features.h"
#include "components/open_from_clipboard/fake_clipboard_recent_content.h"
#include "components/search_engines/search_engines_test_environment.h"
#include "components/search_engines/template_url_service_client.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/omnibox_focus_type.pb.h"
#include "third_party/omnibox_proto/groups.pb.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "url/gurl.h"

#if !BUILDFLAG(IS_IOS)
#include "ui/base/clipboard/test/test_clipboard.h"  // nogncheck
#endif

namespace {

const char kCurrentURL[] =;
const char kClipboardURL[] =;
const char16_t kClipboardText[] =;

class CreateMatchWithContentCallbackWaiter {};

}  // namespace

class ClipboardProviderTest : public testing::Test,
                              public AutocompleteProviderListener {};

void ClipboardProviderTest::OnProviderUpdate(
    bool updated_matches,
    const AutocompleteProvider* provider) {}

TEST_F(ClipboardProviderTest, NotFromOmniboxFocus) {}

TEST_F(ClipboardProviderTest, EmptyClipboard) {}

#if !BUILDFLAG(IS_ANDROID)
// The following tests do not apply to Android.
// On Android, the Omnibox won't access the content of the system clipboard
// before users click the reveal button, so the clipboard suggestions will be
// empty on start.
TEST_F(ClipboardProviderTest, ClipboardIsCurrentURL) {}

TEST_F(ClipboardProviderTest, HasMultipleMatches) {}

TEST_F(ClipboardProviderTest, MatchesUrl) {}

TEST_F(ClipboardProviderTest, MatchesText) {}

TEST_F(ClipboardProviderTest, MatchesImage) {}
#endif  // !BUILDFLAG(IS_ANDROID)

TEST_F(ClipboardProviderTest, DeleteMatch) {}

TEST_F(ClipboardProviderTest, CreateBlankURLMatchOnStart) {}

TEST_F(ClipboardProviderTest, CreateBlankTextMatchOnStart) {}

TEST_F(ClipboardProviderTest, CreateBlankImageMatchOnStart) {}

TEST_F(ClipboardProviderTest, SkipImageMatchGivenWantAsynchronousMatchesFalse) {}

TEST_F(ClipboardProviderTest, CreateURLMatchWithContent) {}

TEST_F(ClipboardProviderTest, CreateTextMatchWithContent) {}

TEST_F(ClipboardProviderTest, CreateImageMatchWithContent) {}

#if BUILDFLAG(IS_ANDROID)
TEST_F(ClipboardProviderTest, Android_MergedWithPZPSGroupOnNTP) {
  SetClipboardText(kClipboardText);
  client_->set_template_url_service(
      search_engines_test_environment_.template_url_service());

  AutocompleteInput input(std::u16string(), metrics::OmniboxEventProto::NTP,
                          classifier_);
  input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_FOCUS);

  provider_->Start(input, false);

  // Expect the clipboard entry, but not the content. Content is not directly
  // available on mobile devices - the user needs to explicitly ask to reveal
  // the content.
  ASSERT_EQ(provider_->matches().size(), 1U);
  const auto& match = provider_->matches().back();
  EXPECT_EQ(AutocompleteMatchType::CLIPBOARD_TEXT, match.type);
  EXPECT_EQ(omnibox::GROUP_PERSONALIZED_ZERO_SUGGEST,
            match.suggestion_group_id);
}

TEST_F(ClipboardProviderTest, Android_StandaloneSuggestionOnSearchActivity) {
  SetClipboardText(kClipboardText);
  client_->set_template_url_service(
      search_engines_test_environment_.template_url_service());

  AutocompleteInput input(std::u16string(),
                          metrics::OmniboxEventProto::ANDROID_SHORTCUTS_WIDGET,
                          classifier_);
  input.set_focus_type(metrics::OmniboxFocusType::INTERACTION_FOCUS);

  provider_->Start(input, false);

  // Expect the clipboard entry, but not the content. Content is not directly
  // available on mobile devices - the user needs to explicitly ask to reveal
  // the content.
  ASSERT_EQ(provider_->matches().size(), 1U);
  const auto& match = provider_->matches().back();
  EXPECT_EQ(AutocompleteMatchType::CLIPBOARD_TEXT, match.type);
  EXPECT_EQ(omnibox::GROUP_MOBILE_CLIPBOARD, match.suggestion_group_id);
}

TEST_F(ClipboardProviderTest, Android_StandaloneSuggestionInNonNTPContext) {
  SetClipboardText(kClipboardText);
  client_->set_template_url_service(
      search_engines_test_environment_.template_url_service());

  AutocompleteInput input =
      CreateAutocompleteInput(metrics::OmniboxFocusType::INTERACTION_FOCUS);

  provider_->Start(input, false);

  // Expect the clipboard entry, but not the content. Content is not directly
  // available on mobile devices - the user needs to explicitly ask to reveal
  // the content.
  ASSERT_EQ(provider_->matches().size(), 1U);
  const auto& match = provider_->matches().back();
  EXPECT_EQ(AutocompleteMatchType::CLIPBOARD_TEXT, match.type);
  EXPECT_EQ(omnibox::GROUP_MOBILE_CLIPBOARD, match.suggestion_group_id);
}
#endif