Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Unified Diff: base/metrics/feature_list_unittest.cc

Issue 1278403003: Initial implementation of FeatureList in base/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More comments addressed. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/metrics/feature_list_unittest.cc
diff --git a/base/metrics/feature_list_unittest.cc b/base/metrics/feature_list_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..29a46d612357e804cb1a02cee40e3629c229effd
--- /dev/null
+++ b/base/metrics/feature_list_unittest.cc
@@ -0,0 +1,96 @@
+// Copyright 2015 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 "base/metrics/feature_list.h"
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+namespace {
+
+const char kFeatureOnName[] = "Example1";
Ilya Sherman 2015/09/01 03:55:26 I think the tests would be easier to follow if thi
Alexei Svitkine (slow) 2015/09/01 15:53:44 Done.
+struct Feature kExampleFeatureOn {
+ kFeatureOnName, true
+};
+
+const char kFeatureOffName[] = "Example2";
+struct Feature kExampleFeatureOff {
+ kFeatureOffName, false
+};
+
+class FeatureListTest : public testing::Test {
Ilya Sherman 2015/09/01 03:55:26 nit: The test harness class should be declared out
Alexei Svitkine (slow) 2015/09/01 15:53:44 Done.
+ public:
+ FeatureListTest() : feature_list_(new FeatureList) {
+ RegisterFeatureListInstance();
+ }
+ ~FeatureListTest() override { ClearFeatureListInstance(); }
+
+ void RegisterFeatureListInstance() {
+ FeatureList::SetInstance(make_scoped_ptr(feature_list_));
+ }
Ilya Sherman 2015/09/01 03:55:26 This doesn't make much sense as a public method if
Alexei Svitkine (slow) 2015/09/01 15:53:44 Done.
+ void ClearFeatureListInstance() { FeatureList::ClearInstanceForTesting(); }
+
+ FeatureList* feature_list() { return feature_list_; }
Ilya Sherman 2015/09/01 03:55:26 This can return a pointer to freed memory if Clear
Alexei Svitkine (slow) 2015/09/01 15:53:44 Done.
+
+ private:
+ // Weak. Owned by the FeatureList::SetInstance().
+ FeatureList* feature_list_;
+
+ DISALLOW_COPY_AND_ASSIGN(FeatureListTest);
+};
+
+} // namespace
+
+TEST_F(FeatureListTest, DefaultStates) {
+ EXPECT_TRUE(FeatureList::IsEnabled(kExampleFeatureOn));
+ EXPECT_FALSE(FeatureList::IsEnabled(kExampleFeatureOff));
+}
+
+TEST_F(FeatureListTest, InitializeFromCommandLine) {
+ struct {
+ const char* enable_features;
+ const char* disable_features;
+ bool expected_feature_on_state;
+ bool expected_feature_off_state;
+ } test_cases[] = {
+ {"", "", true, false},
+ {"Example2", "", true, true},
+ {"Example2", "Example1", false, true},
+ {"Example1,Example2", "", true, true},
+ {"", "Example1,Example2", false, false},
+ // In the case an entry is both, disable takes precedence.
+ {"Example1", "Example1,Example2", false, false},
+ };
+
+ for (size_t i = 0; i < arraysize(test_cases); i++) {
Ilya Sherman 2015/09/01 03:55:26 nit: ++i
Alexei Svitkine (slow) 2015/09/01 15:53:44 Done.
+ const auto& test_case = test_cases[i];
+
+ FeatureList feature_list;
+ feature_list.InitializeFromCommandLine(test_case.enable_features,
+ test_case.disable_features);
+ feature_list.FinalizeInitialization();
Ilya Sherman 2015/09/01 03:55:26 It seems more appropriate to set the singleton, an
Alexei Svitkine (slow) 2015/09/01 15:53:44 Done. I was originally thinking to keep the class
+
+ EXPECT_EQ(test_case.expected_feature_on_state,
+ feature_list.IsFeatureEnabled(kExampleFeatureOn))
+ << i;
+ EXPECT_EQ(test_case.expected_feature_off_state,
+ feature_list.IsFeatureEnabled(kExampleFeatureOff))
+ << i;
+ }
+}
+
+TEST_F(FeatureListTest, CheckFeatureIdentity) {
Ilya Sherman 2015/09/01 03:55:26 This purpose of this test is not immediately obvio
Alexei Svitkine (slow) 2015/09/01 15:53:44 I don't think a test using the public API is possi
Ilya Sherman 2015/09/01 20:17:17 I was thinking we could use a gtest Death Test: [
Alexei Svitkine (slow) 2015/09/01 21:18:38 Right, then the test would have to only be enabled
+ EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kExampleFeatureOn));
+ EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kExampleFeatureOn));
+ EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kExampleFeatureOff));
+ EXPECT_TRUE(feature_list()->CheckFeatureIdentity(kExampleFeatureOff));
Ilya Sherman 2015/09/01 03:55:26 Why are these lines written twice each?
Alexei Svitkine (slow) 2015/09/01 15:53:44 The first time would register it and the second ti
+
+ struct Feature kExampleFeatureOn2 {
+ kFeatureOnName, true
+ };
+ EXPECT_FALSE(feature_list()->CheckFeatureIdentity(kExampleFeatureOn2));
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698