Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: ios/chrome/browser/ui/promos/signin_promo_view_controller_unittest.mm

Issue 2589803002: Upstream Chrome on iOS source code [6/11]. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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 <memory>
8
9 #import "base/mac/scoped_nsobject.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/version.h"
13 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "components/sync_preferences/pref_service_mock_factory.h"
15 #include "components/sync_preferences/pref_service_syncable.h"
16 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
17 #include "ios/chrome/browser/prefs/browser_prefs.h"
18 #include "ios/chrome/browser/signin/authentication_service_factory.h"
19 #import "ios/chrome/browser/signin/authentication_service_fake.h"
20 #import "ios/chrome/browser/ui/promos/signin_promo_view_controller.h"
21 #include "ios/chrome/test/block_cleanup_test.h"
22 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service. h"
23 #include "ios/web/public/test/test_web_thread_bundle.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #import "third_party/ocmock/OCMock/OCMock.h"
26 #import "third_party/ocmock/gtest_support.h"
27
28 @interface SigninPromoViewController (ExposedForTesting)
29 @property(nonatomic, readonly) UIView* accountsContainer;
30 - (void)recordPromoDisplayed;
31 + (base::Version)currentVersion;
32 @end
33
34 namespace {
35 // Fake version used in tests.
36 base::Version* gFakeCurrentVersion = nullptr;
37 }
38
39 @interface FakeVersionSigninPromoViewController : SigninPromoViewController
40 + (void)setFakeCurrentVersion:(const std::string&)fakeCurrentVersion;
41 + (void)clearFakeCurrentVersion;
42 @end
43
44 @implementation FakeVersionSigninPromoViewController
45 + (base::Version)currentVersion {
46 DCHECK(gFakeCurrentVersion);
47 return *gFakeCurrentVersion;
48 }
49
50 + (void)setFakeCurrentVersion:(const std::string&)fakeCurrentVersion {
51 [self clearFakeCurrentVersion];
52 gFakeCurrentVersion = new base::Version(fakeCurrentVersion);
53 }
54
55 + (void)clearFakeCurrentVersion {
56 if (gFakeCurrentVersion) {
57 delete gFakeCurrentVersion;
58 gFakeCurrentVersion = nullptr;
59 }
60 }
61 @end
62
63 namespace {
64
65 enum ButtonTestingID {
66 // IBUITag specified in the .xib file.
67 FRONT_CLOSE_BUTTON_ID = 202,
68 };
69
70 class SigninPromoViewControllerTest : public BlockCleanupTest {
71 public:
72 ~SigninPromoViewControllerTest() override {}
73
74 SigninPromoViewController* controller() { return controller_.get(); }
75
76 void SetUp() override {
77 BlockCleanupTest::SetUp();
78 [FakeVersionSigninPromoViewController setFakeCurrentVersion:"1.0"];
79 TestChromeBrowserState::Builder builder;
80 builder.SetPrefService(CreatePrefService());
81 builder.AddTestingFactory(
82 AuthenticationServiceFactory::GetInstance(),
83 AuthenticationServiceFake::CreateAuthenticationService);
84 chrome_browser_state_ = builder.Build();
85 ios::FakeChromeIdentityService::GetInstanceFromChromeProvider()
86 ->AddIdentities(@[ @"foo", @"bar" ]);
87 controller_.reset([[FakeVersionSigninPromoViewController alloc]
88 initWithBrowserState:chrome_browser_state_.get()]);
89 }
90
91 void TearDown() override {
92 NSUserDefaults* standardDefaults = [NSUserDefaults standardUserDefaults];
93 [standardDefaults removeObjectForKey:kDisplayedSSORecallForMajorVersionKey];
94 [standardDefaults synchronize];
95 [FakeVersionSigninPromoViewController clearFakeCurrentVersion];
96 BlockCleanupTest::TearDown();
97 }
98
99 std::unique_ptr<sync_preferences::PrefServiceSyncable> CreatePrefService() {
100 sync_preferences::PrefServiceMockFactory factory;
101 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
102 new user_prefs::PrefRegistrySyncable);
103 std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs =
104 factory.CreateSyncable(registry.get());
105 RegisterBrowserStatePrefs(registry.get());
106 return prefs;
107 }
108
109 void SetPromoHasBeenShown() {
110 SigninPromoViewController* signin_promo_view_controller =
111 static_cast<SigninPromoViewController*>(controller());
112 EXPECT_TRUE([signin_promo_view_controller
113 isKindOfClass:[SigninPromoViewController class]]);
114 [signin_promo_view_controller recordPromoDisplayed];
115 }
116
117 protected:
118 web::TestWebThreadBundle thread_bundle_;
119 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
120 base::scoped_nsobject<SigninPromoViewController> controller_;
121 };
122
123 TEST_F(SigninPromoViewControllerTest, TestConstructorDestructor) {
124 EXPECT_TRUE(controller());
125 EXPECT_TRUE([controller() view]);
126 }
127
128 TEST_F(SigninPromoViewControllerTest, TestWillDisplay) {
129 EXPECT_TRUE([SigninPromoViewController
130 shouldBePresentedForBrowserState:chrome_browser_state_.get()]);
131 }
132
133 TEST_F(SigninPromoViewControllerTest, TestWillNotDisplaySameVersion) {
134 SetPromoHasBeenShown();
135 EXPECT_FALSE([FakeVersionSigninPromoViewController
136 shouldBePresentedForBrowserState:chrome_browser_state_.get()]);
137 }
138
139 TEST_F(SigninPromoViewControllerTest, TestWillNotDisplayOneMinorVersion) {
140 SetPromoHasBeenShown();
141 // Set the future version to be one minor release ahead.
142 [FakeVersionSigninPromoViewController setFakeCurrentVersion:"1.1"];
143 EXPECT_FALSE([FakeVersionSigninPromoViewController
144 shouldBePresentedForBrowserState:chrome_browser_state_.get()]);
145 }
146
147 TEST_F(SigninPromoViewControllerTest, TestWillNotDisplayTwoMinorVersions) {
148 SetPromoHasBeenShown();
149 // Set the future version to be two minor releases ahead.
150 [FakeVersionSigninPromoViewController setFakeCurrentVersion:"1.2"];
151 EXPECT_FALSE([FakeVersionSigninPromoViewController
152 shouldBePresentedForBrowserState:chrome_browser_state_.get()]);
153 }
154
155 TEST_F(SigninPromoViewControllerTest, TestWillNotDisplayOneMajorVersion) {
156 SetPromoHasBeenShown();
157 // Set the future version to be one major release ahead.
158 [FakeVersionSigninPromoViewController setFakeCurrentVersion:"2.0"];
159 EXPECT_FALSE([FakeVersionSigninPromoViewController
160 shouldBePresentedForBrowserState:chrome_browser_state_.get()]);
161 }
162
163 TEST_F(SigninPromoViewControllerTest, TestWillDisplayTwoMajorVersions) {
164 SetPromoHasBeenShown();
165 // Set the future version to be two major releases ahead.
166 [FakeVersionSigninPromoViewController setFakeCurrentVersion:"3.0"];
167 EXPECT_TRUE([FakeVersionSigninPromoViewController
168 shouldBePresentedForBrowserState:chrome_browser_state_.get()]);
169 }
170
171 } // namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/ui/promos/signin_promo_view_controller.mm ('k') | ios/chrome/browser/ui/qr_scanner/README.md » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698