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

content / browser / content_index / content_index_browsertest.cc [blame]

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

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/content_index_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_content_index_provider.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

namespace content {
namespace {

class ContentIndexTest : public ContentBrowserTest {
 public:
  ContentIndexTest() = default;
  ~ContentIndexTest() override = default;

  void SetUpOnMainThread() override {
    ContentBrowserTest::SetUpOnMainThread();
    shell_ = CreateBrowser();

    https_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_->ServeFilesFromSourceDirectory("content/test/data");
    ASSERT_TRUE(https_server_->Start());
    ASSERT_TRUE(NavigateToURL(
        shell_, https_server_->GetURL("/content_index/test.html")));

    provider_ = static_cast<ShellContentIndexProvider*>(
        shell_->web_contents()->GetBrowserContext()->GetContentIndexProvider());
    ASSERT_TRUE(provider_);

    auto* storage_partition =
        shell_->web_contents()->GetBrowserContext()->GetStoragePartition(
            shell_->web_contents()->GetSiteInstance());
    context_ = storage_partition->GetContentIndexContext();
    ASSERT_TRUE(context_);
  }

  void TearDownOnMainThread() override {
    context_ = nullptr;
    provider_ = nullptr;
    shell_ = nullptr;
    ContentBrowserTest::TearDownOnMainThread();
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->AppendSwitch(
        switches::kEnableExperimentalWebPlatformFeatures);
  }

  std::string RunScriptWithResult(const std::string& script) {
    return EvalJs(shell_->web_contents(), script).ExtractString();
  }

  // Runs |script| and expects it to complete successfully.
  void RunScript(const std::string& script) {
    ASSERT_EQ(RunScriptWithResult(script), "ok");
  }

  std::vector<SkBitmap> GetIcons(int64_t service_worker_registration_id,
                                 const std::string& description_id) {
    std::vector<SkBitmap> out_icons;
    base::RunLoop run_loop;
    context_->GetIcons(
        service_worker_registration_id, description_id,
        base::BindLambdaForTesting([&](std::vector<SkBitmap> icons) {
          out_icons = std::move(icons);
          run_loop.Quit();
        }));
    run_loop.Run();
    return out_icons;
  }

  ShellContentIndexProvider* provider() { return provider_; }

 private:
  std::unique_ptr<net::EmbeddedTestServer> https_server_;
  raw_ptr<ShellContentIndexProvider> provider_ = nullptr;
  raw_ptr<ContentIndexContext> context_ = nullptr;
  raw_ptr<Shell> shell_ = nullptr;
};

IN_PROC_BROWSER_TEST_F(ContentIndexTest, GetIcons) {
  {
    // Don't load any icons.
    provider()->set_icon_sizes({});
    RunScript("addContent('id1', [{src: '/single_face.jpg'}])");
    base::RunLoop().RunUntilIdle();

    auto registration_data = provider()->GetRegistrationDataFromId("id1");
    ASSERT_NE(registration_data.first, -1);
    EXPECT_TRUE(GetIcons(registration_data.first, "id1").empty());
  }

  {
    // Load one icon.
    provider()->set_icon_sizes({{42, 42}});
    RunScript("addContent('id2', [{src: '/single_face.jpg'}])");
    base::RunLoop().RunUntilIdle();

    auto registration_data = provider()->GetRegistrationDataFromId("id2");
    ASSERT_NE(registration_data.first, -1);
    auto icons = GetIcons(registration_data.first, "id2");
    ASSERT_EQ(icons.size(), 1u);
    ASSERT_FALSE(icons[0].isNull());
    EXPECT_EQ(icons[0].width(), 42);
    EXPECT_EQ(icons[0].height(), 42);
  }

  {
    // Load two icons.
    provider()->set_icon_sizes({{42, 42}, {24, 24}});
    RunScript("addContent('id3', [{src: '/single_face.jpg'}])");
    base::RunLoop().RunUntilIdle();

    auto registration_data = provider()->GetRegistrationDataFromId("id3");
    ASSERT_NE(registration_data.first, -1);
    auto icons = GetIcons(registration_data.first, "id3");
    ASSERT_EQ(icons.size(), 2u);
    if (icons[0].height() > icons[1].height()) {
      std::swap(icons[0], icons[1]);
    }

    ASSERT_FALSE(icons[0].isNull());
    EXPECT_EQ(icons[0].width(), 24);
    EXPECT_EQ(icons[0].height(), 24);

    ASSERT_FALSE(icons[1].isNull());
    EXPECT_EQ(icons[1].width(), 42);
    EXPECT_EQ(icons[1].height(), 42);
  }
}

IN_PROC_BROWSER_TEST_F(ContentIndexTest, RegistrationWithoutIcons) {
  // Don't load any icons.
  provider()->set_icon_sizes({});
  // Registering without icons is ok since the browser doesn't need any.
  RunScript("addContent('id1')");
}

IN_PROC_BROWSER_TEST_F(ContentIndexTest, BestIconIsChosen) {
  // Load one icon.
  provider()->set_icon_sizes({{42, 42}});

  // If the first resource is chosen, the registration would fail since the
  // resource does not exist.
  RunScript(R"(
    addContent('id3', [
      {
        src: '/MISSING_IMAGE.png',
        sizes: '2x2',
        type: 'image/png',
      },
      {
        src: '/single_face.jpg',
        sizes: '42x42',
        type: 'image/jpg',
      },
    ]))");
}

}  // namespace
}  // namespace content