1 // Copyright 2020 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 "ash/public/cpp/app_list/app_list_switches.h"
6 #include "base/command_line.h"
7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/app_list/search/cros_action_history/cros_action_recorder.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 #include "base/strings/strcat.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_activity_simulator.h"
15 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
16 #include "chrome/test/base/test_browser_window.h"
17 
18 namespace app_list {
19 
TestUrls()20 const std::vector<GURL>& TestUrls() {
21   static base::NoDestructor<std::vector<GURL>> test_urls{
22       {GURL("https://test0.example.com"), GURL("https://test1.example.com")}};
23   return *test_urls;
24 }
25 
26 // Test functions of CrOSActionRecorder.
27 class CrOSActionRecorderTabTrackerTest
28     : public ChromeRenderViewHostTestHarness {
29  protected:
SetUp()30   void SetUp() override {
31     ChromeRenderViewHostTestHarness::SetUp();
32     // Set the flag.
33     base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
34         ash::switches::kEnableCrOSActionRecorder,
35         ash::switches::kCrOSActionRecorderWithoutHash);
36 
37     // Initialize CrOSActionRecorder.
38     CrOSActionRecorder::GetCrosActionRecorder()->Init(profile());
39 
40     // Initialize browser.
41     browser_ = CreateBrowserWithTestWindowForParams(
42         Browser::CreateParams(profile(), true));
43     tab_strip_model_ = browser_->tab_strip_model();
44   }
45 
TearDown()46   void TearDown() override {
47     tab_strip_model_->CloseAllTabs();
48     browser_.reset();
49     ChromeRenderViewHostTestHarness::TearDown();
50   }
51 
GetCrOSActionHistory()52   CrOSActionHistoryProto GetCrOSActionHistory() {
53     return CrOSActionRecorder::GetCrosActionRecorder()->actions_;
54   }
55 
56   // Add a tab with url as TestUrls[i] and navigate to it immediately.
AddNewTab(const int i)57   void AddNewTab(const int i) {
58     tab_activity_simulator_.AddWebContentsAndNavigate(tab_strip_model_,
59                                                       TestUrls()[i]);
60     if (i == 0)
61       tab_strip_model_->ActivateTabAt(i);
62     else
63       tab_activity_simulator_.SwitchToTabAt(tab_strip_model_, i);
64   }
65 
ExpectCrOSAction(const CrOSActionProto & action,const std::string & action_name,const int url_index)66   void ExpectCrOSAction(const CrOSActionProto& action,
67                         const std::string& action_name,
68                         const int url_index) {
69     // Expected action_name should be action_name-url;
70     const std::string expected_action_name =
71         base::StrCat({action_name, "-", TestUrls()[url_index].spec()});
72     EXPECT_EQ(action.action_name(), expected_action_name);
73   }
74 
75   TabActivitySimulator tab_activity_simulator_;
76   TabStripModel* tab_strip_model_;
77   std::unique_ptr<Browser> browser_;
78 };
79 
80 // Check navigations and reactivations are logged properly.
TEST_F(CrOSActionRecorderTabTrackerTest,NavigateAndForegroundTest)81 TEST_F(CrOSActionRecorderTabTrackerTest, NavigateAndForegroundTest) {
82   // Add and activate the first tab will only log a TabNavigated event.
83   AddNewTab(0);
84   ExpectCrOSAction(GetCrOSActionHistory().actions(0), "TabNavigated", 0);
85 
86   // Add and activate the second tab will log a TabNavigated event, and then a
87   // TabReactivated event.
88   AddNewTab(1);
89   ExpectCrOSAction(GetCrOSActionHistory().actions(1), "TabNavigated", 1);
90   ExpectCrOSAction(GetCrOSActionHistory().actions(2), "TabReactivated", 1);
91 
92   // Switch to tab@0 will log a TabReactivated event.
93   tab_activity_simulator_.SwitchToTabAt(tab_strip_model_, 0);
94   ExpectCrOSAction(GetCrOSActionHistory().actions(3), "TabReactivated", 0);
95 
96   // Check no extra events are logged.
97   EXPECT_EQ(GetCrOSActionHistory().actions_size(), 4);
98 }
99 
100 }  // namespace app_list
101