4040#import " LPRegisterDevice.h"
4141#import " Leanplum.h"
4242#import " LPOperationQueue.h"
43+ #import " LPAPIConfig.h"
44+
45+ #import < OCMock/OCMArg.h>
4346
4447/* *
4548 * Tests leanplum public methods, we seed predefined response that comes from backend
@@ -75,6 +78,10 @@ + (void)setUp
7578
7679- (void )setUp {
7780 [super setUp ];
81+
82+ // Clear the set keys, required for plist testing
83+ [[LPAPIConfig sharedConfig ] setAppId: nil withAccessKey: nil ];
84+ [[LPConstantsState sharedState ] setIsDevelopmentModeEnabled: NO ];
7885}
7986
8087- (void )tearDown {
@@ -84,6 +91,142 @@ - (void)tearDown {
8491 [HTTPStubs removeAllStubs ];
8592}
8693
94+ /* *
95+ * Tests setting the environment in the plist to production.
96+ */
97+ - (void ) test_leanplum_load_plist_env_key_prod
98+ {
99+ id mockLeanplum = OCMClassMock ([Leanplum class ]);
100+ NSMutableDictionary *plistDict = [[NSMutableDictionary alloc ] initWithDictionary: [Leanplum getDefaultAppKeysPlist ] copyItems: YES ];
101+ plistDict[kEnvKey ] = kEnvProduction ;
102+ [[[mockLeanplum stub ] andReturn: plistDict] getDefaultAppKeysPlist ];
103+ // Force reload
104+ [Leanplum load ];
105+
106+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], APPLICATION_ID);
107+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], PRODUCTION_KEY);
108+ XCTAssertFalse ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
109+ }
110+
111+ /* *
112+ * Tests setting the environment in the plist to development.
113+ */
114+ - (void ) test_leanplum_load_plist_env_key_dev
115+ {
116+ id mockLeanplum = OCMClassMock ([Leanplum class ]);
117+ NSMutableDictionary *plistDict = [[NSMutableDictionary alloc ] initWithDictionary: [Leanplum getDefaultAppKeysPlist ] copyItems: YES ];
118+ plistDict[kEnvKey ] = kEnvDevelopment ;
119+ [[[mockLeanplum stub ] andReturn: plistDict] getDefaultAppKeysPlist ];
120+ // Force reload
121+ [Leanplum load ];
122+
123+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], APPLICATION_ID);
124+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], DEVELOPMENT_KEY);
125+ XCTAssertTrue ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
126+ }
127+
128+ /* *
129+ * Tests setting explicitly the environment to development.
130+ */
131+ - (void ) test_set_app_environment_dev
132+ {
133+ [Leanplum setAppEnvironment: kEnvDevelopment ];
134+
135+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], APPLICATION_ID);
136+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], DEVELOPMENT_KEY);
137+ XCTAssertTrue ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
138+ }
139+
140+ /* *
141+ * Tests setting explicitly the environment to production.
142+ */
143+ - (void ) test_set_app_environment_prod
144+ {
145+ [Leanplum setAppEnvironment: kEnvProduction ];
146+
147+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], APPLICATION_ID);
148+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], PRODUCTION_KEY);
149+ XCTAssertFalse ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
150+ }
151+
152+ /* *
153+ * Tests environment cannot be set with incorrect value.
154+ */
155+ - (void ) test_set_app_environment_incorrect
156+ {
157+ [LeanplumHelper mockThrowErrorToThrow ];
158+ XCTAssertThrows ([Leanplum setAppEnvironment: @" staging" ], @" Incorrect environment." );
159+ XCTAssertTrue ([LeanplumHelper.lastErrorMessage containsString: @" Incorrect env parameter." ]);
160+ }
161+
162+ /* *
163+ * Tests environment cannot be set after Leanplum has started.
164+ */
165+ - (void ) test_set_app_environment_after_start
166+ {
167+ [LeanplumHelper mockThrowErrorToThrow ];
168+
169+ [Leanplum load ];
170+ // Set Leanplum start was executed.
171+ [[LPInternalState sharedState ] setCalledStart: YES ];
172+
173+ XCTAssertThrows ([Leanplum setAppEnvironment: kEnvProduction ], @" Already called start." );
174+ XCTAssertTrue ([LeanplumHelper.lastErrorMessage containsString: @" Leanplum already started. Call this method before [Leanplum start]." ]);
175+ }
176+
177+ /* *
178+ * Tests app keys remain unset if plist is nil.
179+ */
180+ - (void ) test_leanplum_load_plist_with_nil
181+ {
182+ id mockLeanplum = OCMClassMock ([Leanplum class ]);
183+ [[[mockLeanplum stub ] andReturn: nil ] getDefaultAppKeysPlist ];
184+ // Force reload
185+ [Leanplum load ];
186+
187+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], nil );
188+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], nil );
189+ XCTAssertFalse ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
190+ }
191+
192+ /* *
193+ * Tests setting the app keys with Leanplum load method using plist.
194+ */
195+ - (void ) test_leanplum_load_plist
196+ {
197+ [Leanplum load ];
198+ #if DEBUG
199+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], DEVELOPMENT_KEY);
200+ XCTAssertTrue ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
201+ #else
202+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], PRODUCTION_KEY);
203+ XCTAssertFalse ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
204+ #endif
205+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], APPLICATION_ID);
206+ }
207+
208+ /* *
209+ * Tests setting the app for development using plist.
210+ */
211+ - (void ) test_set_development_from_plist
212+ {
213+ [Leanplum setAppUsingPlist: [Leanplum getDefaultAppKeysPlist ] forEnvironment: kEnvDevelopment ];
214+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], APPLICATION_ID);
215+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], DEVELOPMENT_KEY);
216+ XCTAssertTrue ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
217+ }
218+
219+ /* *
220+ * Tests setting the app for production using plist.
221+ */
222+ - (void ) test_set_production_from_plist
223+ {
224+ [Leanplum setAppUsingPlist: [Leanplum getDefaultAppKeysPlist ] forEnvironment: kEnvProduction ];
225+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] appId ], APPLICATION_ID);
226+ XCTAssertEqualObjects ([[LPAPIConfig sharedConfig ] accessKey ], PRODUCTION_KEY);
227+ XCTAssertFalse ([[LPConstantsState sharedState ] isDevelopmentModeEnabled ]);
228+ }
229+
87230/* *
88231 * Tests a simple development start.
89232 */
0 commit comments