token_service_unittest.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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// This file defines a unit test harness for the profile's token service.
6
7#ifndef CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_UNITTEST_H_
8#define CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_UNITTEST_H_
9#pragma once
10
11#include "chrome/browser/net/gaia/token_service.h"
12#include "chrome/browser/password_manager/encryptor.h"
13#include "chrome/browser/webdata/web_data_service.h"
14#include "chrome/common/net/gaia/gaia_auth_consumer.h"
15#include "chrome/common/notification_details.h"
16#include "chrome/common/notification_source.h"
17#include "chrome/test/signaling_task.h"
18#include "chrome/test/test_notification_tracker.h"
19#include "chrome/test/testing_profile.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22// TestNotificationTracker doesn't do a deep copy on the notification details.
23// We have to in order to read it out, or we have a bad ptr, since the details
24// are a reference on the stack.
25class TokenAvailableTracker : public TestNotificationTracker {
26 public:
27  const TokenService::TokenAvailableDetails& details() {
28    return details_;
29  }
30
31 private:
32  virtual void Observe(NotificationType type,
33                       const NotificationSource& source,
34                       const NotificationDetails& details) {
35    TestNotificationTracker::Observe(type, source, details);
36    if (type == NotificationType::TOKEN_AVAILABLE) {
37      Details<const TokenService::TokenAvailableDetails> full = details;
38      details_ = *full.ptr();
39    }
40  }
41
42  TokenService::TokenAvailableDetails details_;
43};
44
45class TokenFailedTracker : public TestNotificationTracker {
46 public:
47  const TokenService::TokenRequestFailedDetails& details() {
48    return details_;
49  }
50
51 private:
52  virtual void Observe(NotificationType type,
53                       const NotificationSource& source,
54                       const NotificationDetails& details) {
55    TestNotificationTracker::Observe(type, source, details);
56    if (type == NotificationType::TOKEN_REQUEST_FAILED) {
57      Details<const TokenService::TokenRequestFailedDetails> full = details;
58      details_ = *full.ptr();
59    }
60  }
61
62  TokenService::TokenRequestFailedDetails details_;
63};
64
65class TokenServiceTestHarness : public testing::Test {
66 public:
67  TokenServiceTestHarness()
68      : ui_thread_(BrowserThread::UI, &message_loop_),
69        db_thread_(BrowserThread::DB) {
70  }
71
72  virtual void SetUp() {
73#if defined(OS_MACOSX)
74    Encryptor::UseMockKeychain(true);
75#endif
76    credentials_.sid = "sid";
77    credentials_.lsid = "lsid";
78    credentials_.token = "token";
79    credentials_.data = "data";
80
81    ASSERT_TRUE(db_thread_.Start());
82
83    profile_.reset(new TestingProfile());
84    profile_->CreateWebDataService(false);
85    WaitForDBLoadCompletion();
86
87    success_tracker_.ListenFor(NotificationType::TOKEN_AVAILABLE,
88                               Source<TokenService>(&service_));
89    failure_tracker_.ListenFor(NotificationType::TOKEN_REQUEST_FAILED,
90                               Source<TokenService>(&service_));
91
92    service_.Initialize("test", profile_.get());
93
94    URLFetcher::set_factory(NULL);
95  }
96
97  virtual void TearDown() {
98    // You have to destroy the profile before the db_thread_ stops.
99    if (profile_.get()) {
100      profile_.reset(NULL);
101    }
102
103    db_thread_.Stop();
104    MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask);
105    MessageLoop::current()->Run();
106  }
107
108  void WaitForDBLoadCompletion() {
109    // The WebDB does all work on the DB thread. This will add an event
110    // to the end of the DB thread, so when we reach this task, all DB
111    // operations should be complete.
112    WaitableEvent done(false, false);
113    BrowserThread::PostTask(
114        BrowserThread::DB, FROM_HERE, new SignalingTask(&done));
115    done.Wait();
116
117    // Notifications should be returned from the DB thread onto the UI thread.
118    message_loop_.RunAllPending();
119  }
120
121  MessageLoopForUI message_loop_;
122  BrowserThread ui_thread_;  // Mostly so DCHECKS pass.
123  BrowserThread db_thread_;  // WDS on here
124
125  TokenService service_;
126  TokenAvailableTracker success_tracker_;
127  TokenFailedTracker failure_tracker_;
128  GaiaAuthConsumer::ClientLoginResult credentials_;
129  scoped_ptr<TestingProfile> profile_;
130};
131
132#endif  // CHROME_BROWSER_NET_GAIA_TOKEN_SERVICE_UNITTEST_H_
133