File: one_click_signin_sync_observer_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (244 lines) | stat: -rw-r--r-- 9,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/sync/one_click_signin_sync_observer.h"

#include "base/bind.h"
#include "base/callback.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/signin/signin_promo.h"
#include "chrome/browser/sync/profile_sync_components_factory_mock.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/sync/startup_controller.h"
#include "chrome/browser/sync/test_profile_sync_service.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/testing_profile.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;

namespace {

const char kContinueUrl[] = "https://www.example.com/";

class MockWebContentsObserver : public content::WebContentsObserver {
 public:
  explicit MockWebContentsObserver(content::WebContents* web_contents)
      : content::WebContentsObserver(web_contents) {}
  virtual ~MockWebContentsObserver() {}

  // A hook to verify that the OneClickSigninSyncObserver initiated a redirect
  // to the continue URL. Navigations in unit_tests never complete, but a
  // navigation start is a sufficient signal for the purposes of this test.
  // Listening for this call also has the advantage of being synchronous.
  MOCK_METHOD1(AboutToNavigateRenderFrame, void(content::RenderFrameHost*));
};

class OneClickTestProfileSyncService : public TestProfileSyncService {
 public:
  ~OneClickTestProfileSyncService() override {}

  // Helper routine to be used in conjunction with
  // BrowserContextKeyedServiceFactory::SetTestingFactory().
  static KeyedService* Build(content::BrowserContext* profile) {
    return new OneClickTestProfileSyncService(static_cast<Profile*>(profile));
  }

  bool FirstSetupInProgress() const override {
    return first_setup_in_progress_;
  }

  bool SyncActive() const override { return sync_active_; }

  void set_first_setup_in_progress(bool in_progress) {
    first_setup_in_progress_ = in_progress;
  }

  void set_sync_active(bool active) {
    sync_active_ = active;
  }

 private:
  explicit OneClickTestProfileSyncService(Profile* profile)
      : TestProfileSyncService(
          scoped_ptr<ProfileSyncComponentsFactory>(
              new ProfileSyncComponentsFactoryMock()),
          profile,
          SigninManagerFactory::GetForProfile(profile),
          ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
          browser_sync::MANUAL_START),
        first_setup_in_progress_(false),
        sync_active_(false) {}

  bool first_setup_in_progress_;
  bool sync_active_;
};

class TestOneClickSigninSyncObserver : public OneClickSigninSyncObserver {
 public:
  typedef base::Callback<void(TestOneClickSigninSyncObserver*)>
      DestructionCallback;

  TestOneClickSigninSyncObserver(content::WebContents* web_contents,
                                 const GURL& continue_url,
                                 const DestructionCallback& callback)
      : OneClickSigninSyncObserver(web_contents, continue_url),
        destruction_callback_(callback) {}
  ~TestOneClickSigninSyncObserver() override {
    destruction_callback_.Run(this);
  }

 private:
  DestructionCallback destruction_callback_;

  DISALLOW_COPY_AND_ASSIGN(TestOneClickSigninSyncObserver);
};

// A trivial factory to build a null service.
KeyedService* BuildNullService(content::BrowserContext* context) {
  return NULL;
}

}  // namespace

class OneClickSigninSyncObserverTest : public ChromeRenderViewHostTestHarness {
 public:
  OneClickSigninSyncObserverTest()
      : sync_service_(NULL),
        sync_observer_(NULL),
        sync_observer_destroyed_(true) {}

  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();
    web_contents_observer_.reset(new MockWebContentsObserver(web_contents()));
    sync_service_ =
        static_cast<OneClickTestProfileSyncService*>(
            ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
                profile(), OneClickTestProfileSyncService::Build));
  }

  void TearDown() override {
    // Verify that the |sync_observer_| unregistered as an observer from the
    // sync service and freed its memory.
    EXPECT_TRUE(sync_observer_destroyed_);
    if (sync_service_)
      EXPECT_FALSE(sync_service_->HasObserver(sync_observer_));
    ChromeRenderViewHostTestHarness::TearDown();
  }

 protected:
  void CreateSyncObserver(const std::string& url) {
    sync_observer_ = new TestOneClickSigninSyncObserver(
      web_contents(), GURL(url),
      base::Bind(&OneClickSigninSyncObserverTest::OnSyncObserverDestroyed,
                 base::Unretained(this)));
    if (sync_service_)
      EXPECT_TRUE(sync_service_->HasObserver(sync_observer_));
    EXPECT_TRUE(sync_observer_destroyed_);
    sync_observer_destroyed_ = false;
  }

  OneClickTestProfileSyncService* sync_service_;
  scoped_ptr<MockWebContentsObserver> web_contents_observer_;

 private:
  void OnSyncObserverDestroyed(TestOneClickSigninSyncObserver* observer) {
    EXPECT_EQ(sync_observer_, observer);
    EXPECT_FALSE(sync_observer_destroyed_);
    sync_observer_destroyed_ = true;
  }

  TestOneClickSigninSyncObserver* sync_observer_;
  bool sync_observer_destroyed_;
};

