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#include "base/strings/string16.h"
6#include "chrome/browser/extensions/api/screenlock_private/screenlock_private_api.h"
7#include "chrome/browser/extensions/extension_apitest.h"
8#include "chrome/browser/signin/easy_unlock_service.h"
9#include "chrome/browser/signin/signin_manager_factory.h"
10#include "components/signin/core/browser/signin_manager.h"
11#include "components/signin/core/common/profile_management_switches.h"
12#include "content/public/browser/notification_service.h"
13#include "extensions/browser/api/test/test_api.h"
14#include "extensions/browser/notification_types.h"
15#include "extensions/common/switches.h"
16
17namespace extensions {
18
19namespace {
20
21const char kAttemptClickAuthMessage[] = "attemptClickAuth";
22const char kTestExtensionId[] = "lkegkdgachcnekllcdfkijonogckdnjo";
23const char kTestUser[] = "testuser@gmail.com";
24
25}  // namespace
26
27class ScreenlockPrivateApiTest : public ExtensionApiTest,
28                                 public content::NotificationObserver {
29 public:
30  ScreenlockPrivateApiTest() {}
31
32  virtual ~ScreenlockPrivateApiTest() {}
33
34  // ExtensionApiTest
35  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
36    ExtensionApiTest::SetUpCommandLine(command_line);
37    command_line->AppendSwitchASCII(
38        extensions::switches::kWhitelistedExtensionID, kTestExtensionId);
39
40#if !defined(OS_CHROMEOS)
41    // New profile management needs to be on for non-ChromeOS lock.
42    ::switches::EnableNewProfileManagementForTesting(command_line);
43#endif
44  }
45
46  virtual void SetUpOnMainThread() OVERRIDE {
47    SigninManagerFactory::GetForProfile(profile())
48        ->SetAuthenticatedUsername(kTestUser);
49    ExtensionApiTest::SetUpOnMainThread();
50  }
51
52 protected:
53  // ExtensionApiTest override:
54  virtual void RunTestOnMainThreadLoop() OVERRIDE {
55    registrar_.Add(this,
56                   extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE,
57                   content::NotificationService::AllSources());
58    ExtensionApiTest::RunTestOnMainThreadLoop();
59    registrar_.RemoveAll();
60  }
61
62  // content::NotificationObserver override:
63  virtual void Observe(int type,
64                       const content::NotificationSource& source,
65                       const content::NotificationDetails& details) OVERRIDE {
66    const std::string& content = *content::Details<std::string>(details).ptr();
67    if (content == kAttemptClickAuthMessage) {
68      ScreenlockBridge::Get()->lock_handler()->SetAuthType(
69          kTestUser,
70          ScreenlockBridge::LockHandler::USER_CLICK,
71          base::string16());
72      EasyUnlockService::Get(profile())->AttemptAuth(kTestUser);
73    }
74  }
75
76  // Loads |extension_name| as appropriate for the platform and waits for a
77  // pass / fail notification.
78  void RunTest(const std::string& extension_name) {
79#if defined(OS_CHROMEOS)
80    ASSERT_TRUE(RunComponentExtensionTest(extension_name)) << message_;
81#else
82    ASSERT_TRUE(RunExtensionTest(extension_name)) << message_;
83#endif
84  }
85
86 private:
87  content::NotificationRegistrar registrar_;
88
89  DISALLOW_COPY_AND_ASSIGN(ScreenlockPrivateApiTest);
90};
91
92IN_PROC_BROWSER_TEST_F(ScreenlockPrivateApiTest, LockUnlock) {
93  RunTest("screenlock_private/lock_unlock");
94}
95
96IN_PROC_BROWSER_TEST_F(ScreenlockPrivateApiTest, AuthType) {
97  RunTest("screenlock_private/auth_type");
98}
99
100}  // namespace extensions
101