Home | History | Annotate | Download | only in content
      1 // Copyright 2014 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 "components/keyed_service/content/browser_context_dependency_manager.h"
      6 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 class BrowserContextDependencyManagerUnittests : public ::testing::Test {
     10  protected:
     11   // To get around class access:
     12   void DependOn(BrowserContextKeyedServiceFactory* child,
     13                 BrowserContextKeyedServiceFactory* parent) {
     14     child->DependsOn(parent);
     15   }
     16 
     17   BrowserContextDependencyManager* manager() { return &dependency_manager_; }
     18 
     19   std::vector<std::string>* shutdown_order() { return &shutdown_order_; }
     20 
     21  private:
     22   BrowserContextDependencyManager dependency_manager_;
     23 
     24   std::vector<std::string> shutdown_order_;
     25 };
     26 
     27 class TestService : public BrowserContextKeyedServiceFactory {
     28  public:
     29   TestService(const std::string& name,
     30               std::vector<std::string>* fill_on_shutdown,
     31               BrowserContextDependencyManager* manager)
     32       : BrowserContextKeyedServiceFactory("TestService", manager),
     33         name_(name),
     34         fill_on_shutdown_(fill_on_shutdown) {}
     35 
     36   virtual KeyedService* BuildServiceInstanceFor(
     37       content::BrowserContext* context) const OVERRIDE {
     38     ADD_FAILURE() << "This isn't part of the tests!";
     39     return NULL;
     40   }
     41 
     42   virtual void BrowserContextShutdown(content::BrowserContext* context)
     43       OVERRIDE {
     44     fill_on_shutdown_->push_back(name_);
     45   }
     46 
     47  private:
     48   const std::string name_;
     49   std::vector<std::string>* fill_on_shutdown_;
     50 };
     51 
     52 // Tests that we can deal with a single component.
     53 TEST_F(BrowserContextDependencyManagerUnittests, SingleCase) {
     54   TestService service("service", shutdown_order(), manager());
     55 
     56   manager()->DestroyBrowserContextServices(NULL);
     57 
     58   ASSERT_EQ(1U, shutdown_order()->size());
     59   EXPECT_STREQ("service", (*shutdown_order())[0].c_str());
     60 }
     61 
     62 // Tests that we get a simple one component depends on the other case.
     63 TEST_F(BrowserContextDependencyManagerUnittests, SimpleDependency) {
     64   TestService parent("parent", shutdown_order(), manager());
     65   TestService child("child", shutdown_order(), manager());
     66   DependOn(&child, &parent);
     67 
     68   manager()->DestroyBrowserContextServices(NULL);
     69 
     70   ASSERT_EQ(2U, shutdown_order()->size());
     71   EXPECT_STREQ("child", (*shutdown_order())[0].c_str());
     72   EXPECT_STREQ("parent", (*shutdown_order())[1].c_str());
     73 }
     74 
     75 // Tests two children, one parent
     76 TEST_F(BrowserContextDependencyManagerUnittests, TwoChildrenOneParent) {
     77   TestService parent("parent", shutdown_order(), manager());
     78   TestService child1("child1", shutdown_order(), manager());
     79   TestService child2("child2", shutdown_order(), manager());
     80   DependOn(&child1, &parent);
     81   DependOn(&child2, &parent);
     82 
     83   manager()->DestroyBrowserContextServices(NULL);
     84 
     85   ASSERT_EQ(3U, shutdown_order()->size());
     86   EXPECT_STREQ("child2", (*shutdown_order())[0].c_str());
     87   EXPECT_STREQ("child1", (*shutdown_order())[1].c_str());
     88   EXPECT_STREQ("parent", (*shutdown_order())[2].c_str());
     89 }
     90 
     91 // Tests an M configuration
     92 TEST_F(BrowserContextDependencyManagerUnittests, MConfiguration) {
     93   TestService parent1("parent1", shutdown_order(), manager());
     94   TestService parent2("parent2", shutdown_order(), manager());
     95 
     96   TestService child_of_1("child_of_1", shutdown_order(), manager());
     97   DependOn(&child_of_1, &parent1);
     98 
     99   TestService child_of_12("child_of_12", shutdown_order(), manager());
    100   DependOn(&child_of_12, &parent1);
    101   DependOn(&child_of_12, &parent2);
    102 
    103   TestService child_of_2("child_of_2", shutdown_order(), manager());
    104   DependOn(&child_of_2, &parent2);
    105 
    106   manager()->DestroyBrowserContextServices(NULL);
    107 
    108   ASSERT_EQ(5U, shutdown_order()->size());
    109   EXPECT_STREQ("child_of_2", (*shutdown_order())[0].c_str());
    110   EXPECT_STREQ("child_of_12", (*shutdown_order())[1].c_str());
    111   EXPECT_STREQ("child_of_1", (*shutdown_order())[2].c_str());
    112   EXPECT_STREQ("parent2", (*shutdown_order())[3].c_str());
    113   EXPECT_STREQ("parent1", (*shutdown_order())[4].c_str());
    114 }
    115 
    116 // Tests that it can deal with a simple diamond.
    117 TEST_F(BrowserContextDependencyManagerUnittests, DiamondConfiguration) {
    118   TestService parent("parent", shutdown_order(), manager());
    119 
    120   TestService middle_row_1("middle_row_1", shutdown_order(), manager());
    121   DependOn(&middle_row_1, &parent);
    122 
    123   TestService middle_row_2("middle_row_2", shutdown_order(), manager());
    124   DependOn(&middle_row_2, &parent);
    125 
    126   TestService bottom("bottom", shutdown_order(), manager());
    127   DependOn(&bottom, &middle_row_1);
    128   DependOn(&bottom, &middle_row_2);
    129 
    130   manager()->DestroyBrowserContextServices(NULL);
    131 
    132   ASSERT_EQ(4U, shutdown_order()->size());
    133   EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
    134   EXPECT_STREQ("middle_row_2", (*shutdown_order())[1].c_str());
    135   EXPECT_STREQ("middle_row_1", (*shutdown_order())[2].c_str());
    136   EXPECT_STREQ("parent", (*shutdown_order())[3].c_str());
    137 }
    138 
    139 // A final test that works with a more complex graph.
    140 TEST_F(BrowserContextDependencyManagerUnittests, ComplexGraph) {
    141   TestService everything_depends_on_me(
    142       "everything_depends_on_me", shutdown_order(), manager());
    143 
    144   TestService intermediary_service(
    145       "intermediary_service", shutdown_order(), manager());
    146   DependOn(&intermediary_service, &everything_depends_on_me);
    147 
    148   TestService specialized_service(
    149       "specialized_service", shutdown_order(), manager());
    150   DependOn(&specialized_service, &everything_depends_on_me);
    151   DependOn(&specialized_service, &intermediary_service);
    152 
    153   TestService other_root("other_root", shutdown_order(), manager());
    154 
    155   TestService other_intermediary(
    156       "other_intermediary", shutdown_order(), manager());
    157   DependOn(&other_intermediary, &other_root);
    158 
    159   TestService bottom("bottom", shutdown_order(), manager());
    160   DependOn(&bottom, &specialized_service);
    161   DependOn(&bottom, &other_intermediary);
    162 
    163   manager()->DestroyBrowserContextServices(NULL);
    164 
    165   ASSERT_EQ(6U, shutdown_order()->size());
    166   EXPECT_STREQ("bottom", (*shutdown_order())[0].c_str());
    167   EXPECT_STREQ("specialized_service", (*shutdown_order())[1].c_str());
    168   EXPECT_STREQ("other_intermediary", (*shutdown_order())[2].c_str());
    169   EXPECT_STREQ("intermediary_service", (*shutdown_order())[3].c_str());
    170   EXPECT_STREQ("other_root", (*shutdown_order())[4].c_str());
    171   EXPECT_STREQ("everything_depends_on_me", (*shutdown_order())[5].c_str());
    172 }
    173