| 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 <string> | 7 #include <string> | 
| 8 #include <vector> | 8 #include <vector> | 
| 9 | 9 | 
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" | 
| 11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" | 
|  | 12 #include "base/json/json_writer.h" | 
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" | 
| 13 #include "base/path_service.h" | 14 #include "base/path_service.h" | 
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" | 
| 15 | 16 | 
| 16 namespace dom_distiller { | 17 namespace dom_distiller { | 
| 17 | 18 | 
| 18 // This test uses input data of core features and the output of the training | 19 // This test uses input data of core features and the output of the training | 
| 19 // pipeline's derived feature extraction to ensure that the extraction that is | 20 // pipeline's derived feature extraction to ensure that the extraction that is | 
| 20 // done in Chromium matches that in the training pipeline. | 21 // done in Chromium matches that in the training pipeline. | 
| 21 TEST(DomDistillerPageFeaturesTest, TestCalculateDerivedFeatures) { | 22 TEST(DomDistillerPageFeaturesTest, TestCalculateDerivedFeatures) { | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 59   for (size_t i = 0; i < derived_features->GetSize(); i += 2) { | 60   for (size_t i = 0; i < derived_features->GetSize(); i += 2) { | 
| 60     std::string label; | 61     std::string label; | 
| 61     ASSERT_TRUE(derived_features->GetString(i, &label)); | 62     ASSERT_TRUE(derived_features->GetString(i, &label)); | 
| 62     labels.push_back(label); | 63     labels.push_back(label); | 
| 63   } | 64   } | 
| 64 | 65 | 
| 65   for (size_t i = 0; i < input_entries->GetSize(); ++i) { | 66   for (size_t i = 0; i < input_entries->GetSize(); ++i) { | 
| 66     base::DictionaryValue* core_features; | 67     base::DictionaryValue* core_features; | 
| 67     ASSERT_TRUE(input_entries->GetDictionary(i, &entry)); | 68     ASSERT_TRUE(input_entries->GetDictionary(i, &entry)); | 
| 68     ASSERT_TRUE(entry->GetDictionary("features", &core_features)); | 69     ASSERT_TRUE(entry->GetDictionary("features", &core_features)); | 
|  | 70     // CalculateDerivedFeaturesFromJSON expects a base::Value of the stringified | 
|  | 71     // JSON (and not a base::Value of the JSON itself) | 
|  | 72     std::string stringified_json; | 
|  | 73     ASSERT_TRUE(base::JSONWriter::Write(core_features, &stringified_json)); | 
|  | 74     scoped_ptr<base::Value> stringified_value( | 
|  | 75         new base::StringValue(stringified_json)); | 
| 69     std::vector<double> derived( | 76     std::vector<double> derived( | 
| 70         CalculateDerivedFeaturesFromJSON(core_features)); | 77         CalculateDerivedFeaturesFromJSON(stringified_value.get())); | 
| 71 | 78 | 
| 72     ASSERT_EQ(labels.size(), derived.size()); | 79     ASSERT_EQ(labels.size(), derived.size()); | 
| 73     ASSERT_TRUE(expected_output_entries->GetDictionary(i, &entry)); | 80     ASSERT_TRUE(expected_output_entries->GetDictionary(i, &entry)); | 
| 74     ASSERT_TRUE(entry->GetList("features", &derived_features)); | 81     ASSERT_TRUE(entry->GetList("features", &derived_features)); | 
| 75     std::string entry_url; | 82     std::string entry_url; | 
| 76     ASSERT_TRUE(entry->GetString("url", &entry_url)); | 83     ASSERT_TRUE(entry->GetString("url", &entry_url)); | 
| 77     for (size_t j = 0, value_index = 1; j < derived.size(); | 84     for (size_t j = 0, value_index = 1; j < derived.size(); | 
| 78          ++j, value_index += 2) { | 85          ++j, value_index += 2) { | 
| 79       double expected_value; | 86       double expected_value; | 
| 80       if (!derived_features->GetDouble(value_index, &expected_value)) { | 87       if (!derived_features->GetDouble(value_index, &expected_value)) { | 
| 81         bool bool_value; | 88         bool bool_value; | 
| 82         ASSERT_TRUE(derived_features->GetBoolean(value_index, &bool_value)); | 89         ASSERT_TRUE(derived_features->GetBoolean(value_index, &bool_value)); | 
| 83         expected_value = bool_value ? 1.0 : 0.0; | 90         expected_value = bool_value ? 1.0 : 0.0; | 
| 84       } | 91       } | 
| 85       EXPECT_DOUBLE_EQ(derived[j], expected_value) | 92       EXPECT_DOUBLE_EQ(derived[j], expected_value) | 
| 86           << "incorrect value for entry with url " << entry_url | 93           << "incorrect value for entry with url " << entry_url | 
| 87           << " for derived feature " << labels[j]; | 94           << " for derived feature " << labels[j]; | 
| 88     } | 95     } | 
| 89   } | 96   } | 
| 90 } | 97 } | 
| 91 } | 98 } | 
| OLD | NEW | 
|---|