summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/clear_browsing_data_controller_unittest.mm
blob: 9de805e8eae6003426f77282685a4fe89b1ec704 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 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 <Cocoa/Cocoa.h>

#include "base/scoped_nsobject.h"
#include "chrome/browser/browsing_data_remover.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/clear_browsing_data_controller.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"

namespace {

class ClearBrowsingDataControllerTest : public CocoaTest {
 public:
  virtual void SetUp() {
    CocoaTest::SetUp();
    // Set up some interesting prefs:
    PrefService* prefs = helper_.profile()->GetPrefs();
    prefs->SetBoolean(prefs::kDeleteBrowsingHistory, true);
    prefs->SetBoolean(prefs::kDeleteDownloadHistory, false);
    prefs->SetBoolean(prefs::kDeleteCache, true);
    prefs->SetBoolean(prefs::kDeleteCookies, false);
    prefs->SetBoolean(prefs::kDeletePasswords, true);
    prefs->SetBoolean(prefs::kDeleteFormData, false);
    prefs->SetInteger(prefs::kDeleteTimePeriod,
                      BrowsingDataRemover::FOUR_WEEKS);
    controller_ =
        [ClearBrowsingDataController controllerForProfile:helper_.profile()];
  }

  virtual void TearDown() {
    [controller_ closeDialog];
    CocoaTest::TearDown();
  }

  BrowserTestHelper helper_;
  ClearBrowsingDataController* controller_;
};

TEST_F(ClearBrowsingDataControllerTest, InitialState) {
  // Check properties match the prefs set above:
  EXPECT_TRUE([controller_ clearBrowsingHistory]);
  EXPECT_FALSE([controller_ clearDownloadHistory]);
  EXPECT_TRUE([controller_ emptyCache]);
  EXPECT_FALSE([controller_ deleteCookies]);
  EXPECT_TRUE([controller_ clearSavedPasswords]);
  EXPECT_FALSE([controller_ clearFormData]);
  EXPECT_EQ(BrowsingDataRemover::FOUR_WEEKS,
            [controller_ timePeriod]);
}

TEST_F(ClearBrowsingDataControllerTest, InitialRemoveMask) {
  // Check that the remove-mask matches the initial properties:
  EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY |
                BrowsingDataRemover::REMOVE_CACHE |
                BrowsingDataRemover::REMOVE_PASSWORDS,
            [controller_ removeMask]);
}

TEST_F(ClearBrowsingDataControllerTest, ModifiedRemoveMask) {
  // Invert all properties and check that the remove-mask is still correct:
  [controller_ setClearBrowsingHistory:false];
  [controller_ setClearDownloadHistory:true];
  [controller_ setEmptyCache:false];
  [controller_ setDeleteCookies:true];
  [controller_ setClearSavedPasswords:false];
  [controller_ setClearFormData:true];

  EXPECT_EQ(BrowsingDataRemover::REMOVE_DOWNLOADS |
                BrowsingDataRemover::REMOVE_COOKIES |
                BrowsingDataRemover::REMOVE_FORM_DATA,
            [controller_ removeMask]);
}

TEST_F(ClearBrowsingDataControllerTest, EmptyRemoveMask) {
  // Clear all properties and check that the remove-mask is zero:
  [controller_ setClearBrowsingHistory:false];
  [controller_ setClearDownloadHistory:false];
  [controller_ setEmptyCache:false];
  [controller_ setDeleteCookies:false];
  [controller_ setClearSavedPasswords:false];
  [controller_ setClearFormData:false];

  EXPECT_EQ(0,
            [controller_ removeMask]);
}

TEST_F(ClearBrowsingDataControllerTest, PersistToPrefs) {
  // Change some settings and store to prefs:
  [controller_ setClearBrowsingHistory:false];
  [controller_ setClearDownloadHistory:true];
  [controller_ persistToPrefs];

  // Test that the modified settings were stored to prefs:
  PrefService* prefs = helper_.profile()->GetPrefs();
  EXPECT_FALSE(prefs->GetBoolean(prefs::kDeleteBrowsingHistory));
  EXPECT_TRUE(prefs->GetBoolean(prefs::kDeleteDownloadHistory));

  // Make sure the rest of the prefs didn't change:
  EXPECT_TRUE(prefs->GetBoolean(prefs::kDeleteCache));
  EXPECT_FALSE(prefs->GetBoolean(prefs::kDeleteCookies));
  EXPECT_TRUE(prefs->GetBoolean(prefs::kDeletePasswords));
  EXPECT_FALSE(prefs->GetBoolean(prefs::kDeleteFormData));
  EXPECT_EQ(BrowsingDataRemover::FOUR_WEEKS,
            prefs->GetInteger(prefs::kDeleteTimePeriod));
}

TEST_F(ClearBrowsingDataControllerTest, SameControllerForProfile) {
  ClearBrowsingDataController* controller =
      [ClearBrowsingDataController controllerForProfile:helper_.profile()];
  EXPECT_EQ(controller_, controller);
}

TEST_F(ClearBrowsingDataControllerTest, DataRemoverDidFinish) {
  id observer = [OCMockObject observerMock];
  // Don't use |controller_| as the object because it will free itself twice
  // because both |-dataRemoverDidFinish| and TearDown() call |-closeDialog|.
  ClearBrowsingDataController* controller =
      [[ClearBrowsingDataController alloc] initWithProfile:helper_.profile()];

  NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
  [center addMockObserver:observer
                     name:kClearBrowsingDataControllerDidDelete
                   object:controller];

  int mask = [controller removeMask];
  NSDictionary* expectedInfo =
      [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:mask]
                                forKey:kClearBrowsingDataControllerRemoveMask];
  [[observer expect]
      notificationWithName:kClearBrowsingDataControllerDidDelete
                    object:controller
                  userInfo:expectedInfo];

  // This calls |-closeDialog| and cleans the controller up.
  [controller dataRemoverDidFinish];

  [observer verify];
}

}  // namespace