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

ash / quick_insert / quick_insert_insert_media_unittest.cc [blame]

// Copyright 2024 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/quick_insert/quick_insert_insert_media.h"

#include <optional>
#include <string>

#include "ash/quick_insert/quick_insert_rich_media.h"
#include "ash/quick_insert/quick_insert_web_paste_target.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/callback_helpers.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ime/fake_text_input_client.h"

namespace ash {
namespace {

class ScopedTestFile {
 public:
  bool Create(std::string_view file_name, std::string_view contents) {
    if (!temp_dir_.CreateUniqueTempDir()) {
      return false;
    }
    path_ = temp_dir_.GetPath().Append(file_name);
    if (!base::WriteFile(path_, contents)) {
      return false;
    }
    return true;
  }

  const base::FilePath& path() const { return path_; }

 private:
  base::ScopedTempDir temp_dir_;
  base::FilePath path_;
};

struct TestCase {
  // The media to insert.
  QuickInsertRichMedia media_to_insert;

  // The expected text in the input field if the insertion was successful.
  std::u16string expected_text;

  // The expected image in the input field if the insertion was successful.
  std::optional<GURL> expected_image_url;
};

using QuickInsertInsertMediaTest = testing::TestWithParam<TestCase>;

TEST_P(QuickInsertInsertMediaTest, SupportsInsertingMedia) {
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = true});

  EXPECT_TRUE(
      InputFieldSupportsInsertingMedia(GetParam().media_to_insert, client));
}

TEST_P(QuickInsertInsertMediaTest, InsertsMediaWithNoError) {
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = true});

  base::test::TestFuture<InsertMediaResult> future;
  InsertMediaToInputField(GetParam().media_to_insert, client,
                          /*get_web_paste_target=*/{}, future.GetCallback());

  EXPECT_EQ(future.Get(), InsertMediaResult::kSuccess);
  EXPECT_EQ(client.text(), GetParam().expected_text);
  EXPECT_EQ(client.last_inserted_image_url(), GetParam().expected_image_url);
}

INSTANTIATE_TEST_SUITE_P(
    ,
    QuickInsertInsertMediaTest,
    testing::Values(
        TestCase{
            .media_to_insert = QuickInsertTextMedia(u"hello"),
            .expected_text = u"hello",
        },
        TestCase{
            .media_to_insert =
                QuickInsertImageMedia(GURL("http://foo.com/fake.jpg"),
                                      gfx::Size(10, 10)),
            .expected_image_url = GURL("http://foo.com/fake.jpg"),
        },
        TestCase{
            .media_to_insert = QuickInsertLinkMedia(GURL("http://foo.com"),
                                                    "foo"),
            .expected_text = u"http://foo.com/",
        }));

TEST(QuickInsertInsertImageMediaTest, UnsupportedInputField) {
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = false});

  EXPECT_FALSE(InputFieldSupportsInsertingMedia(
      QuickInsertImageMedia(GURL("http://foo.com"), gfx::Size(10, 10)),
      client));
}

TEST(QuickInsertInsertImageMediaTest,
     InsertingUnsupportedInputFieldFailsAsynchronously) {
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = false});

  base::test::TestFuture<InsertMediaResult> future;
  InsertMediaToInputField(
      QuickInsertImageMedia(GURL("http://foo.com"), gfx::Size(10, 10)), client,
      /*get_web_paste_target=*/{}, future.GetCallback());

  EXPECT_EQ(future.Get(), InsertMediaResult::kUnsupported);
  EXPECT_EQ(client.last_inserted_image_url(), std::nullopt);
}

TEST(QuickInsertInsertLocalFileMediaTest, SupportedInputField) {
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = true});

  EXPECT_TRUE(InputFieldSupportsInsertingMedia(
      QuickInsertLocalFileMedia(base::FilePath("foo.txt")), client));
}

TEST(QuickInsertInsertLocalFileMediaTest, UnsupportedInputField) {
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = false});

  EXPECT_FALSE(InputFieldSupportsInsertingMedia(
      QuickInsertLocalFileMedia(base::FilePath("foo.txt")), client));
}

TEST(QuickInsertInsertLocalFileMediaTest, InsertsAsynchronously) {
  base::test::TaskEnvironment task_environment;
  ScopedTestFile file;
  ASSERT_TRUE(file.Create("foo.png", "Test data"));
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = true});

  base::test::TestFuture<InsertMediaResult> future;
  InsertMediaToInputField(QuickInsertLocalFileMedia(file.path()), client,
                          /*get_web_paste_target=*/{}, future.GetCallback());

  EXPECT_EQ(future.Get(), InsertMediaResult::kSuccess);
  EXPECT_EQ(client.text(), u"");
  EXPECT_EQ(client.last_inserted_image_url(),
            GURL("data:image/png;base64,VGVzdCBkYXRh"));
}

TEST(QuickInsertInsertLocalFileMediaTest,
     InsertingInUnsupportedClientReturnsError) {
  base::test::TaskEnvironment task_environment;
  ScopedTestFile file;
  ASSERT_TRUE(file.Create("foo.png", "Test data"));
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = false});

  base::test::TestFuture<InsertMediaResult> future;
  InsertMediaToInputField(QuickInsertLocalFileMedia(file.path()), client,
                          /*get_web_paste_target=*/{}, future.GetCallback());

  EXPECT_EQ(future.Get(), InsertMediaResult::kUnsupported);
  EXPECT_EQ(client.text(), u"");
  EXPECT_EQ(client.last_inserted_image_url(), std::nullopt);
}

TEST(QuickInsertInsertLocalFileMediaTest,
     InsertingUnsupportedMediaTypeReturnsError) {
  base::test::TaskEnvironment task_environment;
  ScopedTestFile file;
  ASSERT_TRUE(file.Create("foo.meow", "Test data"));
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = true});

  base::test::TestFuture<InsertMediaResult> future;
  InsertMediaToInputField(QuickInsertLocalFileMedia(file.path()), client,
                          /*get_web_paste_target=*/{}, future.GetCallback());

  EXPECT_EQ(future.Get(), InsertMediaResult::kUnsupported);
  EXPECT_EQ(client.text(), u"");
  EXPECT_EQ(client.last_inserted_image_url(), std::nullopt);
}

TEST(QuickInsertInsertLocalFileMediaTest,
     InsertingNonExistentFileReturnsError) {
  base::test::TaskEnvironment task_environment;
  ui::FakeTextInputClient client(
      {.type = ui::TEXT_INPUT_TYPE_TEXT, .can_insert_image = true});

  base::test::TestFuture<InsertMediaResult> future;
  InsertMediaToInputField(QuickInsertLocalFileMedia(base::FilePath("foo.txt")),
                          client,
                          /*get_web_paste_target=*/{}, future.GetCallback());

  EXPECT_EQ(future.Get(), InsertMediaResult::kNotFound);
  EXPECT_EQ(client.text(), u"");
  EXPECT_EQ(client.last_inserted_image_url(), std::nullopt);
}

}  // namespace
}  // namespace ash