Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / signin / chrome_identity_service_observer_bridge_unittest.mm
blob154071badc172511d004085470461e00c700708e
1 // Copyright 2015 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 #import "ios/chrome/browser/signin/chrome_identity_service_observer_bridge.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/memory/scoped_ptr.h"
9 #import "ios/public/provider/chrome/browser/signin/chrome_identity.h"
10 #include "ios/public/provider/chrome/browser/signin/chrome_identity_service.h"
11 #include "testing/platform_test.h"
13 @interface TestChromeIdentityServiceObserver
14     : NSObject<ChromeIdentityServiceObserver>
15 @property(nonatomic) BOOL onIdentityListChangedCalled;
16 @property(nonatomic) BOOL onAccessTokenRefreshFailedCalled;
17 @property(nonatomic) BOOL onProfileUpdateCalled;
18 @property(nonatomic, assign) ChromeIdentity* identity;
19 @property(nonatomic) ios::AccessTokenErrorReason error;
20 @property(nonatomic, readonly)
21     ios::ChromeIdentityService::Observer* observerBridge;
22 @end
24 @implementation TestChromeIdentityServiceObserver {
25   scoped_ptr<ios::ChromeIdentityService::Observer> observer_bridge_;
28 @synthesize onIdentityListChangedCalled = _onIdentityListChangedCalled;
29 @synthesize onAccessTokenRefreshFailedCalled =
30     _onAccessTokenRefreshFailedCalled;
31 @synthesize onProfileUpdateCalled = _onProfileUpdateCalled;
32 @synthesize identity = _identity;
33 @synthesize error = _error;
35 - (instancetype)init {
36   if (self == [super init]) {
37     observer_bridge_.reset(new ChromeIdentityServiceObserverBridge(self));
38   }
39   return self;
42 - (ios::ChromeIdentityService::Observer*)observerBridge {
43   return observer_bridge_.get();
46 #pragma mark - ios::ChromeIdentityService::Observer
48 - (void)onIdentityListChanged {
49   _onIdentityListChangedCalled = YES;
52 - (void)onAccessTokenRefreshFailed:(ChromeIdentity*)identity
53                              error:(ios::AccessTokenErrorReason)error {
54   _onAccessTokenRefreshFailedCalled = YES;
55   _error = error;
56   _identity = identity;
59 - (void)onProfileUpdate:(ChromeIdentity*)identity {
60   _onProfileUpdateCalled = YES;
61   _identity = identity;
64 @end
66 #pragma mark - ChromeIdentityServiceObserverBridgeTest
68 class ChromeIdentityServiceObserverBridgeTest : public PlatformTest {
69  protected:
70   ChromeIdentityServiceObserverBridgeTest()
71       : test_observer_([[TestChromeIdentityServiceObserver alloc] init]) {}
73   ios::ChromeIdentityService::Observer* GetObserverBridge() {
74     return [test_observer_ observerBridge];
75   }
77   TestChromeIdentityServiceObserver* GetTestObserver() {
78     return test_observer_;
79   }
81  private:
82   base::scoped_nsobject<TestChromeIdentityServiceObserver> test_observer_;
85 // Tests that |onIdentityListChanged| is forwarded.
86 TEST_F(ChromeIdentityServiceObserverBridgeTest, onIdentityListChanged) {
87   ASSERT_FALSE(GetTestObserver().onIdentityListChangedCalled);
88   GetObserverBridge()->OnIdentityListChanged();
89   EXPECT_TRUE(GetTestObserver().onIdentityListChangedCalled);
90   EXPECT_FALSE(GetTestObserver().onAccessTokenRefreshFailedCalled);
91   EXPECT_FALSE(GetTestObserver().onProfileUpdateCalled);
94 // Tests that |onAccessTokenRefreshFailed| is forwarded.
95 TEST_F(ChromeIdentityServiceObserverBridgeTest, onAccessTokenRefreshFailed) {
96   base::scoped_nsobject<ChromeIdentity> identity([[ChromeIdentity alloc] init]);
97   ios::AccessTokenErrorReason error =
98       ios::AccessTokenErrorReason::UNKNOWN_ERROR;
99   ASSERT_FALSE(GetTestObserver().onAccessTokenRefreshFailedCalled);
100   GetObserverBridge()->OnAccessTokenRefreshFailed(identity, error);
101   EXPECT_FALSE(GetTestObserver().onIdentityListChangedCalled);
102   EXPECT_TRUE(GetTestObserver().onAccessTokenRefreshFailedCalled);
103   EXPECT_FALSE(GetTestObserver().onProfileUpdateCalled);
104   EXPECT_EQ(identity, GetTestObserver().identity);
105   EXPECT_EQ(error, GetTestObserver().error);
108 // Tests that |onProfileUpdate| is forwarded.
109 TEST_F(ChromeIdentityServiceObserverBridgeTest, onProfileUpdate) {
110   base::scoped_nsobject<ChromeIdentity> identity([[ChromeIdentity alloc] init]);
111   ASSERT_FALSE(GetTestObserver().onProfileUpdateCalled);
112   GetObserverBridge()->OnProfileUpdate(identity);
113   EXPECT_FALSE(GetTestObserver().onIdentityListChangedCalled);
114   EXPECT_FALSE(GetTestObserver().onAccessTokenRefreshFailedCalled);
115   EXPECT_TRUE(GetTestObserver().onProfileUpdateCalled);
116   EXPECT_EQ(identity, GetTestObserver().identity);