OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
6 #import <objc/objc-class.h> | 6 #import <objc/objc-class.h> |
7 | 7 |
8 #import "chrome/app/keystone_glue.h" | 8 #import "chrome/app/keystone_glue.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
11 | 11 |
12 @interface FakeGlueRegistration : NSObject | 12 @interface FakeGlueRegistration : NSObject |
13 @end | 13 @end |
14 | 14 |
15 | 15 |
16 @implementation FakeGlueRegistration | 16 @implementation FakeGlueRegistration |
17 - (void)checkForUpdate { } | 17 |
18 - (void)startUpdate { } | 18 // Send the notifications that a real KeystoneGlue object would send. |
| 19 |
| 20 - (void)checkForUpdate { |
| 21 NSNumber* yesNumber = [NSNumber numberWithBool:YES]; |
| 22 NSString* statusKey = @"Status"; |
| 23 NSDictionary* dictionary = [NSDictionary dictionaryWithObject:yesNumber |
| 24 forKey:statusKey]; |
| 25 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 26 [center postNotificationName:@"KSRegistrationCheckForUpdateNotification" |
| 27 object:nil |
| 28 userInfo:dictionary]; |
| 29 } |
| 30 |
| 31 - (void)startUpdate { |
| 32 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 33 [center postNotificationName:@"KSRegistrationStartUpdateNotification" |
| 34 object:nil]; |
| 35 } |
| 36 |
19 @end | 37 @end |
20 | 38 |
21 | 39 |
22 @interface FakeKeystoneGlue : KeystoneGlue<KeystoneGlueCallbacks> { | 40 @interface FakeKeystoneGlue : KeystoneGlue { |
23 @public | 41 @public |
24 BOOL upToDate_; | 42 BOOL upToDate_; |
25 NSString *latestVersion_; | 43 NSString *latestVersion_; |
26 BOOL successful_; | 44 BOOL successful_; |
27 int installs_; | 45 int installs_; |
28 } | 46 } |
| 47 |
| 48 - (void)fakeAboutWindowCallback:(NSNotification*)notification; |
29 @end | 49 @end |
30 | 50 |
31 | 51 |
32 @implementation FakeKeystoneGlue | 52 @implementation FakeKeystoneGlue |
33 | 53 |
34 - (id)init { | 54 - (id)init { |
35 if ((self = [super init])) { | 55 if ((self = [super init])) { |
36 // some lies | 56 // some lies |
37 upToDate_ = YES; | 57 upToDate_ = YES; |
38 latestVersion_ = @"foo bar"; | 58 latestVersion_ = @"foo bar"; |
39 successful_ = YES; | 59 successful_ = YES; |
40 installs_ = 1010101010; | 60 installs_ = 1010101010; |
| 61 |
| 62 // Set up an observer that takes the notification that the About window |
| 63 // listens for. |
| 64 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 65 [center addObserver:self |
| 66 selector:@selector(fakeAboutWindowCallback:) |
| 67 name:kAutoupdateStatusNotification |
| 68 object:nil]; |
41 } | 69 } |
42 return self; | 70 return self; |
43 } | 71 } |
44 | 72 |
| 73 - (void)dealloc { |
| 74 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 75 [super dealloc]; |
| 76 } |
| 77 |
45 // For mocking | 78 // For mocking |
46 - (NSDictionary*)infoDictionary { | 79 - (NSDictionary*)infoDictionary { |
47 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: | 80 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: |
48 @"http://foo.bar", @"KSUpdateURL", | 81 @"http://foo.bar", @"KSUpdateURL", |
49 @"com.google.whatever", @"KSProductID", | 82 @"com.google.whatever", @"KSProductID", |
50 @"0.0.0.1", @"KSVersion", | 83 @"0.0.0.1", @"KSVersion", |
51 nil]; | 84 nil]; |
52 return dict; | 85 return dict; |
53 } | 86 } |
54 | 87 |
55 // For mocking | 88 // For mocking |
56 - (BOOL)loadKeystoneRegistration { | 89 - (BOOL)loadKeystoneRegistration { |
57 return YES; | 90 return YES; |
58 } | 91 } |
59 | 92 |
60 // Confirms certain things are happy | 93 // Confirms certain things are happy |
61 - (BOOL)dictReadCorrectly { | 94 - (BOOL)dictReadCorrectly { |
62 return ([url_ isEqual:@"http://foo.bar"] && | 95 return ([url_ isEqual:@"http://foo.bar"] && |
63 [productID_ isEqual:@"com.google.whatever"] && | 96 [productID_ isEqual:@"com.google.whatever"] && |
64 [version_ isEqual:@"0.0.0.1"]); | 97 [version_ isEqual:@"0.0.0.1"]); |
65 } | 98 } |
66 | 99 |
67 // Confirms certain things are happy | 100 // Confirms certain things are happy |
68 - (BOOL)hasATimer { | 101 - (BOOL)hasATimer { |
69 return timer_ ? YES : NO; | 102 return timer_ ? YES : NO; |
70 } | 103 } |
71 | 104 |
72 - (void)upToDateCheckCompleted:(BOOL)upToDate | |
73 latestVersion:(NSString*)latestVersion { | |
74 upToDate_ = upToDate; | |
75 latestVersion_ = latestVersion; | |
76 } | |
77 | |
78 - (void)updateCompleted:(BOOL)successful installs:(int)installs { | |
79 successful_ = successful; | |
80 installs_ = installs; | |
81 } | |
82 | |
83 - (void)addFakeRegistration { | 105 - (void)addFakeRegistration { |
84 registration_ = [[FakeGlueRegistration alloc] init]; | 106 registration_ = [[FakeGlueRegistration alloc] init]; |
85 } | 107 } |
86 | 108 |
| 109 - (void)fakeAboutWindowCallback:(NSNotification*)notification { |
| 110 NSDictionary* dictionary = [notification userInfo]; |
| 111 AutoupdateStatus status = static_cast<AutoupdateStatus>( |
| 112 [[dictionary objectForKey:kAutoupdateStatusStatus] intValue]); |
| 113 |
| 114 if (status == kAutoupdateAvailable) { |
| 115 upToDate_ = NO; |
| 116 latestVersion_ = [dictionary objectForKey:kAutoupdateStatusVersion]; |
| 117 } else if (status == kAutoupdateInstallFailed) { |
| 118 successful_ = NO; |
| 119 installs_ = 0; |
| 120 } |
| 121 } |
| 122 |
87 // Confirm we look like callbacks with nil NSNotifications | 123 // Confirm we look like callbacks with nil NSNotifications |
88 - (BOOL)confirmCallbacks { | 124 - (BOOL)confirmCallbacks { |
89 return (!upToDate_ && | 125 return (!upToDate_ && |
90 (latestVersion_ == nil) && | 126 (latestVersion_ == nil) && |
91 !successful_ && | 127 !successful_ && |
92 (installs_ == 0)); | 128 (installs_ == 0)); |
93 } | 129 } |
94 | 130 |
95 @end | 131 @end |
96 | 132 |
(...skipping 10 matching lines...) Expand all Loading... |
107 IMP newInfoImp_ = [[FakeKeystoneGlue class] instanceMethodForSelector:ids]; | 143 IMP newInfoImp_ = [[FakeKeystoneGlue class] instanceMethodForSelector:ids]; |
108 Method infoMethod_ = class_getInstanceMethod([KeystoneGlue class], ids); | 144 Method infoMethod_ = class_getInstanceMethod([KeystoneGlue class], ids); |
109 method_setImplementation(infoMethod_, newInfoImp_); | 145 method_setImplementation(infoMethod_, newInfoImp_); |
110 | 146 |
111 SEL lks = @selector(loadKeystoneRegistration); | 147 SEL lks = @selector(loadKeystoneRegistration); |
112 IMP oldLoadImp_ = [[KeystoneGlue class] instanceMethodForSelector:lks]; | 148 IMP oldLoadImp_ = [[KeystoneGlue class] instanceMethodForSelector:lks]; |
113 IMP newLoadImp_ = [[FakeKeystoneGlue class] instanceMethodForSelector:lks]; | 149 IMP newLoadImp_ = [[FakeKeystoneGlue class] instanceMethodForSelector:lks]; |
114 Method loadMethod_ = class_getInstanceMethod([KeystoneGlue class], lks); | 150 Method loadMethod_ = class_getInstanceMethod([KeystoneGlue class], lks); |
115 method_setImplementation(loadMethod_, newLoadImp_); | 151 method_setImplementation(loadMethod_, newLoadImp_); |
116 | 152 |
| 153 // Dump any existing KeystoneGlue shared instance so that a new one can be |
| 154 // created with the mocked methods. |
| 155 [KeystoneGlue releaseDefaultKeystoneGlue]; |
117 KeystoneGlue *glue = [KeystoneGlue defaultKeystoneGlue]; | 156 KeystoneGlue *glue = [KeystoneGlue defaultKeystoneGlue]; |
118 ASSERT_TRUE(glue); | 157 ASSERT_TRUE(glue); |
119 | 158 |
120 // Fix back up the class to the way we found it. | 159 // Fix back up the class to the way we found it. |
121 method_setImplementation(infoMethod_, oldInfoImp_); | 160 method_setImplementation(infoMethod_, oldInfoImp_); |
122 method_setImplementation(loadMethod_, oldLoadImp_); | 161 method_setImplementation(loadMethod_, oldLoadImp_); |
| 162 [KeystoneGlue releaseDefaultKeystoneGlue]; |
123 } | 163 } |
124 | 164 |
125 TEST_F(KeystoneGlueTest, BasicUse) { | 165 TEST_F(KeystoneGlueTest, BasicUse) { |
126 FakeKeystoneGlue* glue = [[[FakeKeystoneGlue alloc] init] autorelease]; | 166 FakeKeystoneGlue* glue = [[[FakeKeystoneGlue alloc] init] autorelease]; |
127 [glue loadParameters]; | 167 [glue loadParameters]; |
128 ASSERT_TRUE([glue dictReadCorrectly]); | 168 ASSERT_TRUE([glue dictReadCorrectly]); |
129 | 169 |
130 // Likely returns NO in the unit test, but call it anyway to make | 170 // Likely returns NO in the unit test, but call it anyway to make |
131 // sure it doesn't crash. | 171 // sure it doesn't crash. |
132 [glue loadKeystoneRegistration]; | 172 [glue loadKeystoneRegistration]; |
133 | 173 |
134 // Confirm we start up an active timer | 174 // Confirm we start up an active timer |
135 [glue registerWithKeystone]; | 175 [glue registerWithKeystone]; |
136 ASSERT_TRUE([glue hasATimer]); | 176 ASSERT_TRUE([glue hasATimer]); |
137 [glue stopTimer]; | 177 [glue stopTimer]; |
138 | 178 |
139 ASSERT_TRUE(![glue checkForUpdate:glue] && ![glue startUpdate:glue]); | |
140 | |
141 // Brief exercise of callbacks | 179 // Brief exercise of callbacks |
142 [glue addFakeRegistration]; | 180 [glue addFakeRegistration]; |
143 ASSERT_TRUE([glue checkForUpdate:glue]); | 181 [glue checkForUpdate]; |
144 [glue checkComplete:nil]; | 182 [glue installUpdate]; |
145 ASSERT_TRUE([glue startUpdate:glue]); | |
146 [glue startUpdateComplete:nil]; | |
147 ASSERT_TRUE([glue confirmCallbacks]); | 183 ASSERT_TRUE([glue confirmCallbacks]); |
148 } | 184 } |
149 | 185 |
150 } // namespace | 186 } // namespace |
OLD | NEW |