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 "components/browser_context_keyed_service/browser_context_dependency_manager.h"
8 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h"
10 class BrowserContextDependencyManagerUnittests
: public ::testing::Test
{
12 // To get around class access:
13 void DependOn(BrowserContextKeyedServiceFactory
* child
,
14 BrowserContextKeyedServiceFactory
* parent
) {
15 child
->DependsOn(parent
);
18 BrowserContextDependencyManager
* manager() { return &dependency_manager_
; }
20 std::vector
<std::string
>* shutdown_order() { return &shutdown_order_
; }
23 BrowserContextDependencyManager dependency_manager_
;
25 std::vector
<std::string
> shutdown_order_
;
28 class TestService
: public BrowserContextKeyedServiceFactory
{
30 TestService(const std::string
& name
,
31 std::vector
<std::string
>* fill_on_shutdown
,
32 BrowserContextDependencyManager
* manager
)
33 : BrowserContextKeyedServiceFactory("TestService", manager
),
35 fill_on_shutdown_(fill_on_shutdown
) {
38 virtual BrowserContextKeyedService
* BuildServiceInstanceFor(
39 content::BrowserContext
* context
) const OVERRIDE
{
40 ADD_FAILURE() << "This isn't part of the tests!";
44 virtual void BrowserContextShutdown(
45 content::BrowserContext
* context
) OVERRIDE
{
46 fill_on_shutdown_
->push_back(name_
);
50 const std::string name_
;
51 std::vector
<std::string
>* fill_on_shutdown_
;
54 // Tests that we can deal with a single component.
55 TEST_F(BrowserContextDependencyManagerUnittests
, SingleCase
) {
56 TestService
service("service", shutdown_order(), manager());
58 manager()->DestroyBrowserContextServices(NULL
);
60 ASSERT_EQ(1U, shutdown_order()->size());
61 EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
64 // Tests that we get a simple one component depends on the other case.
65 TEST_F(BrowserContextDependencyManagerUnittests
, SimpleDependency
) {
66 TestService
parent("parent", shutdown_order(), manager());
67 TestService
child("child", shutdown_order(), manager());
68 DependOn(&child
, &parent
);
70 manager()->DestroyBrowserContextServices(NULL
);
72 ASSERT_EQ(2U, shutdown_order()->size());
73 EXPECT_STREQ("child", (*shutdown_order())[0].c_str());
74 EXPECT_STREQ("parent", (*shutdown_order())[1].c_str());
77 // Tests two children, one parent
78 TEST_F(BrowserContextDependencyManagerUnittests
, TwoChildrenOneParent
) {
79 TestService
parent("parent", shutdown_order(), manager());
80 TestService
child1("child1", shutdown_order(), manager());
81 TestService
child2("child2", shutdown_order(), manager());
82 DependOn(&child1
, &parent
);
83 DependOn(&child2
, &parent
);
85 manager()->DestroyBrowserContextServices(NULL
);
87 ASSERT_EQ(3U, shutdown_order()->size());
88 EXPECT_STREQ("child2", (*shutdown_order())[0].c_str());
89 EXPECT_STREQ("child1", (*shutdown_order())[1].c_str());
90 EXPECT_STREQ("parent", (*shutdown_order())[2].c_str());
93 // Tests an M configuration
94 TEST_F(BrowserContextDependencyManagerUnittests
, MConfiguration
) {
95 TestService
parent1("parent1", shutdown_order(), manager());
96 TestService
parent2("parent2", shutdown_order(), manager());
98 TestService
child_of_1("child_of_1", shutdown_order(), manager());
99 DependOn(&child_of_1
, &parent1
);
101 TestService
child_of_12("child_of_12", shutdown_order(), manager());
102 DependOn(&child_of_12
, &parent1
);
103 DependOn(&child_of_12
, &parent2
);
105 TestService
child_of_2("child_of_2", shutdown_order(), manager());
106 DependOn(&child_of_2
, &parent2
);
108 manager()->DestroyBrowserContextServices(NULL
);
110 ASSERT_EQ(5U, shutdown_order()->size());
111 EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str());
112 EXPECT_STREQ("child_of_12", (*shutdown_order())[1].c_str());
113 EXPECT_STREQ("child_of_1", (*shutdown_order())[2].c_str());
114 EXPECT_STREQ("parent2", (*shutdown_order())[3].c_str());
115 EXPECT_STREQ("parent1", (*shutdown_order())[4].c_str());
118 // Tests that it can deal with a simple diamond.
119 TEST_F(BrowserContextDependencyManagerUnittests
, DiamondConfiguration
) {
120 TestService
parent("parent", shutdown_order(), manager());
122 TestService
middle_row_1("middle_row_1", shutdown_order(), manager());
123 DependOn(&middle_row_1
, &parent
);
125 TestService
middle_row_2("middle_row_2", shutdown_order(), manager());
126 DependOn(&middle_row_2
, &parent
);
128 TestService
bottom("bottom", shutdown_order(), manager());
129 DependOn(&bottom
, &middle_row_1
);
130 DependOn(&bottom
, &middle_row_2
);
132 manager()->DestroyBrowserContextServices(NULL
);
134 ASSERT_EQ(4U, shutdown_order()->size());
135 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
136 EXPECT_STREQ("middle_row_2", (*shutdown_order())[1].c_str());
137 EXPECT_STREQ("middle_row_1", (*shutdown_order())[2].c_str());
138 EXPECT_STREQ("parent", (*shutdown_order())[3].c_str());
141 // A final test that works with a more complex graph.
142 TEST_F(BrowserContextDependencyManagerUnittests
, ComplexGraph
) {
143 TestService
everything_depends_on_me("everything_depends_on_me",
144 shutdown_order(), manager());
146 TestService
intermediary_service("intermediary_service",
147 shutdown_order(), manager());
148 DependOn(&intermediary_service
, &everything_depends_on_me
);
150 TestService
specialized_service("specialized_service",
151 shutdown_order(), manager());
152 DependOn(&specialized_service
, &everything_depends_on_me
);
153 DependOn(&specialized_service
, &intermediary_service
);
155 TestService
other_root("other_root", shutdown_order(), manager());
157 TestService
other_intermediary("other_intermediary",
158 shutdown_order(), manager());
159 DependOn(&other_intermediary
, &other_root
);
161 TestService
bottom("bottom", shutdown_order(), manager());
162 DependOn(&bottom
, &specialized_service
);
163 DependOn(&bottom
, &other_intermediary
);
165 manager()->DestroyBrowserContextServices(NULL
);
167 ASSERT_EQ(6U, shutdown_order()->size());
168 EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
169 EXPECT_STREQ("specialized_service", (*shutdown_order())[1].c_str());
170 EXPECT_STREQ("other_intermediary", (*shutdown_order())[2].c_str());
171 EXPECT_STREQ("intermediary_service", (*shutdown_order())[3].c_str());
172 EXPECT_STREQ("other_root", (*shutdown_order())[4].c_str());
173 EXPECT_STREQ("everything_depends_on_me", (*shutdown_order())[5].c_str());