Index: components/previews/previews_data_savings_unittest.cc |
diff --git a/components/previews/previews_data_savings_unittest.cc b/components/previews/previews_data_savings_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9c3547c6927a5f8448fb09ad11f7748717a1aa8e |
--- /dev/null |
+++ b/components/previews/previews_data_savings_unittest.cc |
@@ -0,0 +1,113 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/previews/previews_data_savings.h" |
+ |
+#include <stdint.h> |
+ |
+#include <memory> |
+#include <string> |
+ |
+#include "components/data_reduction_proxy/core/common/data_saver_status.h" |
+#include "components/data_reduction_proxy/core/common/data_savings_recorder.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace previews { |
+ |
+namespace { |
+ |
+class TestDataSavingsRecorder |
+ : public data_reduction_proxy::DataSavingsRecorder { |
+ public: |
+ TestDataSavingsRecorder() : data_used_(0), original_size_(0) {} |
+ ~TestDataSavingsRecorder() {} |
+ |
+ // data_reduction_proxy::DataSavingsRecorder implementation: |
+ void UpdateDataSavings(const std::string& host, |
+ int64_t data_used, |
+ int64_t original_size) override { |
+ last_host_ = host; |
+ data_used_ += data_used; |
+ original_size_ += original_size; |
+ } |
+ |
+ std::string last_host() const { return last_host_; } |
+ |
+ int64_t data_used() const { return data_used_; } |
+ |
+ int64_t original_size() const { return original_size_; } |
+ |
+ private: |
+ std::string last_host_; |
+ int64_t data_used_; |
+ int64_t original_size_; |
+}; |
+ |
+class TestDataSaverStatus : public data_reduction_proxy::DataSaverStatus { |
+ public: |
+ TestDataSaverStatus() : data_saver_enabled_(false) {} |
+ ~TestDataSaverStatus() {} |
+ |
+ // data_reduction_proxy::DataSaverStatus implementation: |
+ bool IsDataSaverEnabled() const override { return data_saver_enabled_; } |
+ |
+ void set_data_saver_enabled(bool data_saver_enabled) { |
+ data_saver_enabled_ = data_saver_enabled; |
+ } |
+ |
+ private: |
+ bool data_saver_enabled_; |
+}; |
+ |
+} // namespace |
+ |
+class PreviewsDataSavingsTest : public testing::Test { |
+ public: |
+ PreviewsDataSavingsTest() |
+ : test_data_savings_recorder_(new TestDataSavingsRecorder()), |
+ test_data_saver_status_(new TestDataSaverStatus()), |
+ data_savings_(new PreviewsDataSavings(test_data_savings_recorder_.get(), |
+ test_data_saver_status_.get())) {} |
+ ~PreviewsDataSavingsTest() override {} |
+ |
+ PreviewsDataSavings* data_savings() const { return data_savings_.get(); } |
+ |
+ TestDataSavingsRecorder* test_data_savings_recorder() const { |
+ return test_data_savings_recorder_.get(); |
+ } |
+ |
+ TestDataSaverStatus* test_data_saver_status() const { |
+ return test_data_saver_status_.get(); |
+ } |
+ |
+ private: |
+ std::unique_ptr<TestDataSavingsRecorder> test_data_savings_recorder_; |
+ std::unique_ptr<TestDataSaverStatus> test_data_saver_status_; |
+ std::unique_ptr<PreviewsDataSavings> data_savings_; |
+}; |
+ |
+TEST_F(PreviewsDataSavingsTest, RecordDataSavings) { |
+ int64_t original_size = 200; |
+ int64_t data_used = 100; |
+ std::string host = "host"; |
+ |
+ EXPECT_EQ(0, test_data_savings_recorder()->data_used()); |
+ EXPECT_EQ(0, test_data_savings_recorder()->original_size()); |
+ test_data_saver_status()->set_data_saver_enabled(false); |
+ |
+ data_savings()->RecordDataSavings(host, data_used, original_size); |
+ |
+ EXPECT_EQ(0, test_data_savings_recorder()->data_used()); |
+ EXPECT_EQ(0, test_data_savings_recorder()->original_size()); |
+ EXPECT_EQ(std::string(), test_data_savings_recorder()->last_host()); |
+ test_data_saver_status()->set_data_saver_enabled(true); |
+ |
+ data_savings()->RecordDataSavings(host, data_used, original_size); |
+ |
+ EXPECT_EQ(data_used, test_data_savings_recorder()->data_used()); |
+ EXPECT_EQ(original_size, test_data_savings_recorder()->original_size()); |
+ EXPECT_EQ(host, test_data_savings_recorder()->last_host()); |
+} |
+ |
+} // namespace previews |