| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/dom_distiller/core/page_features.h" | 5 #include "components/dom_distiller/core/page_features.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
| 14 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 19 | 19 |
| 20 namespace dom_distiller { | 20 namespace dom_distiller { |
| 21 | 21 |
| 22 // This test uses input data of core features and the output of the training | 22 // This test uses input data of core features and the output of the training |
| 23 // pipeline's derived feature extraction to ensure that the extraction that is | 23 // pipeline's derived feature extraction to ensure that the extraction that is |
| 24 // done in Chromium matches that in the training pipeline. | 24 // done in Chromium matches that in the training pipeline. |
| 25 TEST(DomDistillerPageFeaturesTest, TestCalculateDerivedFeatures) { | 25 TEST(DomDistillerPageFeaturesTest, TestCalculateDerivedFeatures) { |
| 26 base::FilePath dir_source_root; | 26 base::FilePath dir_source_root; |
| 27 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root)); | 27 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root)); |
| 28 std::string input_data; | 28 std::string input_data; |
| 29 ASSERT_TRUE(base::ReadFileToString( | 29 ASSERT_TRUE(base::ReadFileToString( |
| 30 dir_source_root.AppendASCII( | 30 dir_source_root.AppendASCII( |
| 31 "components/test/data/dom_distiller/core_features.json"), | 31 "components/test/data/dom_distiller/core_features.json"), |
| 32 &input_data)); | 32 &input_data)); |
| 33 std::string expected_output_data; | 33 std::string expected_output_data; |
| 34 // This file contains the output from the calculation of derived features in | 34 // This file contains the output from the calculation of derived features in |
| 35 // the training pipeline. | 35 // the training pipeline. |
| 36 ASSERT_TRUE(base::ReadFileToString( | 36 ASSERT_TRUE(base::ReadFileToString( |
| 37 dir_source_root.AppendASCII( | 37 dir_source_root.AppendASCII( |
| 38 "components/test/data/dom_distiller/derived_features.json"), | 38 "components/test/data/dom_distiller/derived_features.json"), |
| 39 &expected_output_data)); | 39 &expected_output_data)); |
| 40 | 40 |
| 41 scoped_ptr<base::Value> input_json = base::JSONReader::Read(input_data); | 41 std::unique_ptr<base::Value> input_json = base::JSONReader::Read(input_data); |
| 42 ASSERT_TRUE(input_json); | 42 ASSERT_TRUE(input_json); |
| 43 | 43 |
| 44 scoped_ptr<base::Value> expected_output_json = | 44 std::unique_ptr<base::Value> expected_output_json = |
| 45 base::JSONReader::Read(expected_output_data); | 45 base::JSONReader::Read(expected_output_data); |
| 46 ASSERT_TRUE(expected_output_json); | 46 ASSERT_TRUE(expected_output_json); |
| 47 | 47 |
| 48 base::ListValue* input_entries; | 48 base::ListValue* input_entries; |
| 49 ASSERT_TRUE(input_json->GetAsList(&input_entries)); | 49 ASSERT_TRUE(input_json->GetAsList(&input_entries)); |
| 50 ASSERT_GT(input_entries->GetSize(), 0u); | 50 ASSERT_GT(input_entries->GetSize(), 0u); |
| 51 | 51 |
| 52 base::ListValue* expected_output_entries; | 52 base::ListValue* expected_output_entries; |
| 53 ASSERT_TRUE(expected_output_json->GetAsList(&expected_output_entries)); | 53 ASSERT_TRUE(expected_output_json->GetAsList(&expected_output_entries)); |
| 54 ASSERT_EQ(expected_output_entries->GetSize(), input_entries->GetSize()); | 54 ASSERT_EQ(expected_output_entries->GetSize(), input_entries->GetSize()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 67 } | 67 } |
| 68 | 68 |
| 69 for (size_t i = 0; i < input_entries->GetSize(); ++i) { | 69 for (size_t i = 0; i < input_entries->GetSize(); ++i) { |
| 70 base::DictionaryValue* core_features; | 70 base::DictionaryValue* core_features; |
| 71 ASSERT_TRUE(input_entries->GetDictionary(i, &entry)); | 71 ASSERT_TRUE(input_entries->GetDictionary(i, &entry)); |
| 72 ASSERT_TRUE(entry->GetDictionary("features", &core_features)); | 72 ASSERT_TRUE(entry->GetDictionary("features", &core_features)); |
| 73 // CalculateDerivedFeaturesFromJSON expects a base::Value of the stringified | 73 // CalculateDerivedFeaturesFromJSON expects a base::Value of the stringified |
| 74 // JSON (and not a base::Value of the JSON itself) | 74 // JSON (and not a base::Value of the JSON itself) |
| 75 std::string stringified_json; | 75 std::string stringified_json; |
| 76 ASSERT_TRUE(base::JSONWriter::Write(*core_features, &stringified_json)); | 76 ASSERT_TRUE(base::JSONWriter::Write(*core_features, &stringified_json)); |
| 77 scoped_ptr<base::Value> stringified_value( | 77 std::unique_ptr<base::Value> stringified_value( |
| 78 new base::StringValue(stringified_json)); | 78 new base::StringValue(stringified_json)); |
| 79 std::vector<double> derived( | 79 std::vector<double> derived( |
| 80 CalculateDerivedFeaturesFromJSON(stringified_value.get())); | 80 CalculateDerivedFeaturesFromJSON(stringified_value.get())); |
| 81 | 81 |
| 82 ASSERT_EQ(labels.size(), derived.size()); | 82 ASSERT_EQ(labels.size(), derived.size()); |
| 83 ASSERT_TRUE(expected_output_entries->GetDictionary(i, &entry)); | 83 ASSERT_TRUE(expected_output_entries->GetDictionary(i, &entry)); |
| 84 ASSERT_TRUE(entry->GetList("features", &derived_features)); | 84 ASSERT_TRUE(entry->GetList("features", &derived_features)); |
| 85 std::string entry_url; | 85 std::string entry_url; |
| 86 ASSERT_TRUE(entry->GetString("url", &entry_url)); | 86 ASSERT_TRUE(entry->GetString("url", &entry_url)); |
| 87 for (size_t j = 0, value_index = 1; j < derived.size(); | 87 for (size_t j = 0, value_index = 1; j < derived.size(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 EXPECT_EQ(0, lround(derived[7])); | 185 EXPECT_EQ(0, lround(derived[7])); |
| 186 EXPECT_EQ(0, lround(derived[8])); | 186 EXPECT_EQ(0, lround(derived[8])); |
| 187 EXPECT_EQ(10, lround(derived[9])); | 187 EXPECT_EQ(10, lround(derived[9])); |
| 188 EXPECT_EQ(0, lround(derived[10])); | 188 EXPECT_EQ(0, lround(derived[10])); |
| 189 EXPECT_EQ(1, lround(derived[11])); | 189 EXPECT_EQ(1, lround(derived[11])); |
| 190 EXPECT_EQ(0, lround(derived[12])); | 190 EXPECT_EQ(0, lround(derived[12])); |
| 191 EXPECT_EQ(0, lround(derived[13])); | 191 EXPECT_EQ(0, lround(derived[13])); |
| 192 EXPECT_EQ(9, lround(derived[14])); | 192 EXPECT_EQ(9, lround(derived[14])); |
| 193 } | 193 } |
| 194 } | 194 } |
| OLD | NEW |