Home | History | Annotate | Download | only in ios
      1 // Copyright (c) 2012 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 #import <UIKit/UIKit.h>
      6 
      7 #include "base/ios/device_util.h"
      8 #include "base/ios/ios_util.h"
      9 #include "base/strings/sys_string_conversions.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "testing/gtest_mac.h"
     12 #include "testing/platform_test.h"
     13 
     14 namespace {
     15 // The behavior of most of these utility functions depends on what they are run
     16 // on, so there is not much to unittest them. The APIs are run to make sure they
     17 // don't choke. Additional checks are added for particular APIs when needed.
     18 
     19 typedef PlatformTest DeviceUtilTest;
     20 
     21 void CleanNSUserDefaultsForDeviceId() {
     22   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
     23   [defaults removeObjectForKey:@"ChromeClientID"];
     24   [defaults removeObjectForKey:@"ChromiumClientID"];
     25   [defaults removeObjectForKey:@"ClientIDGenerationHardwareType"];
     26   [defaults synchronize];
     27 }
     28 
     29 TEST_F(DeviceUtilTest, GetPlatform) {
     30   GTEST_ASSERT_GT(ios::device_util::GetPlatform().length(), 0U);
     31 }
     32 
     33 TEST_F(DeviceUtilTest, IsRunningOnHighRamDevice) {
     34   ios::device_util::IsRunningOnHighRamDevice();
     35 }
     36 
     37 TEST_F(DeviceUtilTest, IsSingleCoreDevice) {
     38   ios::device_util::IsSingleCoreDevice();
     39 }
     40 
     41 TEST_F(DeviceUtilTest, GetMacAddress) {
     42   GTEST_ASSERT_GT(ios::device_util::GetMacAddress("en0").length(), 0U);
     43 }
     44 
     45 TEST_F(DeviceUtilTest, GetRandomId) {
     46   GTEST_ASSERT_GT(ios::device_util::GetRandomId().length(), 0U);
     47 }
     48 
     49 TEST_F(DeviceUtilTest, GetDeviceIdentifier) {
     50   CleanNSUserDefaultsForDeviceId();
     51 
     52   std::string default_id = ios::device_util::GetDeviceIdentifier(NULL);
     53   std::string other_id = ios::device_util::GetDeviceIdentifier("ForTest");
     54   EXPECT_NE(default_id, other_id);
     55 
     56   CleanNSUserDefaultsForDeviceId();
     57 
     58   std::string new_default_id = ios::device_util::GetDeviceIdentifier(NULL);
     59   if (base::ios::IsRunningOnIOS6OrLater() &&
     60       ![[[[UIDevice currentDevice] identifierForVendor] UUIDString]
     61           isEqualToString:@"00000000-0000-0000-0000-000000000000"]) {
     62     EXPECT_EQ(default_id, new_default_id);
     63   } else {
     64     EXPECT_NE(default_id, new_default_id);
     65   }
     66 
     67   CleanNSUserDefaultsForDeviceId();
     68 }
     69 
     70 TEST_F(DeviceUtilTest, CheckMigration) {
     71   CleanNSUserDefaultsForDeviceId();
     72 
     73   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
     74   [defaults setObject:@"10000000-0000-0000-0000-000000000000"
     75                forKey:@"ChromeClientID"];
     76   [defaults synchronize];
     77   std::string expected_id = ios::device_util::GetDeviceIdentifier(NULL);
     78   [defaults removeObjectForKey:@"ChromeClientID"];
     79   [defaults setObject:@"10000000-0000-0000-0000-000000000000"
     80                forKey:@"ChromiumClientID"];
     81   [defaults synchronize];
     82   std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
     83   EXPECT_EQ(expected_id, new_id);
     84 
     85   CleanNSUserDefaultsForDeviceId();
     86 }
     87 
     88 TEST_F(DeviceUtilTest, CheckMigrationFromZero) {
     89   CleanNSUserDefaultsForDeviceId();
     90 
     91   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
     92   [defaults setObject:@"00000000-0000-0000-0000-000000000000"
     93                forKey:@"ChromeClientID"];
     94   [defaults synchronize];
     95   std::string zero_id = ios::device_util::GetDeviceIdentifier(NULL);
     96   [defaults removeObjectForKey:@"ChromeClientID"];
     97   [defaults setObject:@"00000000-0000-0000-0000-000000000000"
     98                forKey:@"ChromiumClientID"];
     99   [defaults synchronize];
    100   std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
    101   EXPECT_NE(zero_id, new_id);
    102 
    103   CleanNSUserDefaultsForDeviceId();
    104 }
    105 
    106 TEST_F(DeviceUtilTest, CheckDeviceMigration) {
    107   CleanNSUserDefaultsForDeviceId();
    108 
    109   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    110   [defaults setObject:@"10000000-0000-0000-0000-000000000000"
    111                forKey:@"ChromeClientID"];
    112   [defaults synchronize];
    113   std::string base_id = ios::device_util::GetDeviceIdentifier(NULL);
    114   [defaults setObject:@"Foo" forKey:@"ClientIDGenerationHardwareType"];
    115   [defaults synchronize];
    116   std::string new_id = ios::device_util::GetDeviceIdentifier(NULL);
    117   EXPECT_NE(new_id, base_id);
    118 
    119   CleanNSUserDefaultsForDeviceId();
    120 }
    121 
    122 }  // namespace
    123