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

Side by Side Diff: ios/chrome/browser/native_app_launcher/native_app_navigation_controller_unittest.mm

Issue 2585233003: Upstream Chrome on iOS source code [2/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 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 #include <memory>
6
7 #include "base/bind.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/user_metrics.h"
11 #import "ios/chrome/browser/installation_notifier.h"
12 #include "ios/chrome/browser/native_app_launcher/native_app_infobar_delegate.h"
13 #import "ios/chrome/browser/native_app_launcher/native_app_navigation_controller .h"
14 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_provider.h"
15 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_ metadata.h"
16 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_ whitelist_manager.h"
17 #include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h"
18 #include "ios/web/public/test/test_web_thread.h"
19 #include "net/url_request/url_request_test_util.h"
20 #include "testing/platform_test.h"
21
22 @interface NativeAppNavigationController (Testing)
23 - (void)recordInfobarDisplayedOfType:(NativeAppControllerType)type
24 onLinkNavigation:(BOOL)isLinkNavigation;
25 - (NSMutableSet*)appsPossiblyBeingInstalled;
26 - (void)removeAppFromNotification:(NSNotification*)notification;
27 @end
28
29 namespace {
30
31 class FakeChromeBrowserProvider : public ios::TestChromeBrowserProvider {
32 public:
33 FakeChromeBrowserProvider(FakeNativeAppWhitelistManager* fake_manager) {
34 manager_.reset([fake_manager retain]);
35 }
36 ~FakeChromeBrowserProvider() override {}
37
38 id<NativeAppWhitelistManager> GetNativeAppWhitelistManager() const override {
39 return manager_;
40 }
41
42 private:
43 base::scoped_nsprotocol<id<NativeAppWhitelistManager>> manager_;
44 };
45
46 class NativeAppNavigationControllerTest : public PlatformTest {
47 public:
48 NativeAppNavigationControllerTest()
49 : loop_(base::MessageLoop::TYPE_IO),
50 ui_thread_(web::WebThread::UI, &loop_) {}
51
52 protected:
53 void SetUp() override {
54 request_context_getter_ =
55 new net::TestURLRequestContextGetter(loop_.task_runner());
56 controller_.reset([[NativeAppNavigationController alloc]
57 initWithRequestContextGetter:request_context_getter_.get()
58 tab:nil]);
59
60 action_callback_ =
61 base::Bind(&NativeAppNavigationControllerTest::OnUserAction,
62 base::Unretained(this));
63 base::AddActionCallback(action_callback_);
64
65 handler_called_counter_ = 0;
66 }
67
68 void TearDown() override { base::RemoveActionCallback(action_callback_); }
69
70 void SetExpectedActionName(const std::string& action_name) {
71 expected_action_name_.reset(new std::string(action_name));
72 }
73
74 void OnUserAction(const std::string& action_name) {
75 EXPECT_EQ(*expected_action_name_, action_name);
76 handler_called_counter_++;
77 }
78
79 void ExpectHandlerCalledAndReset(int number_of_calls) {
80 EXPECT_EQ(number_of_calls, handler_called_counter_);
81 handler_called_counter_ = 0;
82 }
83
84 base::MessageLoop loop_;
85 web::TestWebThread ui_thread_;
86 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
87 base::scoped_nsobject<NativeAppNavigationController> controller_;
88
89 // The callback to invoke when an action is recorded.
90 base::ActionCallback action_callback_;
91 std::unique_ptr<std::string> expected_action_name_;
92 int handler_called_counter_;
93 };
94
95 TEST_F(NativeAppNavigationControllerTest, TestConstructor) {
96 EXPECT_TRUE(controller_);
97 }
98
99 TEST_F(NativeAppNavigationControllerTest, TestUMA) {
100 SetExpectedActionName("MobileGALInstallInfoBarLinkNavigation");
101 [controller_ recordInfobarDisplayedOfType:NATIVE_APP_INSTALLER_CONTROLLER
102 onLinkNavigation:YES];
103 ExpectHandlerCalledAndReset(1);
104
105 SetExpectedActionName("MobileGALInstallInfoBarDirectNavigation");
106 [controller_ recordInfobarDisplayedOfType:NATIVE_APP_INSTALLER_CONTROLLER
107 onLinkNavigation:NO];
108 ExpectHandlerCalledAndReset(1);
109
110 SetExpectedActionName("MobileGALLaunchInfoBar");
111 [controller_ recordInfobarDisplayedOfType:NATIVE_APP_LAUNCHER_CONTROLLER
112 onLinkNavigation:YES];
113 ExpectHandlerCalledAndReset(1);
114 [controller_ recordInfobarDisplayedOfType:NATIVE_APP_LAUNCHER_CONTROLLER
115 onLinkNavigation:NO];
116 ExpectHandlerCalledAndReset(1);
117
118 SetExpectedActionName("MobileGALOpenPolicyInfoBar");
119 [controller_ recordInfobarDisplayedOfType:NATIVE_APP_OPEN_POLICY_CONTROLLER
120 onLinkNavigation:YES];
121 ExpectHandlerCalledAndReset(1);
122 [controller_ recordInfobarDisplayedOfType:NATIVE_APP_OPEN_POLICY_CONTROLLER
123 onLinkNavigation:NO];
124 ExpectHandlerCalledAndReset(1);
125 }
126
127 TEST_F(NativeAppNavigationControllerTest,
128 TestRemovingAppFromListAfterInstallation) {
129 NSString* const kMapsAppName = @"Maps";
130 NSString* const kMapsAppId = @"1";
131 NSString* const kYoutubeAppName = @"Youtube";
132 NSString* const kYoutubeAppId = @"2";
133
134 base::scoped_nsobject<InstallationNotifier> installationNotifier(
135 [InstallationNotifier alloc]);
136
137 FakeNativeAppWhitelistManager* fakeManager =
138 [[[FakeNativeAppWhitelistManager alloc] init] autorelease];
139 IOSChromeScopedTestingChromeBrowserProvider provider(
140 base::MakeUnique<FakeChromeBrowserProvider>(fakeManager));
141
142 base::scoped_nsobject<FakeNativeAppMetadata> metadataMaps(
143 [[FakeNativeAppMetadata alloc] init]);
144 [metadataMaps setAppName:kMapsAppName];
145 [metadataMaps setAppId:kMapsAppId];
146 ASSERT_TRUE(metadataMaps.get());
147 NSString* appIdMaps = [metadataMaps appId];
148 NSNotification* notificationMaps =
149 [NSNotification notificationWithName:kMapsAppName
150 object:installationNotifier];
151
152 base::scoped_nsobject<FakeNativeAppMetadata> metadataYouTube(
153 [[FakeNativeAppMetadata alloc] init]);
154 [metadataYouTube setAppName:kYoutubeAppName];
155 [metadataYouTube setAppId:kYoutubeAppId];
156
157 ASSERT_TRUE(metadataYouTube.get());
158 NSString* appIdYouTube = [metadataYouTube appId];
159 NSNotification* notificationYouTube =
160 [NSNotification notificationWithName:kYoutubeAppName
161 object:installationNotifier];
162
163 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 0);
164 [[controller_ appsPossiblyBeingInstalled] addObject:appIdMaps];
165 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 1);
166 [[controller_ appsPossiblyBeingInstalled] addObject:appIdYouTube];
167 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 2);
168 [fakeManager setAppScheme:kMapsAppName];
169 [controller_ removeAppFromNotification:notificationMaps];
170 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 1);
171 [fakeManager setAppScheme:kYoutubeAppName];
172 [controller_ removeAppFromNotification:notificationYouTube];
173 DCHECK([[controller_ appsPossiblyBeingInstalled] count] == 0);
174 }
175
176 } // namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/native_app_launcher/native_app_navigation_controller.mm ('k') | ios/chrome/browser/net/cookies_egtest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698