Fix FullScreen crash in Webapp
[platform/framework/web/chromium-efl.git] / testing / gtest_mac.mm
1 // Copyright 2010 The Chromium Authors
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 "gtest_mac.h"
6
7 #include <string>
8
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/googletest/src/googletest/include/gtest/internal/gtest-port.h"
11 #include "third_party/googletest/src/googletest/include/gtest/internal/gtest-string.h"
12
13 // /!\ WARNING!
14 //
15 // Chromium compiles this file as ARC, but other dependencies pull it in and
16 // compile it as non-ARC. Be sure that this file compiles correctly with either
17 // build setting.
18 //
19 // /!\ WARNING!
20
21 #ifdef GTEST_OS_MAC
22
23 #import <Foundation/Foundation.h>
24
25 namespace testing::internal {
26
27 static std::string StringFromNSString(NSString* string) {
28   // Note that -[NSString UTF8String] is banned in Chromium code because
29   // base::SysNSStringToUTF8() is safer, but //testing isn't allowed to depend
30   // on //base, so deliberately ignore that function ban.
31   const char* utf_string = string.UTF8String;
32   return utf_string ? std::string(utf_string) : std::string("(nil nsstring)");
33 }
34
35 // Handles nil values for |obj| properly by using safe printing of %@ in
36 // -stringWithFormat:.
37 std::string StringDescription(id<NSObject> obj) {
38   return StringFromNSString([NSString stringWithFormat:@"%@", obj]);
39 }
40
41 // This overloaded version allows comparison between ObjC objects that conform
42 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_EQ().
43 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
44                                          const char* actual_expression,
45                                          id<NSObject> expected,
46                                          id<NSObject> actual) {
47   if (expected == actual || [expected isEqual:actual]) {
48     return AssertionSuccess();
49   }
50   return EqFailure(expected_expression, actual_expression,
51                    StringDescription(expected), StringDescription(actual),
52                    false);
53 }
54
55 // This overloaded version allows comparison between ObjC objects that conform
56 // to the NSObject protocol. Used to implement {ASSERT|EXPECT}_NE().
57 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
58                                          const char* actual_expression,
59                                          id<NSObject> expected,
60                                          id<NSObject> actual) {
61   if (expected != actual && ![expected isEqual:actual]) {
62     return AssertionSuccess();
63   }
64   Message msg;
65   msg << "Expected: (" << expected_expression << ") != (" << actual_expression
66       << "), actual: " << StringDescription(expected)
67       << " vs " << StringDescription(actual);
68   return AssertionFailure(msg);
69 }
70
71 #if !defined(GTEST_OS_IOS)
72
73 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
74                                          const char* actual_expression,
75                                          const NSRect& expected,
76                                          const NSRect& actual) {
77   if (NSEqualRects(expected, actual)) {
78     return AssertionSuccess();
79   }
80   return EqFailure(expected_expression, actual_expression,
81                    StringFromNSString(NSStringFromRect(expected)),
82                    StringFromNSString(NSStringFromRect(actual)), false);
83 }
84
85 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
86                                          const char* actual_expression,
87                                          const NSRect& expected,
88                                          const NSRect& actual) {
89   if (!NSEqualRects(expected, actual)) {
90     return AssertionSuccess();
91   }
92   Message msg;
93   msg << "Expected: (" << expected_expression << ") != (" << actual_expression
94       << "), actual: " << StringFromNSString(NSStringFromRect(expected))
95       << " vs " << StringFromNSString(NSStringFromRect(actual));
96   return AssertionFailure(msg);
97
98 }
99
100 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
101                                          const char* actual_expression,
102                                          const NSPoint& expected,
103                                          const NSPoint& actual) {
104   if (NSEqualPoints(expected, actual)) {
105     return AssertionSuccess();
106   }
107   return EqFailure(expected_expression, actual_expression,
108                    StringFromNSString(NSStringFromPoint(expected)),
109                    StringFromNSString(NSStringFromPoint(actual)), false);
110 }
111
112 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
113                                          const char* actual_expression,
114                                          const NSPoint& expected,
115                                          const NSPoint& actual) {
116   if (!NSEqualPoints(expected, actual)) {
117     return AssertionSuccess();
118   }
119   Message msg;
120   msg << "Expected: (" << expected_expression << ") != (" << actual_expression
121       << "), actual: " << StringFromNSString(NSStringFromPoint(expected))
122       << " vs " << StringFromNSString(NSStringFromPoint(actual));
123   return AssertionFailure(msg);
124 }
125
126 GTEST_API_ AssertionResult CmpHelperNSEQ(const char* expected_expression,
127                                          const char* actual_expression,
128                                          const NSRange& expected,
129                                          const NSRange& actual) {
130   if (NSEqualRanges(expected, actual)) {
131     return AssertionSuccess();
132   }
133   return EqFailure(expected_expression, actual_expression,
134                    StringFromNSString(NSStringFromRange(expected)),
135                    StringFromNSString(NSStringFromRange(actual)), false);
136 }
137
138 GTEST_API_ AssertionResult CmpHelperNSNE(const char* expected_expression,
139                                          const char* actual_expression,
140                                          const NSRange& expected,
141                                          const NSRange& actual) {
142   if (!NSEqualRanges(expected, actual)) {
143     return AssertionSuccess();
144   }
145   Message msg;
146   msg << "Expected: (" << expected_expression << ") != (" << actual_expression
147       << "), actual: " << StringFromNSString(NSStringFromRange(expected))
148       << " vs " << StringFromNSString(NSStringFromRange(actual));
149   return AssertionFailure(msg);
150 }
151
152 #endif  // !GTEST_OS_IOS
153
154 }  // namespace testing::internal
155
156 #endif  // GTEST_OS_MAC