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

base / test / gtest_xml_unittest_result_printer_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 "base/test/gtest_xml_unittest_result_printer.h"

#include "base/base64.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/strings/strcat.h"
#include "base/test/test_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

class XmlUnitTestResultPrinterTest : public ::testing::Test {
 public:
  void SetUp() override {
    if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
            switches::kTestLauncherOutput)) {
      GTEST_SKIP() << "XmlUnitTestResultPrinterTest is not initialized "
                   << "for single process tests.";
    }
  }
};

TEST_F(XmlUnitTestResultPrinterTest, LinkInXmlFile) {
  XmlUnitTestResultPrinter::Get()->AddLink("unique_link", "http://google.com");
  std::string file_path =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kTestLauncherOutput);
  std::string content;
  ASSERT_TRUE(
      base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
  std::string expected_content =
      base::StrCat({"<link name=\"LinkInXmlFile\" "
                    "classname=\"XmlUnitTestResultPrinterTest\" "
                    "link_name=\"unique_link\">",
                    "http://google.com", "</link>"});
  EXPECT_TRUE(content.find(expected_content) != std::string::npos)
      << expected_content << " not found in " << content;
}

TEST_F(XmlUnitTestResultPrinterTest, EscapedLinkInXmlFile) {
  XmlUnitTestResultPrinter::Get()->AddLink(
      "unique_link", "http://google.com/path?id=\"'<>&\"");
  std::string file_path =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kTestLauncherOutput);
  std::string content;
  ASSERT_TRUE(
      base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
  std::string expected_content = base::StrCat(
      {"<link name=\"EscapedLinkInXmlFile\" "
       "classname=\"XmlUnitTestResultPrinterTest\" "
       "link_name=\"unique_link\">",
       "http://google.com/path?id="'<>&"", "</link>"});
  EXPECT_TRUE(content.find(expected_content) != std::string::npos)
      << expected_content << " not found in " << content;
}

TEST_F(XmlUnitTestResultPrinterTest, TagInXmlFile) {
  XmlUnitTestResultPrinter::Get()->AddTag("tag_name", "tag_value");
  std::string file_path =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kTestLauncherOutput);
  std::string content;
  ASSERT_TRUE(
      base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
  std::string expected_content =
      base::StrCat({"<tag name=\"TagInXmlFile\" "
                    "classname=\"XmlUnitTestResultPrinterTest\" "
                    "tag_name=\"tag_name\">",
                    "tag_value", "</tag>"});
  EXPECT_TRUE(content.find(expected_content) != std::string::npos)
      << expected_content << " not found in " << content;
}

TEST_F(XmlUnitTestResultPrinterTest, MultiTagsInXmlFile) {
  XmlUnitTestResultPrinter::Get()->AddTag("tag_name1", "tag_value1");
  XmlUnitTestResultPrinter::Get()->AddTag("tag_name2", "tag_value2");
  std::string file_path =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kTestLauncherOutput);
  std::string content;
  ASSERT_TRUE(
      base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
  std::string expected_content_1 =
      "<tag name=\"MultiTagsInXmlFile\" "
      "classname=\"XmlUnitTestResultPrinterTest\" "
      "tag_name=\"tag_name1\">tag_value1</tag>";
  EXPECT_TRUE(content.find(expected_content_1) != std::string::npos)
      << expected_content_1 << " not found in " << content;

  std::string expected_content_2 =
      "<tag name=\"MultiTagsInXmlFile\" "
      "classname=\"XmlUnitTestResultPrinterTest\" "
      "tag_name=\"tag_name2\">tag_value2</tag>";
  EXPECT_TRUE(content.find(expected_content_2) != std::string::npos)
      << expected_content_2 << " not found in " << content;
}

TEST_F(XmlUnitTestResultPrinterTest, MultiTagsWithSameNameInXmlFile) {
  XmlUnitTestResultPrinter::Get()->AddTag("tag_name", "tag_value1");
  XmlUnitTestResultPrinter::Get()->AddTag("tag_name", "tag_value2");
  std::string file_path =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kTestLauncherOutput);
  std::string content;
  ASSERT_TRUE(
      base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
  std::string expected_content_1 =
      "<tag name=\"MultiTagsWithSameNameInXmlFile\" "
      "classname=\"XmlUnitTestResultPrinterTest\" "
      "tag_name=\"tag_name\">tag_value1</tag>";
  EXPECT_TRUE(content.find(expected_content_1) != std::string::npos)
      << expected_content_1 << " not found in " << content;

  std::string expected_content_2 =
      "<tag name=\"MultiTagsWithSameNameInXmlFile\" "
      "classname=\"XmlUnitTestResultPrinterTest\" "
      "tag_name=\"tag_name\">tag_value2</tag>";
  EXPECT_TRUE(content.find(expected_content_2) != std::string::npos)
      << expected_content_2 << " not found in " << content;
}

class XmlUnitTestResultPrinterTimestampTest : public ::testing::Test {
 public:
  static void TearDownTestSuite() {
    // <testcase ...> should generated after test case finishes. After
    // TearDown().
    std::string file_path =
        base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
            switches::kTestLauncherOutput);
    if (file_path.empty()) {
      GTEST_SKIP() << "Test has to run with --" << switches::kTestLauncherOutput
                   << " switch.";
    }
    std::string content;
    ASSERT_TRUE(
        base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
    EXPECT_THAT(content, ::testing::ContainsRegex("<testcase.*timestamp="));
  }
};

TEST_F(XmlUnitTestResultPrinterTimestampTest, TimestampInXmlFile) {
  // <x-teststart ... /> should generated at this point
  std::string file_path =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kTestLauncherOutput);
  if (file_path.empty()) {
    GTEST_SKIP() << "Test has to run with --" << switches::kTestLauncherOutput
                 << " switch.";
  }
  std::string content;
  ASSERT_TRUE(
      base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
  EXPECT_THAT(content, ::testing::ContainsRegex("<x-teststart.*timestamp="));
}

}  // namespace base