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. 4 5 #if !defined(__has_feature) || !__has_feature(objc_arc) 6 #error "This file requires ARC support." 7 #endif 8 9 #import "remoting/ios/data_store.h" 10 11 #import "base/compiler_specific.h" 12 #import "testing/gtest_mac.h" 13 14 namespace remoting { 15 16 namespace { 17 18 NSString* kHostId = @"testHost"; 19 NSString* kHostPin = @"testHostPin"; 20 NSString* kPairId = @"testPairId"; 21 NSString* kPairSecret = @"testPairSecret"; 22 23 } // namespace 24 25 class DataStoreTest : public ::testing::Test { 26 protected: 27 virtual void SetUp() OVERRIDE { 28 store_ = [[DataStore allocWithZone:nil] init]; 29 RemoveAllHosts(); 30 EXPECT_EQ(0, HostCount()); 31 } 32 virtual void TearDown() OVERRIDE { RemoveAllHosts(); } 33 34 int HostCount() { return [[store_ allHosts] count]; } 35 36 void RemoveAllHosts() { 37 while (HostCount() > 0) { 38 [store_ removeHost:[store_ allHosts].firstObject]; 39 } 40 [store_ saveChanges]; 41 } 42 43 DataStore* store_; 44 }; 45 46 TEST(DataStoreTest_Static, IsSingleInstance) { 47 DataStore* firstStore = [DataStore sharedStore]; 48 49 ASSERT_NSEQ(firstStore, [DataStore sharedStore]); 50 } 51 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]; 56 57 while ([[store allHosts] count]) { 58 [store removeHost:[store allHosts].firstObject]; 59 } 60 61 ASSERT_EQ(0, [[store allHosts] count]); 62 store = nil; 63 } 64 65 TEST_F(DataStoreTest, CreateHost) { 66 67 const HostPreferences* host = [store_ createHost:kHostId]; 68 ASSERT_STREQ([kHostId UTF8String], [host.hostId UTF8String]); 69 ASSERT_EQ(1, HostCount()); 70 } 71 72 TEST_F(DataStoreTest, GetHostForId) { 73 const HostPreferences* host = [store_ getHostForId:kHostId]; 74 ASSERT_TRUE(host == nil); 75 76 [store_ createHost:kHostId]; 77 78 host = [store_ getHostForId:kHostId]; 79 80 ASSERT_TRUE(host != nil); 81 ASSERT_STREQ([kHostId UTF8String], [host.hostId UTF8String]); 82 } 83 84 TEST_F(DataStoreTest, SaveChanges) { 85 86 const HostPreferences* newHost = [store_ createHost:kHostId]; 87 88 ASSERT_EQ(1, HostCount()); 89 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); 95 96 // Set new values and save 97 newHost.askForPin = [NSNumber numberWithBool:YES]; 98 newHost.hostPin = kHostPin; 99 newHost.pairId = kPairId; 100 newHost.pairSecret = kPairSecret; 101 102 [store_ saveChanges]; 103 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 106 store_ = nil; 107 store_ = [[DataStore allocWithZone:nil] init]; 108 ASSERT_EQ(1, HostCount()); 109 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]); 117 } 118 119 } // namespace remoting 120