// Verify that if no Sync service is present, e.g. because Sync is disabled, the
// observer immediately loads the continue URL.
TEST_F(OneClickSigninSyncObserverTest, NoSyncService_RedirectsImmediately) {
  // Simulate disabling Sync.
  sync_service_ =
      static_cast<OneClickTestProfileSyncService*>(
          ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
              profile(), BuildNullService));

  // The observer should immediately redirect to the continue URL.
  EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_));
  CreateSyncObserver(kContinueUrl);
  EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL());

  // The |sync_observer_| will be destroyed asynchronously, so manually pump
  // the message loop to wait for the destruction.
  content::RunAllPendingInMessageLoop();
}

// Verify that when the WebContents is destroyed without any Sync notifications
// firing, the observer cleans up its memory without loading the continue URL.
TEST_F(OneClickSigninSyncObserverTest, WebContentsDestroyed) {
  EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0);
  CreateSyncObserver(kContinueUrl);
  SetContents(NULL);
}

// Verify that when Sync is configured successfully, the observer loads the
// continue URL and cleans up after itself.
TEST_F(OneClickSigninSyncObserverTest,
       OnSyncStateChanged_SyncConfiguredSuccessfully) {
  CreateSyncObserver(kContinueUrl);
  sync_service_->set_first_setup_in_progress(false);
  sync_service_->set_sync_active(true);

  EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_));
  sync_service_->NotifyObservers();
  EXPECT_EQ(GURL(kContinueUrl), web_contents()->GetVisibleURL());
}

// Verify that when Sync configuration fails, the observer does not load the
// continue URL, but still cleans up after itself.
TEST_F(OneClickSigninSyncObserverTest,
       OnSyncStateChanged_SyncConfigurationFailed) {
  CreateSyncObserver(kContinueUrl);
  sync_service_->set_first_setup_in_progress(false);
  sync_service_->set_sync_active(false);

  EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0);
  sync_service_->NotifyObservers();
  EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL());
}

// Verify that when Sync sends a notification while setup is not yet complete,
// the observer does not load the continue URL, and continues to wait.
TEST_F(OneClickSigninSyncObserverTest,
       OnSyncStateChanged_SyncConfigurationInProgress) {
  CreateSyncObserver(kContinueUrl);
  sync_service_->set_first_setup_in_progress(true);
  sync_service_->set_sync_active(false);

  EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0);
  sync_service_->NotifyObservers();
  EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL());

  // Trigger an event to force state to be cleaned up.
  SetContents(NULL);
}

// Verify that if the continue_url is to the settings page, no navigation is
// triggered, since it would be redundant.
TEST_F(OneClickSigninSyncObserverTest,
       OnSyncStateChanged_SyncConfiguredSuccessfully_SourceIsSettings) {
  GURL continue_url = signin::GetPromoURL(
      signin_metrics::SOURCE_SETTINGS, false);
  CreateSyncObserver(continue_url.spec());
  sync_service_->set_first_setup_in_progress(false);
  sync_service_->set_sync_active(true);

  EXPECT_CALL(*web_contents_observer_, AboutToNavigateRenderFrame(_)).Times(0);
  sync_service_->NotifyObservers();
  EXPECT_NE(GURL(kContinueUrl), web_contents()->GetVisibleURL());
}