summaryrefslogtreecommitdiffstats
path: root/chromeos/system
diff options
context:
space:
mode:
authormnissler <mnissler@chromium.org>2014-10-13 12:52:35 -0700
committerCommit bot <commit-bot@chromium.org>2014-10-13 19:52:53 +0000
commit6e01d07c5ca1a2b711ca494d2320dbe4423e4ee8 (patch)
tree3161b48419a62497bbc14fbe1515847cc1747e46 /chromeos/system
parentbc2ad7606ae6fcd2d83f30154ea623cedc747cfc (diff)
downloadchromium_src-6e01d07c5ca1a2b711ca494d2320dbe4423e4ee8.zip
chromium_src-6e01d07c5ca1a2b711ca494d2320dbe4423e4ee8.tar.gz
chromium_src-6e01d07c5ca1a2b711ca494d2320dbe4423e4ee8.tar.bz2
Replace MockStatisticsProvider with FakeStatisticsProvider.
MockStatisticsProvider is a typical example of gmock only causing excess boilerplate and poor readability. This change implements a proper fake and converts all consumers. BUG=None TEST=unit_tests/browser_tests still compile and pass. Review URL: https://codereview.chromium.org/644413003 Cr-Commit-Position: refs/heads/master@{#299350}
Diffstat (limited to 'chromeos/system')
-rw-r--r--chromeos/system/fake_statistics_provider.cc68
-rw-r--r--chromeos/system/fake_statistics_provider.h57
-rw-r--r--chromeos/system/mock_statistics_provider.cc15
-rw-r--r--chromeos/system/mock_statistics_provider.h36
4 files changed, 125 insertions, 51 deletions
diff --git a/chromeos/system/fake_statistics_provider.cc b/chromeos/system/fake_statistics_provider.cc
new file mode 100644
index 0000000..e9f530b
--- /dev/null
+++ b/chromeos/system/fake_statistics_provider.cc
@@ -0,0 +1,68 @@
+// Copyright 2014 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 "chromeos/system/fake_statistics_provider.h"
+
+namespace chromeos {
+namespace system {
+
+FakeStatisticsProvider::FakeStatisticsProvider() {
+}
+
+FakeStatisticsProvider::~FakeStatisticsProvider() {
+}
+
+void FakeStatisticsProvider::StartLoadingMachineStatistics(
+ const scoped_refptr<base::TaskRunner>& file_task_runner,
+ bool load_oem_manifest) {
+}
+
+bool FakeStatisticsProvider::GetMachineStatistic(const std::string& name,
+ std::string* result) {
+ std::map<std::string, std::string>::const_iterator match =
+ machine_statistics_.find(name);
+ if (match != machine_statistics_.end() && result)
+ *result = match->second;
+ return match != machine_statistics_.end();
+}
+
+bool FakeStatisticsProvider::GetMachineFlag(const std::string& name,
+ bool* result) {
+ std::map<std::string, bool>::const_iterator match = machine_flags_.find(name);
+ if (match != machine_flags_.end() && result)
+ *result = match->second;
+ return match != machine_flags_.end();
+}
+
+void FakeStatisticsProvider::Shutdown() {
+}
+
+void FakeStatisticsProvider::SetMachineStatistic(const std::string& key,
+ const std::string& value) {
+ machine_statistics_[key] = value;
+}
+
+void FakeStatisticsProvider::ClearMachineStatistic(const std::string& key) {
+ machine_statistics_.erase(key);
+}
+
+void FakeStatisticsProvider::SetMachineFlag(const std::string& key,
+ bool value) {
+ machine_flags_[key] = value;
+}
+
+void FakeStatisticsProvider::ClearMachineFlag(const std::string& key) {
+ machine_flags_.erase(key);
+}
+
+ScopedFakeStatisticsProvider::ScopedFakeStatisticsProvider() {
+ StatisticsProvider::SetTestProvider(this);
+}
+
+ScopedFakeStatisticsProvider::~ScopedFakeStatisticsProvider() {
+ StatisticsProvider::SetTestProvider(NULL);
+}
+
+} // namespace system
+} // namespace chromeos
diff --git a/chromeos/system/fake_statistics_provider.h b/chromeos/system/fake_statistics_provider.h
new file mode 100644
index 0000000..7953c40
--- /dev/null
+++ b/chromeos/system/fake_statistics_provider.h
@@ -0,0 +1,57 @@
+// Copyright 2014 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.
+
+#ifndef CHROMEOS_SYSTEM_FAKE_STATISTICS_PROVIDER_H_
+#define CHROMEOS_SYSTEM_FAKE_STATISTICS_PROVIDER_H_
+
+#include <map>
+#include <string>
+
+#include "chromeos/system/statistics_provider.h"
+
+namespace chromeos {
+namespace system {
+
+// A fake StatisticsProvider implementation that is useful in tests.
+class FakeStatisticsProvider : public StatisticsProvider {
+ public:
+ FakeStatisticsProvider();
+ virtual ~FakeStatisticsProvider();
+
+ // StatisticsProvider implementation:
+ virtual void StartLoadingMachineStatistics(
+ const scoped_refptr<base::TaskRunner>& file_task_runner,
+ bool load_oem_manifest) override;
+ virtual bool GetMachineStatistic(const std::string& name,
+ std::string* result) override;
+ virtual bool GetMachineFlag(const std::string& name, bool* result) override;
+ virtual void Shutdown() override;
+
+ void SetMachineStatistic(const std::string& key, const std::string& value);
+ void ClearMachineStatistic(const std::string& key);
+ void SetMachineFlag(const std::string& key, bool value);
+ void ClearMachineFlag(const std::string& key);
+
+ private:
+ std::map<std::string, std::string> machine_statistics_;
+ std::map<std::string, bool> machine_flags_;
+
+ DISALLOW_COPY_AND_ASSIGN(FakeStatisticsProvider);
+};
+
+// A convenience subclass that automatically registers itself as the test
+// StatisticsProvider during construction and cleans up at destruction.
+class ScopedFakeStatisticsProvider : public FakeStatisticsProvider {
+ public:
+ ScopedFakeStatisticsProvider();
+ virtual ~ScopedFakeStatisticsProvider();
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ScopedFakeStatisticsProvider);
+};
+
+} // namespace system
+} // namespace chromeos
+
+#endif // CHROMEOS_SYSTEM_FAKE_STATISTICS_PROVIDER_H_
diff --git a/chromeos/system/mock_statistics_provider.cc b/chromeos/system/mock_statistics_provider.cc
deleted file mode 100644
index 66386d3..0000000
--- a/chromeos/system/mock_statistics_provider.cc
+++ /dev/null
@@ -1,15 +0,0 @@
-// Copyright 2013 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 "chromeos/system/mock_statistics_provider.h"
-
-namespace chromeos {
-namespace system {
-
-MockStatisticsProvider::MockStatisticsProvider() {}
-
-MockStatisticsProvider::~MockStatisticsProvider() {}
-
-} // namespace system
-} // namespace chromeos
diff --git a/chromeos/system/mock_statistics_provider.h b/chromeos/system/mock_statistics_provider.h
deleted file mode 100644
index e0d4e61..0000000
--- a/chromeos/system/mock_statistics_provider.h
+++ /dev/null
@@ -1,36 +0,0 @@
-// Copyright 2013 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.
-
-#ifndef CHROMEOS_SYSTEM_MOCK_STATISTICS_PROVIDER_H_
-#define CHROMEOS_SYSTEM_MOCK_STATISTICS_PROVIDER_H_
-
-#include "base/basictypes.h"
-#include "chromeos/system/statistics_provider.h"
-#include "testing/gmock/include/gmock/gmock.h"
-
-namespace chromeos {
-namespace system {
-
-class CHROMEOS_EXPORT MockStatisticsProvider : public StatisticsProvider {
- public:
- MockStatisticsProvider();
- virtual ~MockStatisticsProvider();
-
- MOCK_METHOD2(StartLoadingMachineStatistics, void(
- const scoped_refptr<base::TaskRunner>&,
- bool));
- MOCK_METHOD2(GetMachineStatistic, bool(const std::string& name,
- std::string* result));
- MOCK_METHOD2(GetMachineFlag, bool(const std::string& name,
- bool* result));
- MOCK_METHOD0(Shutdown, void());
-
- private:
- DISALLOW_COPY_AND_ASSIGN(MockStatisticsProvider);
-};
-
-} // namespace system
-} // namespace chromeos
-
-#endif // CHROMEOS_SYSTEM_MOCK_STATISTICS_PROVIDER_H_