1 // Copyright (c) 2012 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.
5 #include "testing/gtest/include/gtest/gtest.h"
7 #include "chrome/browser/profiles/profile_dependency_manager.h"
8 #include "chrome/browser/profiles/profile_keyed_service_factory.h"
10 class ProfileDependencyManagerUnittests
: public ::testing::Test
{
12 // To get around class access:
13 void DependOn(ProfileKeyedServiceFactory
* child
,
14 ProfileKeyedServiceFactory
* parent
) {
15 child
->DependsOn(parent
);
18 ProfileDependencyManager
* manager() { return &dependency_manager_
; }
20 std::vector
<std::string
>* shutdown_order() { return &shutdown_order_
; }
23 ProfileDependencyManager dependency_manager_
;
25 std::vector
<std::string
> shutdown_order_
;
28 class TestService
: public ProfileKeyedServiceFactory
{
30 TestService(const std::string
& name
,
31 std::vector
<std::string
>* fill_on_shutdown
,
32 ProfileDependencyManager
* manager
)
33 : ProfileKeyedServiceFactory("TestService", manager
),
35 fill_on_shutdown_(fill_on_shutdown
) {
38 virtual ProfileKeyedService
* BuildServiceInstanceFor(
39 Profile
* profile
) const OVERRIDE
{
40 ADD_FAILURE() << "This isn't part of the tests!";
44 virtual void ProfileShutdown(Profile
* profile
) OVERRIDE
{
45 fill_on_shutdown_
->push_back(name_
);
49 const std::string name_
;
50 std::vector
<std::string
>* fill_on_shutdown_
;
53 // Tests that we can deal with a single component.
54 TEST_F(ProfileDependencyManagerUnittests
, SingleCase
) {
55 TestService
service("service", shutdown_order(), manager());
57 manager()->DestroyProfileServices(NULL
);
59 ASSERT_EQ(1U, shutdown_order()->size());
60 EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
63 // Tests that we get a simple one component depends on the other case.
64 TEST_F(ProfileDependencyManagerUnittests
, SimpleDependency
) {
65 TestService
parent("parent", shutdown_order(), manager());
66 TestService
child("child", shutdown_order(), manager());
67 DependOn(&child
, &parent
);
69 manager()->DestroyProfileServices(NULL
);
71 ASSERT_EQ(2U, shutdown_order()->size());
72 EXPECT_STREQ("child", (*shutdown_order())[0].c_str());
73 EXPECT_STREQ("parent", (*shutdown_order())[1].c_str());
76 // Tests two children, one parent
77 TEST_F(ProfileDependencyManagerUnittests
, TwoChildrenOneParent
) {
78 TestService
parent("parent", shutdown_order(), manager());
79 TestService
child1("child1", shutdown_order(), manager());
80 TestService
child2("child2", shutdown_order(), manager());
81 DependOn(&child1
, &parent
);
82 DependOn(&child2
, &parent
);
84 manager()->DestroyProfileServices(NULL
);
86 ASSERT_EQ(3U, shutdown_order()->size());
87 EXPECT_STREQ("child2", (*shutdown_order())[0].c_str());
88 EXPECT_STREQ("child1", (*shutdown_order())[1].c_str());
89 EXPECT_STREQ("parent", (*shutdown_order())[2].c_str());
92 // Tests an M configuration
93 TEST_F(ProfileDependencyManagerUnittests
, MConfiguration
) {
94 TestService
parent1("parent1", shutdown_order(), manager());
95 TestService
parent2("parent2", shutdown_order(), manager());
97 TestService
child_of_1("child_of_1", shutdown_order(), manager());
98 DependOn(&child_of_1
, &parent1
);
100 TestService
child_of_12("child_of_12", shutdown_order(), manager());
101 DependOn(&child_of_12
, &parent1
);
102 DependOn(&child_of_12
, &parent2
);
104 TestService
child_of_2("child_of_2", shutdown_order(), manager());
105 DependOn(&child_of_2
, &parent2
);
107 manager()->DestroyProfileServices(NULL
);
109 ASSERT_EQ(5U, shutdown_order()->size());
110 EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str());
111 EXPECT_STREQ("child_of_12", (*shutdown_order())[1].c_str());
112 EXPECT_STREQ("child_of_1", (*shutdown_order())[2].c_str());
113 EXPECT_STREQ("parent2", (*shutdown_order())[3].c_str());
114 EXPECT_STREQ("parent1", (*shutdown_order())[4].c_str());
117 // Tests that it can deal with a simple diamond.
118 TEST_F(ProfileDependencyManagerUnittests
, DiamondConfiguration
) {
119 TestService
parent("parent", shutdown_order(), manager());
121 TestService
middle_row_1("middle_row_1", shutdown_order(), manager());
122 DependOn(&middle_row_1
, &parent
);
124 TestService
middle_row_2("middle_row_2", shutdown_order(), manager());
125 DependOn(&middle_row_2
, &parent
);
127 TestService
bottom("bottom", shutdown_order(), manager());
128 DependOn(&bottom
, &middle_row_1
);
129 DependOn(&bottom
, &middle_row_2
);
131 manager()->DestroyProfileServices(NULL
);
133 ASSERT_EQ(4U, shutdown_order()->size());
134 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
135 EXPECT_STREQ("middle_row_2", (*shutdown_order())[1].c_str());
136 EXPECT_STREQ("middle_row_1", (*shutdown_order())[2].c_str());
137 EXPECT_STREQ("parent", (*shutdown_order())[3].c_str());
140 // A final test that works with a more complex graph.
141 TEST_F(ProfileDependencyManagerUnittests
, ComplexGraph
) {
142 TestService
everything_depends_on_me("everything_depends_on_me",
143 shutdown_order(), manager());
145 TestService
intermediary_service("intermediary_service",
146 shutdown_order(), manager());
147 DependOn(&intermediary_service
, &everything_depends_on_me
);
149 TestService
specialized_service("specialized_service",
150 shutdown_order(), manager());
151 DependOn(&specialized_service
, &everything_depends_on_me
);
152 DependOn(&specialized_service
, &intermediary_service
);
154 TestService
other_root("other_root", shutdown_order(), manager());
156 TestService
other_intermediary("other_intermediary",
157 shutdown_order(), manager());
158 DependOn(&other_intermediary
, &other_root
);
160 TestService
bottom("bottom", shutdown_order(), manager());
161 DependOn(&bottom
, &specialized_service
);
162 DependOn(&bottom
, &other_intermediary
);
164 manager()->DestroyProfileServices(NULL
);
166 ASSERT_EQ(6U, shutdown_order()->size());
167 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
168 EXPECT_STREQ("specialized_service", (*shutdown_order())[1].c_str());
169 EXPECT_STREQ("other_intermediary", (*shutdown_order())[2].c_str());
170 EXPECT_STREQ("intermediary_service", (*shutdown_order())[3].c_str());
171 EXPECT_STREQ("other_root", (*shutdown_order())[4].c_str());
172 EXPECT_STREQ("everything_depends_on_me", (*shutdown_order())[5].c_str());