1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support."
9 #import "remoting/ios/data_store.h"
11 #import "base/compiler_specific.h"
12 #import "testing/gtest_mac.h"
18 NSString* kHostId = @"testHost";
19 NSString* kHostPin = @"testHostPin";
20 NSString* kPairId = @"testPairId";
21 NSString* kPairSecret = @"testPairSecret";
25 class DataStoreTest : public ::testing::Test {
27 virtual void SetUp() OVERRIDE {
28 store_ = [[DataStore allocWithZone:nil] init];
30 EXPECT_EQ(0, HostCount());
32 virtual void TearDown() OVERRIDE { RemoveAllHosts(); }
34 int HostCount() { return [[store_ allHosts] count]; }
36 void RemoveAllHosts() {
37 while (HostCount() > 0) {
38 [store_ removeHost:[store_ allHosts].firstObject];
46 TEST(DataStoreTest_Static, IsSingleInstance) {
47 DataStore* firstStore = [DataStore sharedStore];
49 ASSERT_NSEQ(firstStore, [DataStore sharedStore]);
52 TEST(DataStoreTest_Static, RemoveAllHost) {
53 // Test this functionality independently before expecting the fixture to do
54 // this correctly during cleanup
55 DataStore* store = [DataStore sharedStore];
57 while ([[store allHosts] count]) {
58 [store removeHost:[store allHosts].firstObject];
61 ASSERT_EQ(0, [[store allHosts] count]);
65 TEST_F(DataStoreTest, CreateHost) {
67 const HostPreferences* host = [store_ createHost:kHostId];
68 ASSERT_STREQ([kHostId UTF8String], [host.hostId UTF8String]);
69 ASSERT_EQ(1, HostCount());
72 TEST_F(DataStoreTest, GetHostForId) {
73 const HostPreferences* host = [store_ getHostForId:kHostId];
74 ASSERT_TRUE(host == nil);
76 [store_ createHost:kHostId];
78 host = [store_ getHostForId:kHostId];
80 ASSERT_TRUE(host != nil);
81 ASSERT_STREQ([kHostId UTF8String], [host.hostId UTF8String]);
84 TEST_F(DataStoreTest, SaveChanges) {
86 const HostPreferences* newHost = [store_ createHost:kHostId];
88 ASSERT_EQ(1, HostCount());
90 // Default values for a new host
91 ASSERT_TRUE([newHost.askForPin boolValue] == NO);
92 ASSERT_TRUE(newHost.hostPin == nil);
93 ASSERT_TRUE(newHost.pairId == nil);
94 ASSERT_TRUE(newHost.pairSecret == nil);
96 // Set new values and save
97 newHost.askForPin = [NSNumber numberWithBool:YES];
98 newHost.hostPin = kHostPin;
99 newHost.pairId = kPairId;
100 newHost.pairSecret = kPairSecret;
102 [store_ saveChanges];
104 // The next time the store is loaded the host will still be present, even
105 // though we are about to release and reinit a new object
107 store_ = [[DataStore allocWithZone:nil] init];
108 ASSERT_EQ(1, HostCount());
110 const HostPreferences* host = [store_ getHostForId:kHostId];
111 ASSERT_TRUE(host != nil);
112 ASSERT_STREQ([kHostId UTF8String], [host.hostId UTF8String]);
113 ASSERT_TRUE([host.askForPin boolValue] == YES);
114 ASSERT_STREQ([kHostPin UTF8String], [host.hostPin UTF8String]);
115 ASSERT_STREQ([kPairId UTF8String], [host.pairId UTF8String]);
116 ASSERT_STREQ([kPairSecret UTF8String], [host.pairSecret UTF8String]);
119 } // namespace remoting