summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/sync_customize_controller_unittest.mm
blob: eccfed494f6fa74d90ab7a5baf208adde3eb92ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/cocoa/sync_customize_controller.h"
#import "chrome/browser/cocoa/sync_customize_controller_cppsafe.h"

#import <Cocoa/Cocoa.h>

#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "chrome/browser/sync/profile_sync_service_mock.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

using ::testing::_;
using ::testing::SaveArg;
using ::testing::SetArgumentPointee;

class SyncCustomizeControllerTest : public CocoaTest {
 protected:
  virtual void SetUp() {
    CocoaTest::SetUp();
    syncable::ModelTypeSet initial_registered_types, initial_preferred_types;
    initial_registered_types.insert(syncable::BOOKMARKS);
    initial_registered_types.insert(syncable::PREFERENCES);
    initial_registered_types.insert(syncable::THEMES);
    initial_preferred_types.insert(syncable::PREFERENCES);
    EXPECT_CALL(sync_service_mock_, GetRegisteredDataTypes(_))
        .WillOnce(SetArgumentPointee<0>(initial_registered_types));
    EXPECT_CALL(sync_service_mock_, GetPreferredDataTypes(_))
        .WillOnce(SetArgumentPointee<0>(initial_preferred_types));
  }

  virtual SyncCustomizeController* MakeSyncCustomizeController() {
    return [[SyncCustomizeController alloc]
            initWithProfileSyncService:&sync_service_mock_];
  }

  ProfileSyncServiceMock sync_service_mock_;
};

TEST_F(SyncCustomizeControllerTest, InitialRead) {
  SyncCustomizeController* controller = MakeSyncCustomizeController();
  // Force nib load.
  [controller window];

  EXPECT_TRUE([controller bookmarksRegistered]);
  EXPECT_TRUE([controller preferencesRegistered]);
  EXPECT_FALSE([controller autofillRegistered]);
  EXPECT_TRUE([controller themesRegistered]);

  EXPECT_FALSE([controller bookmarksPreferred]);
  EXPECT_TRUE([controller preferencesPreferred]);
  EXPECT_FALSE([controller autofillPreferred]);
  EXPECT_FALSE([controller themesPreferred]);

  [controller close];
}

TEST_F(SyncCustomizeControllerTest, RunAsModalSheet) {
  SyncCustomizeController* controller = MakeSyncCustomizeController();
  [controller runAsModalSheet:test_window()];
  [controller endSheetWithCancel:nil];
}

TEST_F(SyncCustomizeControllerTest, ShowSyncCustomizeDialog) {
  ShowSyncCustomizeDialog(test_window(), &sync_service_mock_);
  id controller = [[test_window() attachedSheet] windowController];
  EXPECT_TRUE([controller isKindOfClass:[SyncCustomizeController class]]);
  [controller endSheetWithCancel:nil];
}

TEST_F(SyncCustomizeControllerTest, EndSheetWithCancel) {
  EXPECT_CALL(sync_service_mock_, ChangePreferredDataTypes(_)).Times(0);

  SyncCustomizeController* controller = MakeSyncCustomizeController();
  [controller runAsModalSheet:test_window()];
  [controller setPreferencesPreferred:NO];
  [controller setThemesPreferred:YES];
  [controller endSheetWithCancel:nil];

  // If ChangePreferredDataTypes() wasn't called, then that means the
  // changes weren't committed.
}

TEST_F(SyncCustomizeControllerTest, EndSheetWithOK) {
  syncable::ModelTypeSet preferred_types;
  EXPECT_CALL(sync_service_mock_, ChangePreferredDataTypes(_))
      .WillOnce(SaveArg<0>(&preferred_types));

  SyncCustomizeController* controller = MakeSyncCustomizeController();
  [controller runAsModalSheet:test_window()];
  [controller setPreferencesPreferred:NO];
  [controller setThemesPreferred:YES];
  [controller endSheetWithOK:nil];

  syncable::ModelTypeSet expected_preferred_types;
  expected_preferred_types.insert(syncable::THEMES);
  EXPECT_TRUE(preferred_types == expected_preferred_types);
}
  
TEST_F(SyncCustomizeControllerTest, CannotSelectZeroTypes) {
  SyncCustomizeController* controller = MakeSyncCustomizeController();
  [controller runAsModalSheet:test_window()];
  [controller setBookmarksPreferred:NO];
  [controller setPreferencesPreferred:NO];
  [controller setAutofillPreferred:NO];
  [controller setThemesPreferred:NO];
  
  EXPECT_FALSE([controller okEnabled]);

  // If any data types are enabled, "OK" should be enabled too.
  [controller setBookmarksPreferred:NO];
  [controller setPreferencesPreferred:YES];
  [controller setAutofillPreferred:NO];
  [controller setThemesPreferred:NO];
  
  EXPECT_TRUE([controller okEnabled]);
  
  [controller endSheetWithCancel:nil];
}   

}  // namespace