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

ash / assistant / util / resource_util_unittest.cc [blame]

// Copyright 2020 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/assistant/util/resource_util.h"

#include <map>
#include <string>
#include <string_view>

#include "ash/assistant/util/test_support/macros.h"
#include "chromeos/ui/vector_icons/vector_icons.h"
#include "net/base/url_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/canvas.h"
#include "url/gurl.h"

namespace ash {
namespace assistant {
namespace util {

namespace {

// Helpers ---------------------------------------------------------------------

std::string GetParamValue(const GURL& url, const std::string& param_key) {
  if (!url.has_query())
    return std::string();

  const std::string_view query_piece = url.query_piece();
  url::Component query(0, query_piece.length()), key, value;
  while (url::ExtractQueryKeyValue(query_piece, &query, &key, &value)) {
    if (query_piece.substr(key.begin, key.len) == param_key) {
      return std::string(query_piece.substr(value.begin, value.len));
    }
  }

  return std::string();
}

}  // namespace

// Tests -----------------------------------------------------------------------

using ResourceUtilTest = testing::Test;

TEST_F(ResourceUtilTest, AppendOrReplaceColorParam) {
  GURL icon_link("googleassistant://resource?type=icon");

  // Test append.
  icon_link = AppendOrReplaceColorParam(icon_link, SK_ColorBLACK);
  EXPECT_EQ(GetParamValue(icon_link, "color"), "ff000000");

  // Test replace.
  icon_link = AppendOrReplaceColorParam(icon_link, SK_ColorWHITE);
  EXPECT_EQ(GetParamValue(icon_link, "color"), "ffffffff");
}

TEST_F(ResourceUtilTest, CreateIconResourceLink) {
  // Test w/ color.
  EXPECT_EQ(CreateIconResourceLink(IconName::kAssistant, SK_ColorBLACK),
            GURL("googleassistant://"
                 "resource?type=icon&name=assistant&color=ff000000"));
  // Test w/o color.
  EXPECT_EQ(CreateIconResourceLink(IconName::kAssistant),
            GURL("googleassistant://resource?type=icon&name=assistant"));
}

TEST_F(ResourceUtilTest, CreateVectorIcon) {
  std::string url;
  gfx::ImageSkia actual, expected;

  // Test w/ name.
  url = "googleassistant://resource?type=icon&name=assistant";
  actual = CreateVectorIcon(GURL(url));
  expected =
      gfx::CreateVectorIcon(gfx::IconDescription(chromeos::kAssistantIcon));
  ASSERT_PIXELS_EQ(actual, expected);

  // Test w/ name and size.
  url = "googleassistant://resource?type=icon&name=assistant";
  actual = CreateVectorIcon(GURL(url), /*size=*/24);
  expected = gfx::CreateVectorIcon(
      gfx::IconDescription(chromeos::kAssistantIcon, /*size=*/24));
  ASSERT_PIXELS_EQ(actual, expected);

  // Test w/ name, size, and color.
  url = "googleassistant://resource?type=icon&name=assistant&color=ff000000";
  actual = CreateVectorIcon(GURL(url), /*size=*/24);
  expected = gfx::CreateVectorIcon(gfx::IconDescription(
      chromeos::kAssistantIcon, /*size=*/24, /*color=*/SK_ColorBLACK));
  ASSERT_PIXELS_EQ(actual, expected);

  // Test w/o icon resource link.
  actual = CreateVectorIcon(GURL("googleassistant://resource"));
  EXPECT_TRUE(actual.isNull());

  // Test w/o resource link.
  actual = CreateVectorIcon(GURL("https://g.co/"));
  EXPECT_TRUE(actual.isNull());

  // Test w/ empty.
  actual = CreateVectorIcon(GURL());
  EXPECT_TRUE(actual.isNull());
}

TEST_F(ResourceUtilTest, GetResourceLinkType) {
  const std::map<std::string, ResourceLinkType> test_cases = {
      // SUPPORTED:
      {"googleassistant://resource?type=icon", ResourceLinkType::kIcon},

      // UNSUPPORTED:
      {"googleassistant://resource", ResourceLinkType::kUnsupported},
      {"GOOGLEASSISTANT://RESOURCE?TYPE=ICON", ResourceLinkType::kUnsupported},
      {"https://g.co/", ResourceLinkType::kUnsupported},
      {"", ResourceLinkType::kUnsupported},
  };
  for (const auto& test_case : test_cases)
    EXPECT_EQ(GetResourceLinkType(GURL(test_case.first)), test_case.second);
}

TEST_F(ResourceUtilTest, IsResourceLinkType) {
  const std::map<std::string, ResourceLinkType> test_cases = {
      // SUPPORTED:
      {"googleassistant://resource?type=icon", ResourceLinkType::kIcon},

      // UNSUPPORTED:
      {"googleassistant://resource", ResourceLinkType::kUnsupported},
      {"GOOGLEASSISTANT://RESOURCE?TYPE=ICON", ResourceLinkType::kUnsupported},
      {"https://g.co/", ResourceLinkType::kUnsupported},
      {"", ResourceLinkType::kUnsupported},
  };
  for (const auto& test_case : test_cases)
    EXPECT_TRUE(IsResourceLinkType(GURL(test_case.first), test_case.second));
}

TEST_F(ResourceUtilTest, IsResourceLinkUrl) {
  const std::map<std::string, bool> test_cases = {
      // RESOURCE LINK:
      {"googleassistant://resource", true},
      {"googleassistant://resource?type=icon", true},

      // NOT RESOURCE LINK:
      {"GOOGLEASSISTANT://RESOURCE", false},
      {"https://g.co/", false},
      {"", false},
  };
  for (const auto& test_case : test_cases)
    EXPECT_EQ(IsResourceLinkUrl(GURL(test_case.first)), test_case.second);
}

}  // namespace util
}  // namespace assistant
}  // namespace ash