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