Commit b105c7cb authored by Ilia Samsonov's avatar Ilia Samsonov Committed by Commit Bot

Use gtest utils to set location data expectation.

The reason for this cl is to avoid setting test's
location data expectation explicitly, which easily changes.
Instead, we use gtest utils to find test's location data.

Bug: 1015157
Change-Id: I6802d476afe68fe872a6fabad3649827d874a4e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1866619Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Ilia Samsonov <isamsonov@google.com>
Cr-Commit-Position: refs/heads/master@{#707615}
parent 2c7e31df
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/test/gtest_util.h"
#include "base/test/launcher/test_result.h" #include "base/test/launcher/test_result.h"
namespace base { namespace base {
...@@ -24,6 +25,18 @@ std::string FindStringKeyOrEmpty(const Value& dict_value, ...@@ -24,6 +25,18 @@ std::string FindStringKeyOrEmpty(const Value& dict_value,
return value ? *value : std::string(); return value ? *value : std::string();
} }
// Find and return test case with name |test_case_name|,
// return null if missing.
const testing::TestCase* GetTestCase(const std::string& test_case_name) {
testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
const testing::TestCase* test_case = unit_test->GetTestCase(i);
if (test_case->name() == test_case_name)
return test_case;
}
return nullptr;
}
} // namespace } // namespace
bool ValidateKeyValue(const Value& dict_value, bool ValidateKeyValue(const Value& dict_value,
...@@ -85,6 +98,24 @@ bool ValidateTestResult(const Value* iteration_data, ...@@ -85,6 +98,24 @@ bool ValidateTestResult(const Value* iteration_data,
return true; return true;
} }
bool ValidateTestLocations(const Value* test_locations,
const std::string& test_case_name) {
const testing::TestCase* test_case = GetTestCase(test_case_name);
if (test_case == nullptr) {
ADD_FAILURE() << "Could not find test case " << test_case_name;
return false;
}
bool result = true;
for (int j = 0; j < test_case->total_test_count(); ++j) {
const testing::TestInfo* test_info = test_case->GetTestInfo(j);
std::string full_name =
FormatFullTestName(test_case->name(), test_info->name());
result &= ValidateTestLocation(test_locations, full_name, test_info->file(),
test_info->line());
}
return result;
}
bool ValidateTestLocation(const Value* test_locations, bool ValidateTestLocation(const Value* test_locations,
const std::string& test_name, const std::string& test_name,
const std::string& file, const std::string& file,
......
...@@ -37,6 +37,10 @@ bool ValidateTestResult(const Value* iteration_data, ...@@ -37,6 +37,10 @@ bool ValidateTestResult(const Value* iteration_data,
const std::string& status, const std::string& status,
size_t result_part_count); size_t result_part_count);
// Validate test_locations contains all tests in |test_case_name|.
bool ValidateTestLocations(const Value* test_locations,
const std::string& test_case_name);
// Validate test_locations contains the correct file name and line number. // Validate test_locations contains the correct file name and line number.
bool ValidateTestLocation(const Value* test_locations, bool ValidateTestLocation(const Value* test_locations,
const std::string& test_name, const std::string& test_name,
......
...@@ -701,17 +701,8 @@ TEST_F(UnitTestLauncherDelegateTester, RunMockTests) { ...@@ -701,17 +701,8 @@ TEST_F(UnitTestLauncherDelegateTester, RunMockTests) {
Value* val = root->FindDictKey("test_locations"); Value* val = root->FindDictKey("test_locations");
ASSERT_TRUE(val); ASSERT_TRUE(val);
EXPECT_EQ(4u, val->DictSize()); EXPECT_EQ(4u, val->DictSize());
// If path or test location changes, the following expectation
// will need to change accordingly. EXPECT_TRUE(test_launcher_utils::ValidateTestLocations(val, "MockUnitTests"));
std::string file_name = "../../base/test/launcher/test_launcher_unittest.cc";
EXPECT_TRUE(test_launcher_utils::ValidateTestLocation(
val, "MockUnitTests.DISABLED_PassTest", file_name, 659));
EXPECT_TRUE(test_launcher_utils::ValidateTestLocation(
val, "MockUnitTests.DISABLED_FailTest", file_name, 663));
EXPECT_TRUE(test_launcher_utils::ValidateTestLocation(
val, "MockUnitTests.DISABLED_CrashTest", file_name, 667));
EXPECT_TRUE(test_launcher_utils::ValidateTestLocation(
val, "MockUnitTests.DISABLED_NoRunTest", file_name, 671));
val = root->FindListKey("per_iteration_data"); val = root->FindListKey("per_iteration_data");
ASSERT_TRUE(val); ASSERT_TRUE(val);
......
...@@ -190,15 +190,8 @@ IN_PROC_BROWSER_TEST_F(ContentBrowserTest, RunMockTests) { ...@@ -190,15 +190,8 @@ IN_PROC_BROWSER_TEST_F(ContentBrowserTest, RunMockTests) {
base::Value* val = root->FindDictKey("test_locations"); base::Value* val = root->FindDictKey("test_locations");
ASSERT_TRUE(val); ASSERT_TRUE(val);
EXPECT_EQ(3u, val->DictSize()); EXPECT_EQ(3u, val->DictSize());
// If path or test location changes, the following expectation EXPECT_TRUE(base::test_launcher_utils::ValidateTestLocations(
// will need to change accordingly. val, "MockContentBrowserTest"));
std::string file_name = "../../content/test/content_browser_test_test.cc";
EXPECT_TRUE(base::test_launcher_utils::ValidateTestLocation(
val, "MockContentBrowserTest.DISABLED_PassTest", file_name, 153));
EXPECT_TRUE(base::test_launcher_utils::ValidateTestLocation(
val, "MockContentBrowserTest.DISABLED_FailTest", file_name, 157));
EXPECT_TRUE(base::test_launcher_utils::ValidateTestLocation(
val, "MockContentBrowserTest.DISABLED_CrashTest", file_name, 161));
val = root->FindListKey("per_iteration_data"); val = root->FindListKey("per_iteration_data");
ASSERT_TRUE(val); ASSERT_TRUE(val);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment