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

Side by Side Diff: chrome/browser/profiles/profile_dependency_manager_unittest.cc

Issue 6766004: Create a ProfileDependencyManager to order ProfileKeyedService destruction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix improper usage of static_cast<> in existing mac code. Created 9 years, 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "testing/gtest/include/gtest/gtest.h"
6
7 #include "chrome/browser/profiles/profile_dependency_manager.h"
8 #include "chrome/browser/profiles/profile_keyed_service_factory.h"
9 #include "chrome/test/testing_profile.h"
10
11 class ProfileDependencyManagerUnittests : public ::testing::Test {
12 protected:
13 virtual ~ProfileDependencyManagerUnittests() {
14 EXPECT_TRUE(dependency_manager_.all_components_.empty());
15 EXPECT_TRUE(dependency_manager_.edges_.empty());
16 EXPECT_TRUE(dependency_manager_.destruction_order_.empty());
17 }
18
19 // To get around class access:
20 void DependOn(ProfileKeyedServiceFactory* child,
21 ProfileKeyedServiceFactory* parent) {
22 child->DependsOn(parent);
23 }
24
25 void CreateAndDestroyTestProfile() {
26 TestingProfile profile;
27 profile.SetProfileDependencyManager(&dependency_manager_);
28 }
29
30 ProfileDependencyManager* manager() { return &dependency_manager_; }
31
32 std::vector<std::string>* shutdown_order() { return &shutdown_order_; }
33
34 private:
35 ProfileDependencyManager dependency_manager_;
36
37 std::vector<std::string> shutdown_order_;
38 };
39
40 class TestService : public ProfileKeyedServiceFactory {
41 public:
42 TestService(const std::string& name,
43 std::vector<std::string>* fill_on_shutdown,
44 ProfileDependencyManager* manager)
45 : ProfileKeyedServiceFactory(manager),
46 name_(name),
47 fill_on_shutdown_(fill_on_shutdown) {
48 }
49
50 virtual ProfileKeyedService* BuildServiceInstanceFor(
51 Profile* profile) const {
52 ADD_FAILURE() << "This isn't part of the tests!";
53 return NULL;
54 }
55
56 virtual void ProfileShutdown(Profile* profile) {
57 fill_on_shutdown_->push_back(name_);
58 }
59
60 private:
61 const std::string name_;
62 std::vector<std::string>* fill_on_shutdown_;
63 };
64
65 // Tests that we can deal with a single component.
66 TEST_F(ProfileDependencyManagerUnittests, SingleCase) {
67 TestService service("service", shutdown_order(), manager());
68
69 CreateAndDestroyTestProfile();
70
71 ASSERT_EQ(1U, shutdown_order()->size());
72 EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
73 }
74
75 // Tests that we get a simple one component depends on the other case.
76 TEST_F(ProfileDependencyManagerUnittests, SimpleDependency) {
77 TestService parent("parent", shutdown_order(), manager());
78 TestService child("child", shutdown_order(), manager());
79 DependOn(&child, &parent);
80
81 CreateAndDestroyTestProfile();
82
83 ASSERT_EQ(2U, shutdown_order()->size());
84 EXPECT_STREQ("child", (*shutdown_order())[0].c_str());
85 EXPECT_STREQ("parent", (*shutdown_order())[1].c_str());
86 }
87
88 // Tests two children, one parent
89 TEST_F(ProfileDependencyManagerUnittests, TwoChildrenOneParent) {
90 TestService parent("parent", shutdown_order(), manager());
91 TestService child1("child1", shutdown_order(), manager());
92 TestService child2("child2", shutdown_order(), manager());
93 DependOn(&child1, &parent);
94 DependOn(&child2, &parent);
95
96 CreateAndDestroyTestProfile();
97
98 ASSERT_EQ(3U, shutdown_order()->size());
99 EXPECT_STREQ("child2", (*shutdown_order())[0].c_str());
100 EXPECT_STREQ("child1", (*shutdown_order())[1].c_str());
101 EXPECT_STREQ("parent", (*shutdown_order())[2].c_str());
102 }
103
104 // Tests an M configuration
105 TEST_F(ProfileDependencyManagerUnittests, MConfiguration) {
106 TestService parent1("parent1", shutdown_order(), manager());
107 TestService parent2("parent2", shutdown_order(), manager());
108
109 TestService child_of_1("child_of_1", shutdown_order(), manager());
110 DependOn(&child_of_1, &parent1);
111
112 TestService child_of_12("child_of_12", shutdown_order(), manager());
113 DependOn(&child_of_12, &parent1);
114 DependOn(&child_of_12, &parent2);
115
116 TestService child_of_2("child_of_2", shutdown_order(), manager());
117 DependOn(&child_of_2, &parent2);
118
119 CreateAndDestroyTestProfile();
120
121 ASSERT_EQ(5U, shutdown_order()->size());
122 EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str());
123 EXPECT_STREQ("child_of_12", (*shutdown_order())[1].c_str());
124 EXPECT_STREQ("child_of_1", (*shutdown_order())[2].c_str());
125 EXPECT_STREQ("parent2", (*shutdown_order())[3].c_str());
126 EXPECT_STREQ("parent1", (*shutdown_order())[4].c_str());
127 }
128
129 // Tests that it can deal with a simple diamond.
130 TEST_F(ProfileDependencyManagerUnittests, DiamondConfiguration) {
131 TestService parent("parent", shutdown_order(), manager());
132
133 TestService middle_row_1("middle_row_1", shutdown_order(), manager());
134 DependOn(&middle_row_1, &parent);
135
136 TestService middle_row_2("middle_row_2", shutdown_order(), manager());
137 DependOn(&middle_row_2, &parent);
138
139 TestService bottom("bottom", shutdown_order(), manager());
140 DependOn(&bottom, &middle_row_1);
141 DependOn(&bottom, &middle_row_2);
142
143 CreateAndDestroyTestProfile();
144
145 ASSERT_EQ(4U, shutdown_order()->size());
146 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
147 EXPECT_STREQ("middle_row_2", (*shutdown_order())[1].c_str());
148 EXPECT_STREQ("middle_row_1", (*shutdown_order())[2].c_str());
149 EXPECT_STREQ("parent", (*shutdown_order())[3].c_str());
150 }
151
152 // A final test that works with a more complex graph.
153 TEST_F(ProfileDependencyManagerUnittests, ComplexGraph) {
154 TestService everything_depends_on_me("everything_depends_on_me",
155 shutdown_order(), manager());
156
157 TestService intermediary_service("intermediary_service",
158 shutdown_order(), manager());
159 DependOn(&intermediary_service, &everything_depends_on_me);
160
161 TestService specialized_service("specialized_service",
162 shutdown_order(), manager());
163 DependOn(&specialized_service, &everything_depends_on_me);
164 DependOn(&specialized_service, &intermediary_service);
165
166 TestService other_root("other_root", shutdown_order(), manager());
167
168 TestService other_intermediary("other_intermediary",
169 shutdown_order(), manager());
170 DependOn(&other_intermediary, &other_root);
171
172 TestService bottom("bottom", shutdown_order(), manager());
173 DependOn(&bottom, &specialized_service);
174 DependOn(&bottom, &other_intermediary);
175
176 CreateAndDestroyTestProfile();
177
178 ASSERT_EQ(6U, shutdown_order()->size());
179 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
180 EXPECT_STREQ("specialized_service", (*shutdown_order())[1].c_str());
181 EXPECT_STREQ("other_intermediary", (*shutdown_order())[2].c_str());
182 EXPECT_STREQ("intermediary_service", (*shutdown_order())[3].c_str());
183 EXPECT_STREQ("other_root", (*shutdown_order())[4].c_str());
184 EXPECT_STREQ("everything_depends_on_me", (*shutdown_order())[5].c_str());
185 }
OLDNEW
« no previous file with comments | « chrome/browser/profiles/profile_dependency_manager.cc ('k') | chrome/browser/profiles/profile_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698