Home | History | Annotate | Download | only in internal_api
      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.
      4 
      5 #include "sync/internal_api/js_sync_manager_observer.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/location.h"
      9 #include "base/message_loop/message_loop.h"
     10 #include "base/values.h"
     11 #include "sync/internal_api/public/base/model_type.h"
     12 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
     13 #include "sync/internal_api/public/util/sync_string_conversions.h"
     14 #include "sync/internal_api/public/util/weak_handle.h"
     15 #include "sync/js/js_event_details.h"
     16 #include "sync/js/js_test_util.h"
     17 #include "sync/protocol/sync_protocol_error.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 
     20 namespace syncer {
     21 namespace {
     22 
     23 using ::testing::InSequence;
     24 using ::testing::StrictMock;
     25 
     26 class JsSyncManagerObserverTest : public testing::Test {
     27  protected:
     28   JsSyncManagerObserverTest() {
     29     js_sync_manager_observer_.SetJsEventHandler(
     30         mock_js_event_handler_.AsWeakHandle());
     31   }
     32 
     33  private:
     34   // This must be destroyed after the member variables below in order
     35   // for WeakHandles to be destroyed properly.
     36   base::MessageLoop message_loop_;
     37 
     38  protected:
     39   StrictMock<MockJsEventHandler> mock_js_event_handler_;
     40   JsSyncManagerObserver js_sync_manager_observer_;
     41 
     42   void PumpLoop() {
     43     message_loop_.RunUntilIdle();
     44   }
     45 };
     46 
     47 TEST_F(JsSyncManagerObserverTest, NoArgNotifiations) {
     48   InSequence dummy;
     49 
     50   EXPECT_CALL(mock_js_event_handler_,
     51               HandleJsEvent("onStopSyncingPermanently",
     52                             HasDetails(JsEventDetails())));
     53 
     54   js_sync_manager_observer_.OnStopSyncingPermanently();
     55   PumpLoop();
     56 }
     57 
     58 TEST_F(JsSyncManagerObserverTest, OnInitializationComplete) {
     59   base::DictionaryValue expected_details;
     60   syncer::ModelTypeSet restored_types;
     61   restored_types.Put(BOOKMARKS);
     62   restored_types.Put(NIGORI);
     63   expected_details.Set("restoredTypes", ModelTypeSetToValue(restored_types));
     64 
     65   EXPECT_CALL(mock_js_event_handler_,
     66               HandleJsEvent("onInitializationComplete",
     67                             HasDetailsAsDictionary(expected_details)));
     68 
     69   js_sync_manager_observer_.OnInitializationComplete(
     70       WeakHandle<JsBackend>(),
     71       WeakHandle<DataTypeDebugInfoListener>(),
     72       true,
     73       restored_types);
     74   PumpLoop();
     75 }
     76 
     77 TEST_F(JsSyncManagerObserverTest, OnSyncCycleCompleted) {
     78   sessions::SyncSessionSnapshot snapshot(
     79       sessions::ModelNeutralState(),
     80       ProgressMarkerMap(),
     81       false,
     82       5,
     83       2,
     84       7,
     85       false,
     86       0,
     87       base::Time::Now(),
     88       std::vector<int>(MODEL_TYPE_COUNT, 0),
     89       std::vector<int>(MODEL_TYPE_COUNT, 0),
     90       sync_pb::GetUpdatesCallerInfo::UNKNOWN);
     91   base::DictionaryValue expected_details;
     92   expected_details.Set("snapshot", snapshot.ToValue());
     93 
     94   EXPECT_CALL(mock_js_event_handler_,
     95               HandleJsEvent("onSyncCycleCompleted",
     96                             HasDetailsAsDictionary(expected_details)));
     97 
     98   js_sync_manager_observer_.OnSyncCycleCompleted(snapshot);
     99   PumpLoop();
    100 }
    101 
    102 TEST_F(JsSyncManagerObserverTest, OnActionableError) {
    103   SyncProtocolError sync_error;
    104   sync_error.action = CLEAR_USER_DATA_AND_RESYNC;
    105   sync_error.error_type = TRANSIENT_ERROR;
    106   base::DictionaryValue expected_details;
    107   expected_details.Set("syncError", sync_error.ToValue());
    108 
    109   EXPECT_CALL(mock_js_event_handler_,
    110               HandleJsEvent("onActionableError",
    111                            HasDetailsAsDictionary(expected_details)));
    112 
    113   js_sync_manager_observer_.OnActionableError(sync_error);
    114   PumpLoop();
    115 }
    116 
    117 
    118 TEST_F(JsSyncManagerObserverTest, OnConnectionStatusChange) {
    119   const ConnectionStatus kStatus = CONNECTION_AUTH_ERROR;
    120   base::DictionaryValue expected_details;
    121   expected_details.SetString("status",
    122                              ConnectionStatusToString(kStatus));
    123 
    124   EXPECT_CALL(mock_js_event_handler_,
    125               HandleJsEvent("onConnectionStatusChange",
    126                             HasDetailsAsDictionary(expected_details)));
    127 
    128   js_sync_manager_observer_.OnConnectionStatusChange(kStatus);
    129   PumpLoop();
    130 }
    131 
    132 }  // namespace
    133 }  // namespace syncer
    134