Home | History | Annotate | Download | only in sync
      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 <algorithm>
      6 #include <vector>
      7 
      8 #include "base/command_line.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/strings/string_util.h"
     11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
     12 #include "chrome/browser/sync/profile_sync_components_factory_impl.h"
     13 #include "chrome/browser/sync/profile_sync_service.h"
     14 #include "chrome/browser/sync/profile_sync_service_android.h"
     15 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
     16 #include "chrome/test/base/testing_profile.h"
     17 #include "components/signin/core/browser/profile_oauth2_token_service.h"
     18 #include "components/sync_driver/data_type_controller.h"
     19 #include "content/public/test/test_browser_thread_bundle.h"
     20 #include "sync/internal_api/public/base/model_type.h"
     21 #include "testing/gtest/include/gtest/gtest.h"
     22 
     23 namespace {
     24 
     25 int NumberOfSetBits(jlong bitmask) {
     26   int num = 0;
     27   while (bitmask > 0) {
     28     num += (bitmask & 1);
     29     bitmask >>= 1;
     30   }
     31   return num;
     32 }
     33 
     34 }  // namespace
     35 
     36 class ProfileSyncServiceAndroidTest : public testing::Test {
     37  public:
     38   ProfileSyncServiceAndroidTest()
     39       : command_line_(base::CommandLine::NO_PROGRAM) {}
     40   virtual ~ProfileSyncServiceAndroidTest() {}
     41 
     42   virtual void SetUp() OVERRIDE {
     43     ProfileOAuth2TokenService* token_service =
     44         ProfileOAuth2TokenServiceFactory::GetForProfile(&profile_);
     45     ProfileSyncComponentsFactory* factory =
     46         new ProfileSyncComponentsFactoryImpl(
     47             &profile_,
     48             &command_line_,
     49             GURL(),
     50             token_service,
     51             profile_.GetRequestContext());
     52     sync_service_.reset(new ProfileSyncService(
     53         make_scoped_ptr(factory),
     54         &profile_,
     55         scoped_ptr<SupervisedUserSigninManagerWrapper>(),
     56         token_service,
     57         browser_sync::MANUAL_START));
     58     factory->RegisterDataTypes(sync_service_.get());
     59   }
     60 
     61  protected:
     62   syncer::ModelTypeSet GetRegisteredDataTypes() {
     63     sync_driver::DataTypeController::StateMap controller_states;
     64     sync_service_->GetDataTypeControllerStates(&controller_states);
     65     syncer::ModelTypeSet model_types;
     66     for (sync_driver::DataTypeController::StateMap::const_iterator it =
     67              controller_states.begin();
     68          it != controller_states.end(); ++it) {
     69       model_types.Put(it->first);
     70     }
     71     return model_types;
     72   }
     73 
     74  private:
     75   base::CommandLine command_line_;
     76   content::TestBrowserThreadBundle thread_bundle_;
     77   TestingProfile profile_;
     78   scoped_ptr<ProfileSyncService> sync_service_;
     79 };
     80 
     81 TEST_F(ProfileSyncServiceAndroidTest, ModelTypesToInvalidationNames) {
     82   syncer::ModelTypeSet model_types = GetRegisteredDataTypes();
     83 
     84   jlong model_type_selection =
     85       ProfileSyncServiceAndroid::ModelTypeSetToSelection(model_types);
     86   // The number of set bits in the model type bitmask should be equal to the
     87   // number of model types.
     88   EXPECT_EQ(static_cast<int>(model_types.Size()),
     89             NumberOfSetBits(model_type_selection));
     90 
     91   std::vector<std::string> invalidation_names;
     92   for (syncer::ModelTypeSet::Iterator it = model_types.First(); it.Good();
     93        it.Inc()) {
     94     std::string notification_type;
     95     if (syncer::RealModelTypeToNotificationType(it.Get(), &notification_type))
     96       invalidation_names.push_back(notification_type);
     97   }
     98 
     99   std::sort(invalidation_names.begin(), invalidation_names.end());
    100   EXPECT_EQ(JoinString(invalidation_names, ", "),
    101             ProfileSyncServiceAndroid::ModelTypeSelectionToStringForTest(
    102                 model_type_selection));
    103 }
    104