summaryrefslogtreecommitdiff
path: root/chromium/chrome/common/mac/staging_watcher_unittest.mm
blob: e3eb86ab95035400aa0f156bf47008f34c14d744 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2019 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.

#include "chrome/common/mac/staging_watcher.h"

#include <dispatch/dispatch.h>

#include "base/mac/bundle_locations.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsobject.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

enum class KVOOrNot { kUseKVO, kDontUseKVO };

class StagingKeyWatcherTest : public testing::TestWithParam<KVOOrNot> {
 public:
  StagingKeyWatcherTest() = default;
  ~StagingKeyWatcherTest() = default;

 protected:
  void SetUp() override {
    testingBundleID_.reset([[NSString alloc]
        initWithFormat:@"org.chromium.StagingKeyWatcherTest.%d", getpid()]);
    defaults_.reset(
        [[NSUserDefaults alloc] initWithSuiteName:testingBundleID_]);

    [defaults_ removeObjectForKey:[CrStagingKeyWatcher stagingKeyForTesting]];
  }

  void TearDown() override {
    [defaults_ removeObjectForKey:[CrStagingKeyWatcher stagingKeyForTesting]];
  }

  base::scoped_nsobject<CrStagingKeyWatcher> CreateKeyWatcher() {
    base::scoped_nsobject<CrStagingKeyWatcher> keyWatcher(
        [[CrStagingKeyWatcher alloc]
            initWithUserDefaults:defaults_
                     pollingTime:0.5
            disableKVOForTesting:(GetParam() == KVOOrNot::kDontUseKVO)]);

    return keyWatcher;
  }

  void SetDefaultsValue(id value) {
    [defaults_ setObject:value
                  forKey:[CrStagingKeyWatcher stagingKeyForTesting]];
  }

  void ClearDefaultsValueInSeparateProcess() {
    [NSTask launchedTaskWithLaunchPath:@"/usr/bin/defaults"
                             arguments:@[
                               @"delete", testingBundleID_.get(),
                               [CrStagingKeyWatcher stagingKeyForTesting]
                             ]];
  }

  void SetDefaultsValueInSeparateProcess() {
    NSString* appPath = [base::mac::OuterBundle() bundlePath];

    [NSTask launchedTaskWithLaunchPath:@"/usr/bin/defaults"
                             arguments:@[
                               @"write", testingBundleID_.get(),
                               [CrStagingKeyWatcher stagingKeyForTesting],
                               @"-dict", appPath, appPath
                             ]];
  }

 private:
  base::scoped_nsobject<NSString> testingBundleID_;
  base::scoped_nsobject<NSUserDefaults> defaults_;
};

INSTANTIATE_TEST_SUITE_P(KVOandNot,
                         StagingKeyWatcherTest,
                         testing::Values(KVOOrNot::kUseKVO,
                                         KVOOrNot::kDontUseKVO));

}  // namespace

TEST_P(StagingKeyWatcherTest, NoBlockingWhenNoKey) {
  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  [watcher waitForStagingKeyToClear];
  ASSERT_FALSE([watcher lastWaitWasBlockedForTesting]);
}

TEST_P(StagingKeyWatcherTest, NoBlockingWhenWrongKeyType) {
  SetDefaultsValue(@"this is not a dictionary");

  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  [watcher waitForStagingKeyToClear];
  ASSERT_FALSE([watcher lastWaitWasBlockedForTesting]);
}

TEST_P(StagingKeyWatcherTest, NoBlockingWhenArrayType) {
  SetDefaultsValue(@[ @3, @1, @4, @1, @5 ]);

  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  [watcher waitForStagingKeyToClear];
  ASSERT_FALSE([watcher lastWaitWasBlockedForTesting]);
}

TEST_P(StagingKeyWatcherTest, NoBlockingWhenEmptyArray) {
  SetDefaultsValue(@[]);

  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  [watcher waitForStagingKeyToClear];
  ASSERT_FALSE([watcher lastWaitWasBlockedForTesting]);
}

TEST_P(StagingKeyWatcherTest, NoBlockingWhenEmptyDictionary) {
  SetDefaultsValue(@{});

  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  [watcher waitForStagingKeyToClear];
  ASSERT_FALSE([watcher lastWaitWasBlockedForTesting]);
}

TEST_P(StagingKeyWatcherTest, BlockFunctionality) {
  NSString* appPath = [base::mac::OuterBundle() bundlePath];
  SetDefaultsValue(@{appPath : appPath});

  NSRunLoop* runloop = [NSRunLoop currentRunLoop];
  ASSERT_EQ(nil, [runloop currentMode]);

  dispatch_async(dispatch_get_main_queue(), ^{
    ASSERT_NE(nil, [runloop currentMode]);
    ClearDefaultsValueInSeparateProcess();
  });

  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  [watcher waitForStagingKeyToClear];
  ASSERT_TRUE([watcher lastWaitWasBlockedForTesting]);
}

TEST_P(StagingKeyWatcherTest, CallbackOnKeySet) {
  // The staging key begins not set.

  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  NSRunLoop* runloop = [NSRunLoop currentRunLoop];

  __block bool observerCalled = false;
  [watcher setStagingKeyChangedObserver:^(BOOL stagingKeySet) {
    observerCalled = true;
    CFRunLoopStop([runloop getCFRunLoop]);
  }];

  SetDefaultsValueInSeparateProcess();
  ASSERT_FALSE([watcher isStagingKeySet]);
  while (!observerCalled && [runloop runMode:NSDefaultRunLoopMode
                                  beforeDate:[NSDate distantFuture]]) {
    /* run! */
  }

  EXPECT_TRUE([watcher isStagingKeySet]);
}

TEST_P(StagingKeyWatcherTest, CallbackOnKeyUnset) {
  NSString* appPath = [base::mac::OuterBundle() bundlePath];
  SetDefaultsValue(@{appPath : appPath});

  base::scoped_nsobject<CrStagingKeyWatcher> watcher = CreateKeyWatcher();
  NSRunLoop* runloop = [NSRunLoop currentRunLoop];

  __block bool observerCalled = false;
  [watcher setStagingKeyChangedObserver:^(BOOL stagingKeySet) {
    observerCalled = true;
    CFRunLoopStop([runloop getCFRunLoop]);
  }];

  ClearDefaultsValueInSeparateProcess();
  ASSERT_TRUE([watcher isStagingKeySet]);
  while (!observerCalled && [runloop runMode:NSDefaultRunLoopMode
                                  beforeDate:[NSDate distantFuture]]) {
    /* run! */
  }

  EXPECT_FALSE([watcher isStagingKeySet]);
}